91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

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
乐安县| 咸丰县| 卓资县| 榆中县| 镇平县| 莱芜市| 游戏| 峨眉山市| 青州市| 北宁市| 陇西县| 邵东县| 沙洋县| 通州区| 奉贤区| 盐源县| 大城县| 明水县| 武陟县| 万宁市| 高清| 化德县| 栖霞市| 遂川县| 巫山县| 思南县| 西乡县| 凤翔县| 融水| 西城区| 陇西县| 天门市| 阿拉善右旗| 怀仁县| 凤山市| 岳池县| 红安县| 佛教| 荔浦县| 呼伦贝尔市| 荃湾区|