android-drag-FlowLayout

repository·master·Indexed 20 days ago

https://github.com/lightsun/android-drag-flowlayout

A draggable flow layout library for Android that supports AndroidX. It provides a GridView-like experience with variable width items that wrap automatically and support long-press dragging for reordering. Key components include DragFlowLayout for XML layouts, DragAdapter for data binding, and the IDraggable interface to control item draggability.

Tokens
2K
Snippets
7
Records
7
Agent score
22%

What's inside android-drag-flowlayout

  1. Prevent specific items from being dragged

    master

    By default, all items are draggable. To disable dragging for specific items, ensure the data object associated with that item implements the IDraggable interface and returns false for isDraggable().

    // Data entity implementing IDraggable
    private static class TestBean implements IDraggable {
        String text;
        boolean draggable = true; 
    
        public TestBean(String text) {
            this.text = text;
        }
    
        @Override
        public boolean isDraggable() {
            return draggable;
        }
    }
  2. Install android-drag-FlowLayout

    master

    To use DragFlowLayout, add the JitPack repository to your root build.gradle file and then implement the dependency. If you are using AndroidX, use the version suffix -x (e.g., 1.8.8-x).

    // root gradle
    allprojects {
        repositories {
            maven { url "https://jitpack.io" }
        }
    }
    
    // implementation
    implementation 'com.github.LightSun:android-drag-FlowLayout:1.8.8-x'
  3. Add DragFlowLayout to XML layout

    master

    Include the DragFlowLayout component in your XML layout file using its full package name.

    <com.heaven7.android.dragflowlayout.DragFlowLayout
        android:id="@+id/drag_flowLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </com.heaven7.android.dragflowlayout.DragFlowLayout>
  4. Handle item click and delete events

    master

    Use setOnItemClickListener with ClickToDeleteItemListenerImpl to handle clicks. This specific implementation allows you to define a callback for when a delete action (e.g., clicking a close button within an item) is successful.

    mDragflowLayout.setOnItemClickListener(new ClickToDeleteItemListenerImpl(R.id.iv_close){
         // Callback when deletion is successful
        @Override
        protected void onDeleteSuccess(DragFlowLayout dfl, View child, Object data) {
           // your code to remove data from your source list
        }
    });
  5. Monitor drag state and view changes

    master

    You can listen to changes in the layout's drag state or observe when views are added or removed from the layout.

    // Set drag state listener
    mDragflowLayout.setOnDragStateChangeListener(new DragFlowLayout.OnDragStateChangeListener() {
        @Override
        public void onDragStateChange(DragFlowLayout dfl, int dragState) {
            System.out.println("on drag state change : dragState = " + dragState);
        }
    });
    
    // Add view observer
    mDragflowLayout.addViewObserver(new IViewObserver() {
        @Override
        public void onAddView(View child, int index) {
            Logger.i(TAG, "onAddView", "index = " + index);
        }
        @Override
        public void onRemoveView(View child, int index) {
            Logger.i(TAG, "onRemoveView", "index = " + index);
        }
    });
  6. Configure DragAdapter to bind data to items

    master

    To display data, you must implement a DragAdapter<T>. The adapter handles the item layout ID, data binding to the view, and retrieving the data object from a view (typically via a tag).

    mDragflowLayout.setDragAdapter(new DragAdapter<TestBean>() {
    
        @Override  // Get your item layout ID
        public int getItemLayoutId() {
            return R.layout.item_drag_flow;
        }
    
        @Override // Bind data to the item view
        public void onBindData(View itemView, int dragState, TestBean data) {
            itemView.setTag(data);
    
            TextView tv = (TextView) itemView.findViewById(R.id.tv_text);
            tv.setText(data.text);
            
            // Example: Show a close button only during drag mode
            itemView.findViewById(R.id.iv_close).setVisibility(
                    dragState != DragFlowLayout.DRAG_STATE_IDLE
                    && data.draggable ? View.VISIBLE : View.INVISIBLE);
        }
    
        @NonNull
        @Override // Retrieve data from the specific child view
        public TestBean getData(View itemView) {
            return (TestBean) itemView.getTag();
        }
    });
  7. Reference: DragFlowLayout Public API

    master

    The following methods are available on the DragFlowLayout instance to control its behavior and state.

    // Set drag state listener
    public void setOnDragStateChangeListener(OnDragStateChangeListener l)
    
    // Get current drag state
    public @DragState int getDragState()
    
    // Set item click event handler
    public void setOnItemClickListener(OnItemClickListener l)
    
    // Set the data adapter
    void setDragAdapter(DragAdapter<T> adapter)
    
    // Get the Item Manager for CRUD operations (Add, Delete, Update, Query)
    public DragItemManager getDragItemManager()
    
    // Set whether dragging is globally enabled
    public void setDraggable(boolean draggable)
    
    // Set the number of cached views to avoid frequent creation
    public void prepareItemsByCount(int count)
    
    // Manually trigger the start of a drag (enters draggable state)
    public void beginDrag()
    
    // Manually trigger the end of a drag (returns to IDLE state)
    public void finishDrag()