Drag and Swipe in RecyclerView. Part 1: ItemTouchHelper
3r3444.
3r33450.
There are many tutorials, libraries, and examples of the implementation of 3r33428. drag & drop
and 3r33428. swipe-to-dismiss
in Android using RecyclerView. Most of them still use the outdated 3r314. View.ondragListener
and approach 3r316. SwipeToDismiss
designed by Roman Nurik. Although new and more effective methods are already available. Very few use the latest API, often relying on 3r33430. GestureDetectors and 3r33430. onInterceptTouchEvent or on other more complex implementations. In fact, there is a very simple way to add these functions to RecyclerView
. This requires only one class, which is also part of the Android Support Library. 3r3444. RecyclerView.ItemDecoration , making it easy to add to almost any existing 3r33030. LayoutManager and adapter. It also works with animation elements and provides the ability to drag items of one type to another place in the list and much more. In this article, I will demonstrate a simple implementation of ItemTouchHelper
. Later, in this series of articles, we will expand the scope and consider the remaining possibilities. 3r3444.
3r33450.
3r33434. Note. 3r33434. Want to see the result right away? Check out Github: Android-ItemTouchHelper-Demo . 3r3442. The first commit 3r3443. refers to this article. Demo 3r33430. .apk file can be downloaded here is . 3r3444.
3r33450.
3r366. 3r3444.
3r33450.
Setup
3r33450.
First we need to configure RecyclerView
. If you have not done so yet, add the dependency RecyclerView
in your file 3r33030. build.gradle . 3r3444.
3r33450. 3r3013. 3r33430. compile 'com.android.support:recyclerview-v7:???' 3r3407.
3r33450.
3r33430. ItemTouchHelper will work with virtually any RecyclerView.Adapter
and 3r33430. LayoutManager , but this article is based on examples using r3r397. These files are . 3r3444.
3r33450. 3r3102. Use ItemTouchHelper and ItemTouchHelper.Callback
3r33450.
To use ItemTouchHelper
, you need to create ItemTouchHelper.Callback . This is an interface that allows you to track the movement (3r33232. Eng. 3r33232. Move) and swipe (3r33232. Eng. 3r33233. Swipe). In addition, here you can monitor the status of the selected view
-component and override the default animation. There is a helper class that you can use if you want to use the basic implementation, 3r3117. SimpleCallback . But in order to understand how this works in practice, we will do everything on our own. 3r3444.
3r33450.
The main functions of the interface that we must override to enable the basic functionality of 3r33428. drag & drop and 3r33428. swipe-to-dismiss :
3r33450. 3r3013. 3r3402. getMovementFlags (RecyclerView, ViewHolder)
3r33450. onMove (RecyclerView, ViewHolder, ViewHolder)
3r33450. onSwiped (ViewHolder, int) 3r3407.
3r33450.
We will also use several auxiliary methods: 3r3444.
3r33450. 3r3013. 3r3402. isLongPressDragEnabled ()
3r33450. isItemViewSwipeEnabled () 3r3407.
3r33450.
Consider them one by one. 3r3444.
3r33450. 3r3013. 3r3402. @Override
public int getMovementFlags (RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; 3r33450. int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END; 3r33450. return makeMovementFlags (dragFlags, swipeFlags); 3r33450.} 3r3407.
3r33450.
3r33430. ItemTouchHelper makes it easy to determine the direction of the event. You need to override the method getMovementFlags ()
to indicate which directions to drag and drop will be supported. To create returned flags, use an auxiliary method ItemTouchHelper.makeMovementFlags (int, int)
. In this example, we allow dragging and brushing in both directions. 3r3444.
3r33450. 3r3013. 3r3402. @Override
public boolean isLongPressDragEnabled () {
return true; 3r33450.} 3r3407.
3r33450.
3r33430. ItemTouchHelper can only be used for dragging without the swipe function (or vice versa), so you must specify which features should be supported. Method isLongPressDragEnabled ()
should return the value true
so that dragging is supported after a long press on element RecyclerView
. Alternatively, you can call method ItemTouchHelper.startDrag (RecyclerView.ViewHolder)
to start dragging manually. Consider this option later. 3r3444.
3r33450. 3r3013. 3r3402. @Override
public boolean isItemViewSwipeEnabled () {
return true; 3r33450.} 3r3407.
3r33450.
To enable swipe after touching anywhere within 3-333430. view -component, just return the value true
from method 3r33030. isItemViewSwipeEnabled () . Alternatively, you can call method ItemTouchHelper.startSwipe (RecyclerView.ViewHolder)
to start swiping manually. 3r3444.
3r33450.
The following two methods, onMove ()
and 3r33430. onSwiped () , are required in order to notify about updating data. So, first we will create an interface that allows you to pass these events along a chain of calls. 3r3444.
3r33450.
3r33232. ItemTouchHelperAdapter.java 3r3444.
3r33450. 3r3013. 3r3402. public interface ItemTouchHelperAdapter {
3r33450. void onItemMove (int fromPosition, int toPosition); 3r33450. 3r33450. void onItemDismiss (int position); 3r33450.} 3r3407.
3r33450.
The easiest way to do this is to make RecyclerListAdapter implemented the listener. 3r3444.
3r33450. 3r3013. 3r3402. public class RecyclerListAdapter extends
RecyclerView.Adapter 3r33450. implements ItemTouchHelperAdapter {
3r33450. //code from[примера](https://gist.github.com/iPaulPro/2216ea5e14818056cfcc#file-recyclerlistadapter-java)
3r33450. @Override
public void onItemDismiss (int position) {
mItems.remove (position); 3r33450. notifyItemRemoved (position); 3r33450.} 3r33450. 3r33450. @Override
public boolean onItemMove (int fromPosition, int toPosition) {
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap (mItems, i, i + 1);
}
} else {
for (int i = fromPosition; i> toPosition; i -) {
Collections.swap (mItems, i, i - 1);
}
}
NotifyItemMoved (fromPosition, toPosition); 3rrr5050. 3r33450.
It is very important to call methods notifyItemRemoved ()
and 3r33430. notifyItemMoved () So that the adapter sees the changes. It should also be noted that we change the position of the element every time view
-component is shifted to a new index, and 3r3343428. not at the very end of the move (event “drop”) 3r33434. . 3r3444.
3r33450.
Now we can go back to creating SimpleItemTouchHelperCallback
since we still need to override the methods onMove ()
and 3r33430. onSwiped () . First add the constructor and field for the adapter:
3r33450. 3r3013. 3r3402. private final ItemTouchHelperAdapter mAdapter; 3r33450. 3r33450. public SimpleItemTouchHelperCallback (
ItemTouchHelperAdapter adapter) {
mAdapter = adapter; 3r33450.} 3r3407.
3r33450.
Then redefine the remaining events and report this to the adapter:
3r33450. 3r3013. 3r3402. @Override
public boolean onMove (RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder,
RecyclerView.ViewHolder target) {
mAdapter.onItemMove (viewHolder.getAdapterPosition (),
target.getAdapterPosition ()); 3r33450. return true; 3r33450.} 3r33450. @Override
public void onSwiped (RecyclerView.ViewHolder viewHolder,
int direction) {
mAdapter.onItemDismiss (viewHolder.getAdapterPosition ()); 3r33450.} 3r3407.
3r33450.
As a result, the class Callback
should look something like this:
3r33450. 3r3013. 3r3402. public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
3r33450. private final ItemTouchHelperAdapter mAdapter; 3r33450. 3r33450. public SimpleItemTouchHelperCallback (ItemTouchHelperAdapter adapter) {
mAdapter = adapter; 3r33450.} 3r33450. 3r33450. @Override
public boolean isLongPressDragEnabled () {
return true; 3r33450.} 3r33450. 3r33450. @Override
public boolean isItemViewSwipeEnabled () {
return true; 3r33450.} 3r33450. 3r33450. @Override
public int getMovementFlags (RecyclerView recyclerView, ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN; 3r33450. int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END; 3r33450. return makeMovementFlags (dragFlags, swipeFlags); 3r33450.} 3r33450. 3r33450. @Override
public boolean onMove (RecyclerView recyclerView, ViewHolder viewHolder,
ViewHolder target) {
mAdapter.onItemMove (viewHolder.getAdapterPosition (), target.getAdapterPosition ()); 3r33450. return true; 3r33450.} 3r33450. 3r33450. @Override
public void onSwiped (ViewHolder viewHolder, int direction) {
mAdapter.onItemDismiss (viewHolder.getAdapterPosition ()); 3r33450.} 3r33450. 3r33450.} 3r3407.
3r33450.
When the callback is ready, we can create ItemTouchHelper
and call the method. attachToRecyclerView (RecyclerView)
(for example, in MainFragment.java ):
3r33450. 3r3013. 3r3402. ItemTouchHelper.Callback callback =
new SimpleItemTouchHelperCallback (adapter); 3r33450. ItemTouchHelper touchHelper = new ItemTouchHelper (callback); 3r33450. touchHelper.attachToRecyclerView (recyclerView); 3r3407.
3r33450.
After the launch should get something like this:
3r33450.
3r3155. 3r3444.
3r33450. 3r33434. Conclusion 3r3r436.
3r33450.
This is the most simplified implementation of ItemTouchHelper
. However, you may notice that you do not need to use third-party library for implementing standard actions drag & drop and 3r33428. swipe-to-dismiss in 3r33430. RecyclerView . In the next part, we will pay more attention to the appearance of the elements at the time of dragging or swiping. 3r3444.
3r33450. 3r33434. Source code
3r33450.
I created a project on GitHub to show what is described in this series of articles: 3r34040. Android-ItemTouchHelper-Demo
. 3r3442. The first commit 3r3443. mainly refers to this part and a little to the second. 3r3444.
3r33450. 3r33450. 3r33448. ! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e.parentNode.insertBefore (r, e)}; "[object Opera]" == e.opera? a.addEventListener? a.addEventListener ("DOMContentLoaded", d! ): d ()}}} t ("//mediator.mail.ru/script/2820404/"""_mediator") () ();
3r33450.
It may be interesting
weber
Author2-11-2018, 08:28
Publication DateDevelopment / Programming
Category- Comments: 0
- Views: 396
Helpful information. Fortunate me I discovered your web site accidentally,
and I am stunned why this accident did not happen earlier! I bookmarked it. Thanks, I've recently been looking for information about this topic for [hide]a[https://www.pizzahutcouponcode.com/pizza-hut-coupons-code/
] long time and yours is the greatest I've discovered so far. But, what concerning the conclusion? Are you positive about the source?
entegrasyon programları
entegrasyon programları