English 中文(简体)
Android ListView 拖放用于honeycomb 和 ICS “错误: 报告下降结果: 假 ”
原标题:Android ListView Drag and Drop for Honeycomb and ICS "Error: Reporting drop result: false"

我一直在尝试创建一个 ListView, 我可以使用拖放来排序 。

我试图遵循Android 指南here 和在上提供的关于Git的一些源代码。此外,我不想使用音乐应用程序的例子,因为我正试图使用Honecomb和Honecomb上的新工具。

到目前为止,我成功地创建了列表,我可以拖动项目。 不幸的是,当我把项目放入列表时,我犯了以下错误:

"I/ViewRooot(22739):报告下降结果:假"

我怀疑我的收听器不是在正确的项目上创建的,因此滴音从未被调用。这里是源代码,非常感谢您的帮助。

列表中的 XML :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dropTarget"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     android:layout_weight="1">
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@android:id/list" >
    </ListView>
</LinearLayout>

My listView: I have not yet been able to get into "ACTION_DROP" event so that code is not tested. Just something I was working on. My main question is that I never get into ACTION_DROP.

public class procedureListView extends ListActivity {
    private ListView mListView = null;
    private ArrayAdapter<String> mArrayAdapter = null;
    private View layoutDropArea = null;

    public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.list);

          String[] countries = getResources().getStringArray(R.array.arrayOfStuff);
          mArrayAdapter = new ArrayAdapter<String>(this, R.layout.list_item, countries);
          setListAdapter(mArrayAdapter);

          mListView = getListView();
          mListView.setTextFilterEnabled(true);

          layoutDropArea = findViewById(R.id.dropTarget);

          setupDragDrop();
    }
    /**
     * Setup what to do when we drag list items
     */
    public void setupDragDrop(){
        mListView.setOnItemLongClickListener(new OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> arg0, View v, int position, long arg3){
                String value = (String) ((TextView) v).getText();
                ClipData data = ClipData.newPlainText("procedure", value);
                v.startDrag(data, new mDragShadowBuilder(v), null, 0);          
                return true;
            }
        });
        myDragListener mDragListener = new myDragListener();
        //mListView.setOnDragListener(mDragListener);

        layoutDropArea.setOnDragListener(mDragListener);



    }
    protected class myDragListener implements OnDragListener{

        public boolean onDrag(View v, DragEvent event) {
            final int action = event.getAction();
            switch (action) {
                case DragEvent.ACTION_DRAG_ENTERED:
                    v.setBackgroundColor(Color.GRAY);
                    break;
                case DragEvent.ACTION_DRAG_EXITED:
                    v.setBackgroundColor(Color.TRANSPARENT);
                    break;
                case DragEvent.ACTION_DRAG_STARTED:
                    break;
                case DragEvent.ACTION_DRAG_LOCATION:
                    v.setVisibility(View.VISIBLE);
                // return processDragStarted(event);
                case DragEvent.ACTION_DROP:
                    v.setBackgroundColor(Color.TRANSPARENT);
                    int newPosition = mListView.getPositionForView(v);
                    if (newPosition != ListView.INVALID_POSITION)
                        return processDrop(event, newPosition);
                    else
                        return false;
            }
            return false;
        }

    }

    private boolean processDrop(DragEvent event, int newPosition) {
        ClipData data = event.getClipData();
        if (data != null) {
            if (data.getItemCount() > 0) {
                Item item = data.getItemAt(0);
                String value = item.toString();
                updateViewsAfterDropComplete(value, newPosition);
                return true;
            }
        }
        return false;
    }
    private void updateViewsAfterDropComplete(String listItem, int index) {
        Log.d("InsertItem", "Position: "+ index);
        mArrayAdapter.insert(listItem, index);
        mArrayAdapter.notifyDataSetChanged();
    }
    private boolean processDragStarted(DragEvent event) {
        ClipDescription clipDesc = event.getClipDescription();
        if (clipDesc != null) {
            return clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
        }
        return false;
    }
}

非常感谢你的帮助!

更新:

我不知道为什么,但当我把案件改成这个案子时,它似乎起作用了:

switch (action) {
                case DragEvent.ACTION_DRAG_ENTERED:
                    //v.setBackgroundColor(Color.GRAY);
                    return false;

                case DragEvent.ACTION_DRAG_EXITED:
                    //v.setBackgroundColor(Color.TRANSPARENT);
                    return true;

                case DragEvent.ACTION_DRAG_STARTED:
                    return true;

                case DragEvent.ACTION_DRAG_LOCATION:
                    //v.setVisibility(View.VISIBLE);
                    return false;
                // return processDragStarted(event);
                case DragEvent.ACTION_DROP:
                    v.setBackgroundColor(Color.TRANSPARENT);
                    int newPosition = mListView.pointToPosition((int)(event.getX()),(int) event.getY());
                    Log.d("Position", Integer.toString(newPosition));
                    if (newPosition != ListView.INVALID_POSITION)
                        return processDrop(event, newPosition);
                    else
                        return false;
                default:
                    return true;

            }
最佳回答

您的更新修复了问题, 因为您要从 true from inDrag 返回 , 当你得到 DragEvent.ACtion_Drag_STAD , 以便继续接收拖动事件到该听众。 在更新中您返回此案例的 true , 因此您继续接收拖动事件, 并正确运行拖放逻辑 。

如果您不返回 true for the DragEvent.ACTION_Drag_STAMED case, 除了 DragEvent.ACTION_DRAG_ENDED , 您的听众将得不到任何其他事件。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...