我正在使用tutorial为我的RecyclerView构建一个itemTouchListener . Recyclerview充满了比屏幕更多的物品(超过10个),因此回收利用起了作用 . itemtouchHelper可以处理上下和左右移动 . 经过2天的挣扎(将setStableIds设置为true,导致上下移动时视图视图闪烁),我终于得到了更好的行为 . 我的关键功能代码:
@Override
public int getItemCount() {
return questionlist.size();
}
@Override
public void onViewMoved(int oldPosition, int newPosition) {
targetqueobj = questionlist.get(oldPosition);
this.fromPosition = oldPosition;
this.toPosition = newPosition;
questionlist.remove(targetqueobj);
questionlist.add(newPosition, targetqueobj);
// targetqueobj.setinturn(toPosition+1);
}
@Override
public void onViewSwiped(final RecyclerView.ViewHolder thisviewholder,final int position, int direction) {
targetqueobj = questionlist.get(position);
if (direction == ItemTouchHelper.LEFT){
// saveqset();
Intent intent = new Intent(context, QuestionEditActivity.class);
intent.putExtra("com.logictop.mqapp.QuestionObjParcelable",targetqueobj);
context.startActivity(intent);
}
if (direction == ItemTouchHelper.RIGHT) {
// DIALOG
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.remove_question);
builder.setNegativeButton(R.string.No, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// thisviewholder.itemView.setAlpha(1);
notifyDataSetChanged();
}
});
builder.setPositiveButton(R.string.Yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
questionlist.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, getItemCount()-position);
}
});
Dialog dialog = builder.create();
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
}
if (direction == ItemTouchHelper.RIGHT) {
// DIALOG
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.remove_question);
builder.setNegativeButton(R.string.No, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
thisviewholder.itemView.setAlpha(1);
notifyDataSetChanged();
}
});
builder.setPositiveButton(R.string.Yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
questionlist.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, getItemCount()-position);
}
});
Dialog dialog = builder.create();
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
}
问题是这个 . 虽然回收者视图运行顺畅,物品的位置变化非常好,但当物品被轻扫时,有时候另一个物品看起来也会在向下滚动回收物品时被移除(所以看起来被移除的另一个物品会填充相同的“屏幕”位置在滚动后的recyclerview中) . 它不会每次都发生,并且主要发生在某些特定位置的视图被刷掉时 . 如果我向上或向下移动邻居视图,可以修补“差距” .
我已经尝试了我在这里找到的每个解决方案(notifyDataChanged,notifyItemRangeChanged(带有每个参数compination) . 但没有什么能让我得到一个稳定的行为 . 在很多stackoverflow搜索之后我决定按照 holder.setIsRecyclable(false)
的建议,尽管我没有't want to do that (as it eliminates the point of having a recyclerview after all). But in that case, other problems appear. You'看到在大多数视图被刷掉的情况下,它们较低的视图不会离开屏幕,即使它们显然有"left the adapter"(无法将它们擦掉) . 最后,最后的视图仍然卡住了 .
我已经在一个全新的项目中测试了两种方式,没有任何外部方法 . 我试过将 notifyDataChanged()
放在clearView的覆盖函数中 . 似乎没有任何东西可以提供坚如磐石的稳定回收视图,而这在某些方面本身并不会产生差距 .
现在的问题是:是否有一种方法可以使再循环工作具有可行的回收行为,或者我应该接受这种情况?非常感谢您的关注!
UPDATE 1---------------------------- 正如我被告知可能存在这个问题 thisviewholder.itemView.setAlpha(1);
我将它与相应的重写onChildDraw(整个)一起评论,用于在视图被刷出时淡出视图 . 所以现在什么都看不见了 . 问题仍然存在 .
UPDATE 2---------------------------- 我已经强制执行了stableIds(已经做过一次而且没有用)
@Override
public long getItemId(int position){
return questionlist.get(position).getquestionid();
}
和适配器构造函数
public QSetEditAdapter(ArrayList<QuestionObj_prcl> questionlist, Context context) {
this.context = context;
this.questionlist = questionlist;
setHasStableIds(true);
}
问题仍然存在 .
UPDATE 3---------------------------- 我最终使用了different tutorial,现在一切都按预期工作了 . 非常感谢!
1 回答
以这种方式更改adapter:
将hasStableIds()设为true .
实施getItemId(int)并为您的商品返回唯一ID .