使用ItemTouchHelper.SimpleCallback禁用在RecyclerView中的位置滑动 [英] Disable Swipe for position in RecyclerView using ItemTouchHelper.SimpleCallback
问题描述
我正在使用recyclerview 22.2.0和帮助程序类ItemTouchHelper.SimpleCallback启用对我的列表的滑动到关闭选项.但是,由于上面有一种标头,因此我需要为适配器的第一个位置禁用滑动行为.由于 RecyclerView.Adapter 没有 isEnabled()方法,因此我尝试通过 isEnabled()和
I am using recyclerview 22.2.0 and the helper class ItemTouchHelper.SimpleCallback to enable swipe-to-dismiss option to my list. But as I have a type of header on it, I need to disable the swipe behavior for the first position of the adapter. As RecyclerView.Adapter doesn't have a isEnabled() method, I tried to disable the view interaction through the methods isEnabled() and isFocusable() in the ViewHolder creation itself, but had no success. I tried to adjust the swipe threshold to a full value, like 0f ot 1f in the SimpleCallback's method getSwipeThreshold(), but no success too.
我的代码的一些片段可以帮助您.
Some fragments of my code to help you to help me.
我的活动:
@Override
protected void onCreate(Bundle bundle) {
//... initialization
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
RecyclerView.ViewHolder target) {
return false;
}
@Override
public float getSwipeThreshold(RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof CartAdapter.MyViewHolder) return 1f;
return super.getSwipeThreshold(viewHolder);
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
}
我有一个通用适配器,具有两种视图类型.在要禁用滑动的 ViewHolder 中,我做了:
And I have a common adapter with two view types. In the ViewHolder that I want to disable swiping, I did:
public static class MyViewHolder extends RecyclerView.ViewHolder {
public ViewGroup mContainer;
public MyViewHolder(View v) {
super(v);
v.setFocusable(false);
v.setEnabled(false);
mContainer = (ViewGroup) v.findViewById(R.id.container);
}
}
推荐答案
稍作播放后,我设法确保 SimpleCallback 具有名为 getSwipeDirs()的方法.由于我为不可滑动位置使用了特定的ViewHolder,因此可以使用 instanceof 来避免滑动.如果不是您这种情况,则可以使用ViewHolder在适配器中的位置来执行此控制.
After playing a bit, I managed that SimpleCallback has a method called getSwipeDirs(). As I have a specific ViewHolder for the not swipable position, I can make use of instanceof to avoid the swipe. If that's not your case, you can perform this control using the position of ViewHolder in the Adapter.
Java
@Override
public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof CartAdapter.MyViewHolder) return 0;
return super.getSwipeDirs(recyclerView, viewHolder);
}
科特林
override fun getSwipeDirs (recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
if (viewHolder is CartAdapter.MyViewHolder) return 0
return super.getSwipeDirs(recyclerView, viewHolder)
}
这篇关于使用ItemTouchHelper.SimpleCallback禁用在RecyclerView中的位置滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!