我已经解决这个问题很多天了,看不到尽头,所以任何帮助都将不胜感激!这就是问题所在:
我有一个房间数据库和一个回收器视图,我在其中显示了该数据库的一部分。一般来说,一切都很好。但是,如果我尝试拖动(从而切换顺序(具有不同视图类型的两个项目,适配器会将该项目放回原来的位置。
这个先前的堆栈溢出问题完美地显示了问题:Android RecyclerView:在多个ViewType 上拖放
代码:(在片段java中(
// Configuring RecycleView
RecyclerView recyclerView = root.findViewById(R.id.recyclerview_viewpager_schedule);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setHasFixedSize(true);
// configuring Adapter
daysAdapter = new DaysAdapter();
prevfrom = -1;
flagish = -1;
//daysAdapter.setHasStableIds(true);
recyclerView.setAdapter(daysAdapter);
daysViewModel.getNotesInDay(args.getInt(ARG_OBJECT)).observe(getViewLifecycleOwner(), new Observer<List<DayActivity>>() {
@Override
public void onChanged(List<DayActivity> dayActivities) {
//I use a flag in order for the list not to update onMove because that makes a weird looking effect. It updates later on and it works just fine!
if (flagish != 5) daysAdapter.submitList(dayActivities);
flagish = 1;
}
});
ItemTouchHelper mIth = new ItemTouchHelper(
new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0) {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
final int fromPos = viewHolder.getAdapterPosition();
final int toPos = target.getAdapterPosition();
Log.d("onMove", "From:" + fromPos);
Log.d("onMove", "to:" + toPos);
Log.d("STOP_HERE_please_1", fromPos + " " + toPos);
Bundle arguments = getArguments();
Log.d("STOP_HERE_3", String.valueOf(arguments.getInt(ARG_OBJECT)));
if (fromPos==toPos) return false;
//prevFrom is the previous fromPos in case the item hansn't been dropped yet
if (prevfrom == -1) prevfrom = fromPos;
if (prevfrom == toPos) return false;
getView().clearFocus();
// Change position
DayActivity lItem;
newList = new ArrayList<>();
int lIncrement = (fromPos<toPos) ? -1 : +1; // true if scroll down
int lMax = Integer.max(prevfrom, toPos);
int lMin = Integer.min(prevfrom, toPos);
List<DayActivity>oldList =daysAdapter.getOrdersBetween(lMin, lMax);
//This function (getOrdersBetween) gets the DayActivities whose positions are between those two values
for (int i=lMin; i<= lMax; i++) {
lItem = oldList.get(i);
if (i == fromPos) {
lItem.setOrder_Day(toPos);
} else
lItem.setOrder_Day(lItem.getOrder_Day() + lIncrement);
//The program uses the order to order the items in the recycler view
newList.add(lItem);
}
daysAdapter.notifyItemMoved(fromPos, toPos);
return true;
}
@Override
public boolean isLongPressDragEnabled() {
return true;
}
@Override
public boolean isItemViewSwipeEnabled() {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
}
@Override
public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
super.onSelectedChanged(viewHolder, actionState);
if (actionState == ACTION_STATE_DRAG)
viewHolder.itemView.setAlpha(0.5f);
}
@Override
public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setAlpha(1.0f);
flagish = 5;
if(newList != null) daysViewModel.updateList(newList);
prevfrom = -1;
}
我相信问题是在适配器中解决的。notifyitemmoved,但我还没能解决它!有些人在其他帖子中建议我制作sethasStableIds((,但这个函数只会导致notifyItemMoved具有类似的效果,即使这两个项目的视图类型相似。
我认为没有必要用适配器中的代码重载这篇文章,但如果有人需要,请告诉我。谢谢
NotifyItemMoved应适用于不同的ViewTypes。
我想您正在使用AsyncDiffer?检查您是否覆盖适配器getItemViewType(position)
功能正确,也许您正在从尚未同步的AsyncDifferens当前列表中获取位置?