Android GridView reorder elements via Drag and Drop

后端 未结 8 1271
温柔的废话
温柔的废话 2020-12-12 17:43

I have a GridView in an application I am working on. I would like to be able to reorder the items in the GridView via drag-and-drop. I have found lots of help for ListViews

相关标签:
8条回答
  • 2020-12-12 18:14

    Google recently released a few code labs a few months back. https://codelabs.developers.google.com/codelabs/android-training-adaptive-layouts/index.html?index=..%2F..%2Fandroid-training#0

    You can check the solution to it here https://github.com/google-developer-training/android-fundamentals-apps-v2/tree/master/MaterialMe-Resource

    They make grid layout with movable cards that can be dragged and dropped anywhere in the layout using the itemTouchHandler.

    The more detailed code on how to do the drag and drop is here You need to look into the Task 3: Make your CardView swipeable, movable, and clickable section

    0 讨论(0)
  • 2020-12-12 18:20

    Using the drag-n-drop framework, instead that cycling the childs and setting the draglistener, I use as a grid item layout container, a DragableLinearLayout that extends the LinearLayout and implements the onDragEvent(DragEvent) method.

    So you can fill your grid with the adapter as usual and most of the drag and drop code is on the onDragEvent of DragableLinearLayout

    public class DragableLinearLayout extends LinearLayout {
    
    
        public DragableLinearLayout(Context context, AttributeSet attrs,
                int defStyle) {
            super(context, attrs, defStyle);
    
        }
    
        public DragableLinearLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
    
        }
    
        public DragableLinearLayout(Context context) {
            super(context);
    
        }
    
        @Override
        public boolean onDragEvent(DragEvent event) {
            //in wich grid item am I?
            GridView parent = (GridView) getParent();
            Object item = parent.getAdapter().getItem(
                    parent.getPositionForView(this));
                //if you need the database id of your item...
            Cursor cur = (Cursor) item;
            long l_id = cur.getLong(cur.getColumnIndex("youritemid"));
    
            switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
    
                return true;
            case DragEvent.ACTION_DRAG_ENTERED:
    
                setBackgroundColor(Color.GREEN);
                invalidate();
                return true;
            case DragEvent.ACTION_DRAG_EXITED:
                setBackgroundColor(Color.WHITE);
                invalidate();
                return false;
            case DragEvent.ACTION_DROP:
    ClipData cd = event.getClipData();
                long l_id_start = Long.valueOf(cd.getItemAt(0).getText()
                        .toString());
                //
                Toast.makeText(getContext(), "DROP FROM " + l_id_start
                        + " TO " + l_id, Toast.LENGTH_LONG);
                //do your stuff  
                        ........
                        //the db requery will be on the onDragEvent.drop of the container
                        //see the listener
    
    
                return false;
            case DragEvent.ACTION_DRAG_ENDED:
                setBackgroundColor(Color.WHITE);
                invalidate();
                //
                return false;
    
            }
    
            return true;
    
        }
    
    
    }
    
    
    
    private View.OnDragListener listenerOnDragEvent = new View.OnDragListener() {
    
        public boolean onDrag(View v, DragEvent event) {
            // Defines a variable to store the action type for the incoming
            // event
            final int action = event.getAction();
            switch (action) {
    
            case DragEvent.ACTION_DROP:
    
                // REQUERY
                updateDbView();
                return false;
                // break;
    
            }
            return true;
        }
    };
    
    0 讨论(0)
提交回复
热议问题