亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

android itemtouchhelper如何實現滑動刪除

小樊
81
2024-11-25 13:29:51
欄目: 編程語言

要實現滑動刪除功能,您需要使用ItemTouchHelper類。以下是實現滑動刪除的步驟:

  1. 首先,確保您的項目已經添加了RecyclerView支持庫的依賴項。在app的build.gradle文件中添加以下依賴:
dependencies {
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
}
  1. 創建一個適配器類,繼承自RecyclerView.Adapter,并實現必要的方法。在這個類中,您需要定義一個接口來處理滑動事件:
public interface OnItemSwipeListener {
    void onItemSwiped(int position);
}
  1. 在適配器類中添加一個OnItemSwipeListener成員變量,并在構造函數中接收一個實例。然后,在onBindViewHolder方法中設置滑動監聽器:
private OnItemSwipeListener mListener;

public MyAdapter(OnItemSwipeListener listener) {
    mListener = listener;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    holder.itemView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mStartX = event.getX();
                mStartY = event.getY();
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                float endX = event.getX();
                float endY = event.getY();
                float deltaX = endX - mStartX;
                float deltaY = endY - mStartY;

                if (Math.abs(deltaX) > Math.abs(deltaY)) {
                    if (deltaX > 0) {
                        if (mListener != null) {
                            mListener.onItemSwiped(position);
                        }
                    } else {
                        // Handle left swipe
                    }
                } else {
                    if (deltaY > 0) {
                        // Handle down swipe
                    } else {
                        // Handle up swipe
                    }
                }
            }
            return false;
        }
    });
}
  1. 在您的Activity或Fragment中,創建一個ItemTouchHelper.Callback子類,并重寫onInterceptTouchEventonMove方法:
private class MyItemTouchHelperCallback extends ItemTouchHelper.Callback {

    private final MyAdapter mAdapter;

    public MyItemTouchHelperCallback(MyAdapter adapter) {
        mAdapter = adapter;
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent e) {
        return false;
    }

    @Override
    public void onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        if (viewHolder instanceof MyViewHolder) {
            mAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
            mAdapter.notifyItemRangeChanged(viewHolder.getAdapterPosition(), mAdapter.getItemCount());
        }
    }
}
  1. 在Activity或Fragment中,初始化ItemTouchHelper并附加到RecyclerView:
MyAdapter mAdapter = new MyAdapter(/* pass your data here */);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mAdapter);

ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new MyItemTouchHelperCallback(mAdapter));
itemTouchHelper.attachToRecyclerView(recyclerView);

現在,當用戶滑動列表項時,將觸發onSwiped方法,從而刪除相應的列表項。您可以根據需要自定義刪除動畫和其他細節。

0
区。| 普兰店市| 岳池县| 太康县| 恩平市| 邹城市| 浪卡子县| 桂阳县| 巍山| 台南县| 肥西县| 玉田县| 大英县| 长海县| 眉山市| 景谷| 桦川县| 汉源县| 张家川| 祁阳县| 金平| 朝阳区| 盱眙县| 邹平县| 鹤山市| 惠来县| 长沙市| 铅山县| 怀远县| 潜山县| 阿图什市| 黄陵县| 汝州市| 镶黄旗| 库车县| 高州市| 呼玛县| 和田县| 鹤岗市| 沾益县| 淮南市|