Recyclerview implements drag sorting and slide deletion functions

For the drag sorting of recyclerview, you need to use the itemtouchhelper class. The itemtouchhelper class is a usage class provided by Google that supports the sliding and dragging of recyclerview. This class is used to realize the sliding deletion and drag sorting functions of recyclerview. First, let's briefly introduce the callback class, an internal abstract class of itemtouchhelper.

This class is a static abstract class in the itemtouchhelper class. It is mainly used to connect the itemtouchhelper with its own applications, so that developers can control the specific behavior of each view through the viewholder and receive user event callbacks. There are three abstract methods in this class: getmovementflags, onmove and onshipped. It is also often used in development.

This method returns a flag indicating three statuses of an item: idle, sliding and dragging. Different sliding and dragging directions are set according to different layout managers of recyclerview. Generally, the makemovementflags (int dragflags, int swipeflags) method is used to return. Dragflags indicates the direction of dragging and swipeflags indicates the direction of sliding.

public@H_502_17@ abstract@H_502_17@ int@H_502_17@ getMovementFlags@H_502_17@(RecyclerView recyclerView,ViewHolder viewHolder);

When the itemtouchhelper drags an item, the method will be called back, and the item will be moved from the old position to the new position. If it does not drag, the method will never be called. Returning true indicates that the item has been moved to the new position.

public@H_502_17@ abstract@H_502_17@ boolean@H_502_17@ onMove@H_502_17@(RecyclerView recyclerView,ViewHolder viewHolder,ViewHolder target);

Called when the item slides. If it does not slide, the method will not be called. You can make corresponding judgment and perform some operations through direction.

public@H_502_17@ abstract@H_502_17@ void@H_502_17@ onSwiped@H_502_17@(ViewHolder viewHolder,int@H_502_17@ direction);

In addition, the frequently used methods include onselectedchanged, Clearview and so on.

This method is called when an item changes from a static state to a sliding or dragging state. You can judge in which state the item performs some operations through actionstate. When overriding this method, you must call the method of its parent class.

public@H_502_17@ void@H_502_17@ onSelectedChanged@H_502_17@(ViewHolder viewHolder,int@H_502_17@ actionState) {
        if@H_502_17@ (viewHolder != null@H_502_17@) { sUICallback.onSelected(viewHolder.itemView); } }

When the interaction with the user ends or the related animation is completed, the method is called.

public@H_502_17@ void@H_502_17@ clearView@H_502_17@(RecyclerView recyclerView,ViewHolder viewHolder) {
        sUICallback.clearView(viewHolder.itemView);
}

The drag sorting of recyclerview needs to be implemented with the help of an android.support.v7.widget.helper.itemtouchhelper class. The focus of drag sorting is the onmove (int fromposition, int toposition) method in the interface. The specific implementation in the gridadapter is as follows:

@Override@H_502_17@
public@H_502_17@ void@H_502_17@ onMove@H_502_17@(int@H_502_17@ fromPosition,int@H_502_17@ toPosition) { if@H_502_17@ (fromPosition < toPosition) { for@H_502_17@ (int@H_502_17@ i = fromPosition; i < toPosition; i++) { Collections.swap(list,i,i + 1@H_502_17@); } } else@H_502_17@ { for@H_502_17@ (int@H_502_17@ i = fromPosition; i > toPosition; i--) { Collections.swap(list,i - 1@H_502_17@); } } notifyItemMoved(fromPosition,toPosition); }

The drag sorting of recyclerview needs to be implemented with the help of an android.support.v7.widget.helper.itemtouchhelper class. Sideslip deletion focuses on onshipped (int position) in the interface. The specific implementation in gridadapter is shown below:


    @Override@H_502_17@
    public@H_502_17@ void@H_502_17@ onSwiped@H_502_17@(int@H_502_17@ position) { Log.i("drag"@H_502_17@,"onSwiped"@H_502_17@); list.remove(position); notifyItemRemoved(position); }

