RecyclerView 的拖拽和长按变化Item的位置,设置左右滑动删除数据和滑动改变背景等

匿名 (未验证) 提交于 2019-12-03 00:27:02

项目地址:https://github.com/ChloeDimen/RecyclerviewDemo点击打开链接

1:想要实现RecyclerView 的拖拽和长按变化Item的位置,设置左右滑动删除数据和滑动改变背景的关键字:ItemTouchHelper

关键代码:

MyItemTouchHelperCallBack callback = new MyItemTouchHelperCallBack(adapter);  itemTouchHelper = new ItemTouchHelper(callback); itemTouchHelper.attachToRecyclerView(rl_data);

2:主要是实现MyItemTouchHelperCallback继承ItemTouchHelper.callback

public class MyItemTouchHelperCallBack extends ItemTouchHelper.Callback {     private ItemTouchMoveListener mItemTouchMoveListener;      public MyItemTouchHelperCallBack(ItemTouchMoveListener itemTouchMoveListener) {         mItemTouchMoveListener = itemTouchMoveListener;     }      //callback    @Override     public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {         //Up,down.left,right         //ItemTouchHelper.UP,ItemTouchHelper.DOWN,ItemTouchHelper.LEFT,ItemTouchHelper.RIGHT         int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;//        //int swipeFlags = 0;         int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;//        int flags = makeMovementFlags(dragFlags, swipeFlags);           return flags;     }      //    @Override     public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {         if (viewHolder.getItemViewType() != target.getItemViewType()) {//            return false;         }           //adapter.notifyItemMoved(fromPositiontoPosition);item        mItemTouchMoveListener.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());         return false;     }      //    @Override     public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {         //1:2adapter.notifyItemRemoveposition        mItemTouchMoveListener.onItemRemove(viewHolder.getAdapterPosition());      }      //item    @Override     public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {          //,        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {             viewHolder.itemView.setBackgroundColor(viewHolder.itemView.getContext().getResources().getColor(R.color.colorPrimary));          }         super.onSelectedChanged(viewHolder, actionState);      }      //     @Override     public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {          viewHolder.itemView.setBackgroundColor(Color.WHITE);          super.clearView(recyclerView, viewHolder);     }      @Override     public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {         //dx,if (actionState==ItemTouchHelper.ACTION_STATE_SWIPE){     //1-0     float result = Math.abs(dX) / viewHolder.itemView.getWidth();     viewHolder.itemView.setAlpha(result);      //    viewHolder.itemView.setScaleX(result);     viewHolder.itemView.setScaleY(result);  }          super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);     }      @Override     public boolean isLongPressDragEnabled() {         //        return true;     }   } 

3:实现两个接口,分别拖拽效果和删除条目和移动条目的功能

public interface OnStartDragListener {     /**      *      * @param viewHolder      */     void onStartDrag(RecyclerView.ViewHolder viewHolder);  } 
public interface ItemTouchMoveListener {     /**      *      *      * @param fromPosition      * @param toPosition      * @return       */     boolean onItemMove(int fromPosition, int toPosition);      /**      *      * @param position      * @return      */     boolean onItemRemove( int position); } 

4:adapter实现两个接口

public interface ItemTouchMoveListener {     /**      *      *      * @param fromPosition      * @param toPosition      * @return       */     boolean onItemMove(int fromPosition, int toPosition);      /**      *      * @param position      * @return      */     boolean onItemRemove( int position); } 

5:长按改变背景和拖拽改变位置效果

关键代码:

方向判断:

//callback@Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {     //Up,down.left,right     //ItemTouchHelper.UP,ItemTouchHelper.DOWN,ItemTouchHelper.LEFT,ItemTouchHelper.RIGHT     int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;//    //int swipeFlags = 0;     int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;//    int flags = makeMovementFlags(dragFlags, swipeFlags);       return flags; }

长按:

   @Override public boolean isLongPressDragEnabled() {     //    return true; }


改变位置

@Override  public boolean onItemMove(int fromPosition, int toPosition) {      //1:  2      Collections.swap(number,fromPosition,toPosition);      notifyItemMoved(fromPosition,toPosition);      return false;  } 


6:按下图片拖拽

关键代码:监听按下的状态

holder.iv_data.setOnTouchListener(new View.OnTouchListener() {     @Override     public boolean onTouch(View v, MotionEvent event) {         if (event.getAction() == MotionEvent.ACTION_DOWN) {             //            mListener.onStartDrag(holder);         }          return false;     } }); 

实现拖拽功能:

@Override public void onStartDrag(RecyclerView.ViewHolder viewHolder) {     itemTouchHelper.startDrag(viewHolder); } 


7:左右滑动删除条目和透明度,旋转。。效果

关键代码:

//callback@Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {     //Up,down.left,right     //ItemTouchHelper.UP,ItemTouchHelper.DOWN,ItemTouchHelper.LEFT,ItemTouchHelper.RIGHT     int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;//    //int swipeFlags = 0;     int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;//    int flags = makeMovementFlags(dragFlags, swipeFlags);       return flags; }
//@Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {     //1:2adapter.notifyItemRemoveposition    mItemTouchMoveListener.onItemRemove(viewHolder.getAdapterPosition());  }

 @Override public boolean onItemRemove(int position) {     //1:  2    number.remove(position);     notifyItemRemoved(position);     return false; }
 @Override     public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {         //dx,if (actionState==ItemTouchHelper.ACTION_STATE_SWIPE){     //1-0     float result = Math.abs(dX) / viewHolder.itemView.getWidth();     viewHolder.itemView.setAlpha(result);      //    viewHolder.itemView.setScaleX(result);     viewHolder.itemView.setScaleY(result);  }


8:item选中滑动效果(背景改变。。。。)

关键代码:

//item@Override public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {      //,    if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {         viewHolder.itemView.setBackgroundColor(viewHolder.itemView.getContext().getResources().getColor(R.color.colorPrimary));      }     super.onSelectedChanged(viewHolder, actionState);  }  // @Override public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {      viewHolder.itemView.setBackgroundColor(Color.WHITE);      super.clearView(recyclerView, viewHolder); }




项目的文字说明很清晰了。

未完待续。。。。.















易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!