短視訊系統,長按側滑實現刪除的按鈕
短視訊系統,長按側滑實現刪除的按鈕實現的相關程式碼
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 短視訊app製作,實現訊息列表的左滑刪除或長按刪除APP
- php短視訊原始碼,向左滑動顯示刪除按鈕PHP原始碼
- 短視訊app開發,左滑刪除或長按彈出刪除選擇框APP
- 短視訊軟體開發,按鈕側滑顯示各個選項
- 直播商城平臺,購物車長按右滑出現刪除按鈕
- php短視訊原始碼,按鈕的圓角圖示實現PHP原始碼
- 短視訊直播系統,動態內的圖片實現長按儲存
- 短視訊系統原始碼,實現按鈕開啟關閉,顏色可自定義原始碼
- 短視訊app原始碼,提示以按鈕彈窗的形式實現APP原始碼
- 短視訊平臺原始碼,長按視訊、對話方塊彈出操作可選按鈕原始碼
- Simple WPF: WPF 實現按鈕的長按,短按功能
- 小程式實現長按刪除圖片
- 短視訊系統原始碼,動態內容實現長按複製貼上原始碼
- php短視訊系統,點選按鈕開啟wifi或者關閉wifiPHPWiFi
- CustomPainter——微信拍視訊按鈕效果實現AI
- jQuery點選按鈕刪除div元素jQuery
- kindeditor 圖片管理增加刪除操作按鈕
- 點選刪除按鈕彈出是否刪除提示框
- 短視訊開發app,自定義帶進度條的視訊播放按鈕APP
- WPF中的ListBox怎麼新增刪除按鈕並刪除所在行
- 短視訊app開發,長按將視訊儲存到相簿APP
- 動態建立具有刪除行按鈕的table表格
- 刪除按鈕點選後的虛線輪廓
- RecyclerView 梳理:點選&長按事件、分割線、拖曳排序、滑動刪除View事件排序
- 日常總結 --- 視訊播放按鈕
- onethink1.1.141101資料模型“刪除”按鈕失效。模型
- idea除錯按鈕的作用Idea除錯
- 短視訊平臺開發,依靠DrawerLayout實現側滑選單效果
- 短視訊開發,點選按鈕Button,更換背景顏色
- JavaFx 實現按鈕防抖Java
- android短視訊開發,點選分享按鈕生成分享連結Android
- QT經驗(一)——按鈕長按事件分析QT事件
- 搭建自己的直播平臺,修改cell刪除按鈕字型大小
- 原生js實現一個側滑刪除取消元件(item slide)JS元件IDE
- JavaScript新增一個文字框並帶有刪除按鈕JavaScript
- Dcat-admin新增頂部右側按鈕
- Fiori Elements List Report table 裡的普通按鈕,Global 按鈕 和 Determining 按鈕
- Mac實用技巧:如何設定長按delete實現連續刪除小技巧!Macdelete