/**
 * Created by jzman on 2017/5/17 0015.
 */@H_502_17@
public@H_502_17@ class@H_502_17@ ItemTouchCallBack@H_502_17@ extends@H_502_17@ itemtouchhelper@H_502_17@.Callback@H_502_17@ {@H_502_17@ private@H_502_17@ static@H_502_17@ final@H_502_17@ String TAG = "drag"@H_502_17@; private@H_502_17@ OnItemTouchListener onItemTouchListener; public@H_502_17@ void@H_502_17@ setOnItemTouchListener@H_502_17@(OnItemTouchListener onItemTouchListener) { this@H_502_17@.onItemTouchListener = onItemTouchListener; } /** * 根据 RecyclerView 不同的布局管理器,设置不同的滑动、拖动方向 * 该方法使用 makeMovementFlags(int dragFlags,int swipeFlags) 方法返回 * 参数: dragFlags:拖动的方向 * swipeFlags:滑动的方向 * @param@H_502_17@ recyclerView * @param@H_502_17@ viewHolder * @return@H_502_17@ */@H_502_17@ @Override@H_502_17@ public@H_502_17@ int@H_502_17@ getMovementFlags@H_502_17@(RecyclerView recyclerView,RecyclerView.ViewHolder viewHolder) { Log.i(TAG,"getMovementFlags"@H_502_17@); if@H_502_17@ (recyclerView.getLayoutManager() instanceof@H_502_17@ GridLayoutManager || recyclerView.getLayoutManager() instanceof@H_502_17@ StaggeredGridLayoutManager){ //此处不需要进行滑动操作,可设置为除4和8之外的整数,这里设为0@H_502_17@ //不支持滑动@H_502_17@ return@H_502_17@ makeMovementFlags(itemtouchhelper.UP | itemtouchhelper.DOWN | itemtouchhelper.LEFT | itemtouchhelper.RIGHT,0@H_502_17@ ); }else@H_502_17@ { //如果是linearlayoutmanager则只能向上向下滑动,@H_502_17@ //此处第二个参数设置支持向右滑动@H_502_17@ return@H_502_17@ makeMovementFlags(itemtouchhelper.UP | itemtouchhelper.DOWN,itemtouchhelper.RIGHT ); } } /** * 当 itemtouchhelper 拖动一个Item时该方法将会被回调,Item将从旧的位置移动到新的位置 * 如果不拖动这个方法将从来不会调用,返回true表示已经被移动到新的位置 * @param@H_502_17@ recyclerView * @param@H_502_17@ viewHolder * @param@H_502_17@ target * @return@H_502_17@ */@H_502_17@ @Override@H_502_17@ public@H_502_17@ boolean@H_502_17@ onMove@H_502_17@(RecyclerView recyclerView,RecyclerView.ViewHolder viewHolder,RecyclerView.ViewHolder target) { Log.i(TAG,"onMove"@H_502_17@); int@H_502_17@ fromPosition = viewHolder.getAdapterPosition(); int@H_502_17@ toPosition = target.getAdapterPosition(); onItemTouchListener.onMove(fromPosition,toPosition); return@H_502_17@ true@H_502_17@; } /** * 当Item被滑动的时候被调用 * 如果你不滑动这个方法将不会被调用 * @param@H_502_17@ viewHolder * @param@H_502_17@ direction */@H_502_17@ @Override@H_502_17@ public@H_502_17@ void@H_502_17@ onSwiped@H_502_17@(RecyclerView.ViewHolder viewHolder,int@H_502_17@ direction) { Log.i(TAG,"onSwiped"@H_502_17@); //此处是侧滑删除的主要代码@H_502_17@ int@H_502_17@ position = viewHolder.getAdapterPosition(); onItemTouchListener.onSwiped(position); } /** * 当Item被滑动、拖动的时候被调用 * @param@H_502_17@ viewHolder * @param@H_502_17@ actionState */@H_502_17@ @Override@H_502_17@ public@H_502_17@ void@H_502_17@ onSelectedChanged@H_502_17@(RecyclerView.ViewHolder viewHolder,int@H_502_17@ actionState) { Log.i(TAG,"onSelectedChanged"@H_502_17@); //...@H_502_17@ super@H_502_17@.onSelectedChanged(viewHolder,actionState); } /** * 当与用户交互结束或相关动画完成之后被调用 * @param@H_502_17@ recyclerView * @param@H_502_17@ viewHolder */@H_502_17@ @Override@H_502_17@ public@H_502_17@ void@H_502_17@ clearView@H_502_17@(RecyclerView recyclerView,"clearView"@H_502_17@); //...@H_502_17@ super@H_502_17@.clearView(recyclerView,viewHolder); } /** * 移动交换数据的更新监听 */@H_502_17@ public@H_502_17@ interface@H_502_17@ OnItemTouchListener@H_502_17@ {@H_502_17@ //拖动Item时调用@H_502_17@ void@H_502_17@ onMove(int@H_502_17@ fromPosition,int@H_502_17@ toPosition); //滑动Item时调用@H_502_17@ void@H_502_17@ onSwiped(int@H_502_17@ position); } } 

/**
 * Created by jzman on 2017/05/17 0009.
 * RecycleView的Adapter
 */@H_502_17@
public class@H_502_17@ GridAdapter@H_502_17@ extends@H_502_17@ RecyclerView@H_502_17@.Adapter@H_502_17@<GridAdapter@H_502_17@.DataViewHolder@H_502_17@> implements@H_502_17@ View@H_502_17@.OnClickListener@H_502_17@,ItemTouchCallBack@H_502_17@.OnItemTouchListener@H_502_17@ {@H_502_17@ private@H_502_17@ Context context; private@H_502_17@ List<SimpleTitleGrid> list; public GridAdapter(Context context,List<SimpleTitleGrid> list) { this@H_502_17@.context = context; this@H_502_17@.list = list; } /** * 创建ViewHolder * @param@H_502_17@ parent * @param@H_502_17@ viewType * @return@H_502_17@ */@H_502_17@ @Override@H_502_17@ public DataViewHolder onCreateViewHolder(ViewGroup parent,int viewType) { //加载item布局文件(每一个)@H_502_17@ View view = LayoutInflater.from(context).inflate(R.layout.item,null@H_502_17@); //为View设置单击事件@H_502_17@ view.setOnClickListener(this@H_502_17@); DataViewHolder viewHolder = new@H_502_17@ DataViewHolder(view); //使用代码设置宽高(xml布局设置无效时)@H_502_17@ view.setLayoutParams(new@H_502_17@ RecyclerView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)); return@H_502_17@ viewHolder; } /** * 绑定数据 * @param@H_502_17@ holder * @param@H_502_17@ position */@H_502_17@ @Override@H_502_17@ public void onBindViewHolder(DataViewHolder holder,int position) { //设置每一个Item的高度@H_502_17@ holder.textView.setText(list.get(position).getTitle()); } /** * 选项总数 * @return@H_502_17@ */@H_502_17@ @Override@H_502_17@ public int getItemCount() { return@H_502_17@ list.size(); } /** * 单击事件 * @param@H_502_17@ v */@H_502_17@ @Override@H_502_17@ public void onClick(View v) { if@H_502_17@(onItemClickListener!=null@H_502_17@){ int position = recyclerView.getChildAdapterPosition(v); //程序执行到此,会执行该方法的具体实现@H_502_17@ onItemClickListener.onItemClick(recyclerView,v,position,list.get(position)); } } @Override@H_502_17@ public void onMove(int fromPosition,int toPosition) { if@H_502_17@ (fromPosition < toPosition) { for@H_502_17@ (int i = fromPosition; i < toPosition; i++) { Collections.swap(list,i + 1@H_502_17@); } } else@H_502_17@ { for@H_502_17@ (int i = fromPosition; i > toPosition; i--) { Collections.swap(list,i - 1@H_502_17@); } } notifyItemMoved(fromPosition,toPosition); } @Override@H_502_17@ public void onSwiped(int position) { Log.i("drag"@H_502_17@,"onSwiped"@H_502_17@); list.remove(position); notifyItemRemoved(position); } /** * RecycleView中针对ViewHolder来实现 */@H_502_17@ public static class@H_502_17@ DataViewHolder@H_502_17@ extends@H_502_17@ RecyclerView@H_502_17@.ViewHolder@H_502_17@ {@H_502_17@ TextView textView; public DataViewHolder(View itemView) { super@H_502_17@(itemView); textView = (TextView) itemView.findViewById(R.id.tv_grid); } } private@H_502_17@ RecyclerView recyclerView; private@H_502_17@ OnItemClickListener onItemClickListener; public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this@H_502_17@.onItemClickListener = onItemClickListener; } /** * 设计recycleView选项单击事件的回调接口(给外面使用) * 参考ListView选项单击事件方法 */@H_502_17@ public interface OnItemClickListener{ //参数(父组件,点击的View,位置,这里可能是某个对象的id或对象/这里不需要)@H_502_17@ void onItemClick(RecyclerView recyclerView,View view,int position,SimpleTitleGrid obj); } /** * 将RecycleView附加到Adapter上 */@H_502_17@ @Override@H_502_17@ public void onAttachedToRecyclerView(RecyclerView recyclerView) { super@H_502_17@.onAttachedToRecyclerView(recyclerView); this@H_502_17@.recyclerView= recyclerView; } /** * 将RecycleView从Adapter解除 */@H_502_17@ @Override@H_502_17@ public void onDetachedFromRecyclerView(RecyclerView recyclerView) { super@H_502_17@.onDetachedFromRecyclerView(recyclerView); this@H_502_17@.recyclerView = null@H_502_17@; } }

/**
 * Created by jzman on 2017/05/17 0029.
 * RecycleView的Adapter
 */@H_502_17@
public@H_502_17@ class@H_502_17@ MainActivity@H_502_17@ extends@H_502_17@ AppCompatActivity@H_502_17@ implements@H_502_17@ GridAdapter@H_502_17@.OnItemClickListener@H_502_17@{@H_502_17@ private@H_502_17@ RecyclerView rv_user; private@H_502_17@ GridAdapter adapter; @Override@H_502_17@ protected@H_502_17@ void@H_502_17@ onCreate@H_502_17@(Bundle savedInstanceState) { super@H_502_17@.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private@H_502_17@ void@H_502_17@ initView@H_502_17@() { rv_user = (RecyclerView) findViewById(R.id.rv_user); adapter = new@H_502_17@ GridAdapter(this@H_502_17@,DataUtils.getUserGrids()); ItemTouchCallBack touchCallBack = new@H_502_17@ ItemTouchCallBack(); touchCallBack.setOnItemTouchListener(adapter); itemtouchhelper itemtouchhelper = new@H_502_17@ itemtouchhelper(touchCallBack); rv_user.setLayoutManager(new@H_502_17@ GridLayoutManager(this@H_502_17@,3@H_502_17@)); // rv_user.setLayoutManager(new linearlayoutmanager(this));@H_502_17@ rv_user.setAdapter(adapter); itemtouchhelper.attachToRecyclerView(rv_user); adapter.setOnItemClickListener(this@H_502_17@); } @Override@H_502_17@ public@H_502_17@ void@H_502_17@ onItemClick@H_502_17@(RecyclerView recyclerView,int@H_502_17@ position,SimpleTitleGrid obj) { Toast.makeText(MainActivity.this@H_502_17@,obj.getTitle(),Toast.LENGTH_SHORT).show(); } }

WeChat official account can be concerned: jzman-blog gets updated articles and exchanges learning together.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>