短視訊系統,長按側滑實現刪除的按鈕

zhibo系統開發發表於2021-10-18

短視訊系統,長按側滑實現刪除的按鈕實現的相關程式碼

1,導包

implementation ‘com.android.support:recyclerview-v7:27.1.1’

2,Xml檔案

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_linear_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="20dp"/>
</android.support.constraint.ConstraintLayout>

3,Activity

public class RvItemTouchActivity extends AppCompatActivity {
    private android.support.v7.widget.RecyclerView rvlinearlist;
    private ArrayList<String> mData;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_linear);
        this.rvlinearlist = (RecyclerView) findViewById(R.id.rv_linear_list);
        initData();
        initAdapter();
    }
    private void initAdapter() {
        //線性佈局拖拽
        rvlinearlist.setLayoutManager(new LinearLayoutManager(this));
        //自定義類實現拖拽,側滑刪除
        RvItemTouchAdapter adapter = new RvItemTouchAdapter(this, mData);
        new ItemTouchHelper(new MyItemTouchHandler(adapter)).attachToRecyclerView(rvlinearlist);
        rvlinearlist.setAdapter(adapter);
       /* //庫實現
        RvItemTouchAdapter2 adapter2 = new RvItemTouchAdapter2(R.layout.item_rv_linear2, mData);
        rvlinearlist.setAdapter(adapter2);
        ItemDragAndSwipeCallback itemDragAndSwipeCallback = new ItemDragAndSwipeCallback(adapter2);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragAndSwipeCallback);
        itemTouchHelper.attachToRecyclerView(rvlinearlist);
// 開啟拖拽
        adapter2.enableDragItem(itemTouchHelper);
        adapter2.setOnItemDragListener(onItemDragListener);
// 開啟滑動刪除
        adapter2.enableSwipeItem();
        adapter2.setOnItemSwipeListener(onItemSwipeListener);
*/
    }
    OnItemDragListener onItemDragListener = new OnItemDragListener() {
        @Override
        public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos){}
        @Override
        public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {
            //移動位置互換
            Collections.swap(mData,from,to);
        }
        @Override
        public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {}
    };
    OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() {
        @Override
        public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.e("MMM", "onItemSwipeStart: "+pos );
        }
        @Override
        public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.e("MMM", "clearView: "+pos);
        }
        @Override
        public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.e("MMM", "onItemSwiped: "+pos);
        }
        @Override
        public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {
            //正在滑動監聽
        }
    };
    private void initData() {
        mData = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            mData.add("頻道"+i);
        }
    }
}

4,Adapter

public class RvItemTouchAdapter extends MyItemTouchHandler.ItemTouchAdapterImpl {
    private final LayoutInflater mLayoutInflater;
    private Context mContext;
    private List<String>mData;
    public RvItemTouchAdapter(Context context, List<String> data) {
        mContext = context;
        mData = data;
        mLayoutInflater = LayoutInflater.from(context);
    }
    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        //移動時交換位置
        Collections.swap(mData,fromPosition,toPosition);
    }
    @Override
    public void onItemRemove(int position) {
        //移除監聽
        mData.remove(position);
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.item_rv_linear2, parent, false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ((ViewHolder) holder).tvTv.setText(mData.get(position));
        }
    }
    @Override
    public int getItemCount() {
        return mData.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTv;
        public ViewHolder(View itemView) {
            super(itemView);
            tvTv=itemView.findViewById(R.id.tv_item_rv_linear_text);
        }
    }
}

5,MyItemTouchHandler

