在Android中具有拖放功能的可扩展LIst

时间:2013-08-16 05:25:17

标签: android android-listview drag-and-drop expandablelistview

我想创建一个具有拖放功能的可扩展列表视图,即我可以通过拖放来安排内部或外部项目

4 个答案:

答案 0 :(得分:1)

您可以从实现View.OnDragListener的类创建拖动事件侦听器对象(“侦听器”)。您可以使用View对象的setOnDragListener()方法为View设置拖动事件侦听器对象。每个View对象还有一个onDragEvent()回调方法。这两个都在developer section

中有更详细的描述

请参阅此链接以获取工作样本: bit.ly/16r49T3

答案 1 :(得分:1)

您需要添加一些侦听器才能使其正常工作。这些监听器放在它们出现的片段的OnStart中。

第一个监听器如下,假设您调用了listview“lvListView”(这是为了拖动孩子,我在其他地方找到了它):

    lvListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
                int groupPosition = ExpandableListView.getPackedPositionGroup(id);
                int childPosition = ExpandableListView.getPackedPositionChild(id);

                // You now have everything that you would as if this was an OnChildClickListener()
                // Add your logic here.

                //include this:
                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
                view.startDrag(null, shadowBuilder, lvItems.getItemAtPosition(position), 0);
                // Return true as we are handling the event.
                return true;
            }

            return false;
        }
    });

这允许你拖动孩子,但现在你需要一个侦听所有你的DubSteppin'(Drop)。我在我的代码中放置了以下内容:

     lvListView.setOnDragListener(new AdapterView.OnDragListener() {

        @Override
        public boolean onDrag(View v, DragEvent event) {
            int action = event.getAction();
            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:
                    //layoutParams = (RelativeLayout.LayoutParams)v.getLayoutParams();
                    Log.i("msg", "Action is DragEvent.ACTION_DRAG_STARTED");

                    // Do nothing
                    break;

                case DragEvent.ACTION_DRAG_ENTERED:
                    Log.i("msg", "Action is DragEvent.ACTION_DRAG_ENTERED");
                    int x_cord = (int) event.getX();
                    int y_cord = (int) event.getY();
                    break;

                case DragEvent.ACTION_DRAG_EXITED:
                    Log.i("msg", "Action is DragEvent.ACTION_DRAG_EXITED");
                    x_cord = (int) event.getX();
                    y_cord = (int) event.getY();

                    break;

                case DragEvent.ACTION_DRAG_LOCATION:
                    Log.i("msg", "Action is DragEvent.ACTION_DRAG_LOCATION");
                    x_cord = (int) event.getX();
                    y_cord = (int) event.getY();
                    Log.i("msg", Integer.toString(x_cord) + "," + Integer.toString(y_cord));
                    int nPointToPosition = lvListView.pointToPosition(x_cord,y_cord);
                    if(lvListView.getItemAtPosition(nCheck)!= null) {

                        // THE FUN PART IS HERE!
                        // ******this is the header list number******
                        int ngroupPosition = lvListView.getPackedPositionGroup(lvListView.getExpandableListPosition(nCheck));

                        // ******this is the child position******
                        int nchildPosition = lvListView.getPackedPositionChild(lvListView.getExpandableListPosition(nCheck));

                    }
                    break;

                case DragEvent.ACTION_DRAG_ENDED:
                    Log.i("msg", "Action is DragEvent.ACTION_DRAG_ENDED");
                    // This is where I added some activities....
                    break;

                case DragEvent.ACTION_DROP:
                    Log.i("msg", "ACTION_DROP event");
                    // This is also a good place, place with it and see what you want to do
                    break;

                default:
                    break;
            }
            //return value
            return true;
        }
    });//end DragListener

对不起“necropost”,但我想向前付款,因为通常我会在这里的代码块中找到答案。

我也从其他代码块中获得了大部分内容,并通过它来解决问题,所以如果你认识到代码,谢谢!

答案 2 :(得分:0)

答案 3 :(得分:0)

如果有人仍在寻找解决方案,我修改了拖放列表视图并将其更新为拖放可扩展列表视图,请检查答案here