如何有选择地设置 itemTouchHelper 滑动? [英] how to set itemTouchHelper swipe selectively?
问题描述
所以我在 recyclerview rv 上实现了这个 itemtouchhelper.simple 回调.
so i have implemented this itemtouchhelper.simple callback on a recyclerview rv.
现在在这个 rv 中,我根据内容类型将 2 种布局设置为一行.
now in this rv i have set 2 kinds on layout as a row depending on the content type.
所以当我在 rv 上设置这个 touchhelper 时,它正在这两种布局上实现,即使我不想这样做.我只想将这种滑动应用到这种布局的一种类型.
so as i set this touchhelper on the rv it is being implemented on both of these layouts even though i did'nt want to do that.i only want to apply that swipe to only one type of this layout.
ItemTouchHelper.SimpleCallback ith = new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
if(viewHolder.itemView.findViewById(R.id.messageholder_from) != null)
{
Log.d("texts", "onSwiped: "+viewHolder.itemView.findViewById(R.id.messageholder_from).findViewById(R.id.crfrom));
}
adapter.notifyDataSetChanged();
}
};
new ItemTouchHelper(ith).attachToRecyclerView(rv);
如您所见,我只想在包含此 messageholder_from 子项的行上实现滑动,否则我不想实现滑动.
as you can see this code i only want to implement the swipe on the row which has this messageholder_from child in it otherwise i don't want to implement the swipe.
有什么方法可以删除另一个子 messageholder_to 上的滑动动画和侦听器.
is there any way to remove the swipe animation and the listener on this other child messageholder_to.
我的应用根据消息 ID 显示 to_layout 或 from_layout.
my app shows either a to_layout or a from_layout depending on he message id.
感谢您的任何帮助.
推荐答案
在您的 ItemTouchHelper.SimpleCallback
中,重写 getSwipeDirs()
方法并为任何行返回 0您想禁用滑动.
Inside your ItemTouchHelper.SimpleCallback
, override the getSwipeDirs()
method and return 0 for any row that you want to disable swiping on.
@Override
public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
if (viewHolder.itemView.findViewById(R.id.messageholder_from) == null) {
return 0;
}
return super.getSwipeDirs(recyclerView, viewHolder);
}
根据您的应用的确切设置方式,可能有更好的方法来检测 viewHolder
是您想要禁止滑动的那种.例如,也许你可以
Depending on exactly how your app is set up, there might be a better way to detect that viewHolder
is the kind you want to disallow swiping on. For example, maybe you could have
if (viewHolder instanceof WrongKindOfViewHolder)
或
if (viewHolder.isNotSwipeable)
这篇关于如何有选择地设置 itemTouchHelper 滑动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!