我尝试在适配器中使用 startSwipe,它似乎不起作用。奇怪的是,当我尝试用手指手动滑动它时,它起作用了。只是当我单击支架内的按钮时 startSwipe 不起作用。这方面需要帮助~
这就是我调用 startSwipe 的地方(在 onCreateViewHolder 方法中)。
final ViewHolder holder = new ViewHolder(rootView);
holder.rightItemLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mItemTouchHelper.startSwipe(holder);
}
});
这就是我设置 ItemTouchHelper 的方式:
mItemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
}
});
ItemTouchHelper 如何附加到recyclerView:
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
CategoryAdapter adapter = new CategoryAdapter(this, items);
recyclerView.setAdapter(adapter);
adapter.getItemTouchHelper().attachToRecyclerView(recyclerView);