简体   繁体   中英

Start scrolling before overflow when dragging the recyclerview item in Android

Controlling the drag and drop events with ItemTouchHelper.SimpleCallback on my RecyclerView as follow:

@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {

    int fromPosition = viewHolder.getAdapterPosition();
    int toPosition = target.getAdapterPosition();

    Collections.swap(adapter.getShelves(), fromPosition, toPosition);
    adapter.notifyItemMoved(fromPosition, toPosition);

    return true;
}

The item dragging with a long press. When the dragging item overflows the screen, the scrolling is starting to its direction by stages normally. The part of the holder for dragging is top of the item so there isn't any problem when dragging down but dragging up is very difficult for people because the fingers overflow the device. How to start dragging to up/down when don't just reached, or before certain margin to screen bounds?

your can use library github link

add to dependencies:

   dependencies {
    implementation 'com.thesurix.gesturerecycler:gesture-recycler:1.11.0'
}

and kotlin:

    // Define your RecyclerView and adapter as usually
val manager = LinearLayoutManager(context)
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = manager

// Extend GestureAdapter and write your own
// ViewHolder items must extend GestureViewHolder
val adapter = MonthsAdapter(R.layout.linear_item)
adapter.data = months
recyclerView.adapter = adapter

and Swipe:

 val gestureManager = GestureManager.Builder(recyclerView)
                 // Enable swipe
                .setSwipeEnabled(true)
                 // Enable long press drag and drop 
                .setLongPressDragEnabled(true)
                 // Enable manual drag from the beginning, you need to provide View inside your GestureViewHolder
                .setManualDragEnabled(true)
                 // Use custom gesture flags
                 // Do not use those methods if you want predefined flags for RecyclerView layout manager 
                .setSwipeFlags(ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT)
                .setDragFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN)
                .build()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM