Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Draggable GridLayout | Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 4.71k
    Comment on it

    In draggable gridlayout cells of the GridLayout(consist rows and columns) can be draged from one position to another by using OnDragListener.

     

     

    To use GridLayout, first add gridlayout to project gradle (app level) and follow below steps to add GridLayout:-

    1. go to File->ProjectStructure->app->Dependencies->+->Library dependency
    2. Search for gridLayout and press OK

    3. On Item touch listener of GridLayout Item, GridLayout.setOnDragListener(new DragListener(thumbnailId)) is call to initiate OnDragListener.
     

    class DragListener implements View.OnDragListener {
            public ArrayList<ImageModel> imagesList;
    
            public DragListener(ArrayList<ImageModel> imagesList) {
                this.imagesList = imagesList;
    
            }
    
            @Override
            public boolean onDrag(View v, DragEvent event) {
                final View view = (View) event.getLocalState();
                switch (event.getAction()) {
                    case DragEvent.ACTION_DRAG_LOCATION:
                        if (view == v) return true;
                        index = calculateNewIndex(event.getX(), event.getY());
    
                        if (!checked) {
                            startIndex = index;
                            checked = true;
                        }
    
                        final Rect rect = new Rect();
                        mScrollView.getHitRect(rect);
                        final int scrollY = mScrollView.getScrollY();
    
                        if (event.getY() - scrollY > mScrollView.getBottom() - 250) {
                            startScrolling(scrollY, mGrid.getHeight());
                        } else if (event.getY() - scrollY < mScrollView.getTop() + 250) {
                            startScrolling(scrollY, 0);
                        } else {
                            stopScrolling();
                        }
    
                        // remove the view from the old position
                        try {
                            mGrid.removeView(view);
                            // and push to the new
                            mGrid.addView(view, index);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    case DragEvent.ACTION_DRAG_ENDED:
    
                        if (!event.getResult()) {
                            view.setVisibility(View.VISIBLE);
                        }
                        view.setVisibility(View.VISIBLE);
                        checked = false;
                        int lastIndex = index;
                        if (startIndex > lastIndex) {
                            imagesList.add(lastIndex, imagesList.get(startIndex));
                            imagesList.remove(startIndex + 1);
                            mGrid.removeAllViews();
                            setThumbnail(imagesList);
                        } else if (startIndex < lastIndex) {
                            ImageModel temp = imagesList.get(startIndex);
                            for (int i = startIndex + 1; i <= lastIndex; i++) {
                                imagesList.set(i - 1, imagesList.get(i));
                            }
                            imagesList.set(lastIndex, temp);
                            mGrid.removeAllViews();
                            setThumbnail(imagesList);
                        }
                        break;
                }
                return true;
            }

    Source Code:-please find attachment.

    Draggable GridLayout | Android

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: