Android ListView拖放用于Honeycomb和ICS“错误:报告丢弃结果:false” [英] Android ListView Drag and Drop for Honeycomb and ICS "Error: Reporting drop result: false"

查看:263
本文介绍了Android ListView拖放用于Honeycomb和ICS“错误:报告丢弃结果:false”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying to create a ListView which I can sort using drag and drop.

我试图遵循Android指南这里和Git上提供的一些源代码此处。此外,我不想使用Music应用程序示例,因为我正在尝试使用Honeycomb和更高版本中提供的新工具。

I have attempted to follow the Android guide here and some source code provided on Git over here. Also, I do not want to use the Music app example as I am trying to use the new tools given in Honeycomb and up.

到目前为止,我已经成功地创建了列表,我可以拖动项目。不幸的是,当我将项目删除到列表中时,我收到以下错误:

So far I have been successful in creating the list and I can drag the items. Unfortunately when I drop the item onto the list I get the following error:

I / ViewRoot(22739):报告丢弃结果:false。

"I/ViewRoot(22739): Reporting drop result: false".

我怀疑我的丢包监听器不是在正确的项目上创建的,因此这个drop不会被调用。以下是一些源代码,非常感谢您的帮助。

I have a suspicion that my drop listener is not created on the right item and thus the drop never gets called. Here is some source code, thank you so much for your help.

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>

我的listView:
我还没有能够进入ACTION_DROP事件该代码未测试。只是我正在做的事情我的主要问题是我没有进入ACTION_DROP。

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 case更改为这个时,它似乎起作用:

I could not quite figure out why. But when I changed switch case to this, it seemed to work:

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;

            }


推荐答案

您的更新解决问题,因为您得到 DragEvent.ACTION_DRAG_STARTED 时,必须从 onDrag 返回 true / code>,以便继续向该侦听器接收拖动事件。在您的更新中,对于这种情况,您返回 true ,所以您继续接收拖动事件,并且删除逻辑正常工作。

Your update fixed the problem because you have to return true from onDrag when you get DragEvent.ACTION_DRAG_STARTED in order to continue receiving drag events to that listener. In your update you return true for this case, so you continue receiving drag events and the drop logic works correctly.

如果您不为 DragEvent.ACTION_DRAG_STARTED 案例返回 true ,则您的收听者将无法获得任何其他事件除了 DragEvent.ACTION_DRAG_ENDED

If you don't return true for the DragEvent.ACTION_DRAG_STARTED case, your listener won't get any other events except DragEvent.ACTION_DRAG_ENDED.

这篇关于Android ListView拖放用于Honeycomb和ICS“错误:报告丢弃结果:false”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