public class MyItemTouchHandler extends ItemTouchHelper.Callback {
      ItemTouchAdapterImpl adapter;    
      public MyItemTouchHandler(@NonNull ItemTouchAdapterImpl adapter) {
          this.adapter = adapter;    
      }    
      /**     
       * 設定 允許拖拽和滑動刪除的方向     
       */    
       @Override    
       public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
          // 指定可 拖拽方向 和 滑動消失的方向        
          int dragFlags,swipeFlags;        
          RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();        
          if (manager instanceof GridLayoutManager || manager instanceof StaggeredGridLayoutManager) {
               // 上下左右都可以拖動            
               dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;       
           } else {            
              // 可以上下拖動            
              dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;        
           }        
           // 可以左右方向滑動消失        
           swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;        
           // 如果某個值傳 0 , 表示不支援該功能        
           return makeMovementFlags(dragFlags, swipeFlags);    
        }    
        /**     
         * 拖拽後回撥,一般通過介面暴露給adapter, 讓adapter去處理資料的交換     
         */    
         @Override    
         public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {        
             // 相同 viewType 之間才能拖動交換        
             if (viewHolder.getItemViewType() == target.getItemViewType()) {            
                 int fromPosition = viewHolder.getAdapterPosition();
                 int toPosition = target.getAdapterPosition();
                 if (fromPosition < toPosition) {    
                    //途中所有的item位置都要移動    
                    for (int i = fromPosition; i < toPosition; i++) {        
                        adapter.onItemMove(i, i + 1);    
                    }
                  } else {    
                    for (int i = fromPosition; i > toPosition; i--) {        
                        adapter.onItemMove(i, i - 1);    
                    }
                  }
                  adapter.notifyItemMoved(fromPosition, toPosition);
                  return true;    
             }        
             return false;    
          }    
          /**     
           * 滑動刪除後回撥,一般通過介面暴露給adapter, 讓adapter去刪除該條資料     
           */    
           @Override    
           public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {        
              // 刪除資料        
              adapter.onItemRemove(viewHolder.getAdapterPosition());        
              // adapter 重新整理        
              adapter.notifyItemRemoved(viewHolder.getAdapterPosition());    
           }    
           @Override    
           public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
               super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);       
               if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {            
                    //滑動時改變Item的透明度           
                    final float alpha = 1 - Math.abs(dX) / (float)viewHolder.itemView.getWidth();            
                    viewHolder.itemView.setAlpha(alpha);            
                    viewHolder.itemView.setTranslationX(dX);       
               }    
           }   
           /**     
            * item被選中(長按)     
            * 這裡改變了 item的背景色, 也可以通過介面暴露, 讓adapter去處理邏輯     
            */    
            @Override    
            public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {        
                if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {            
                   // 拖拽狀態            
                   viewHolder.itemView.setBackgroundColor(Color.BLUE);
                }else if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {            
                   // 滑動刪除狀態            
                   viewHolder.itemView.setBackgroundColor(Color.RED);       
                }       
                super.onSelectedChanged(viewHolder, actionState);    
            }   
            /**     
             * item取消選中(取消長按)     
             * 這裡改變了 item的背景色, 也可以通過介面暴露, 讓adapter去處理邏輯     
             */    
             @Override    
             public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {        
                viewHolder.itemView.setBackgroundColor(Color.TRANSPARENT);        
                super.clearView(recyclerView, viewHolder);    
             }    
             /**     
              * 是否支援長按開始拖拽,預設開啟     * 可以不開啟,然後在長按 item 的時候,手動 呼叫 mItemTouchHelper.startDrag(myHolder) 開啟,更加靈活     
              */    
              @Override    
              public boolean isLongPressDragEnabled() {        
                 return adapter.autoOpenDrag();    
              }    
              /**     
               * 是否支援滑動刪除,預設開啟     * 可以不開啟,然後在長按 item 的時候,手動 呼叫 mItemTouchHelper.startSwipe(myHolder) 開啟,更加靈活     
               */    
               @Override    
               public boolean isItemViewSwipeEnabled() {        
                    return adapter.autoOpenSwipe();    
               }    
               // 建議讓 adapter 實現該介面    
               public static abstract class ItemTouchAdapterImpl extends RecyclerView.Adapter{        
                    public abstract void onItemMove(int fromPosition, int toPosition);       
                    public abstract void onItemRemove(int position);       
                    // 是否自動開啟拖拽        
                    protected boolean autoOpenDrag(){            
                         return true;       
                    }        
                    // 是否自動開啟滑動刪除        
                    protected boolean autoOpenSwipe(){            
                         return true;        
                    }    
               }
    }


以上就是短視訊系統,長按側滑實現刪除的按鈕實現的相關程式碼, 更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2837882/,如需轉載,請註明出處,否則將追究法律責任。

相關文章