gpt4 book ai didi

android - 不同 ViewType 之间的 NotifyItemMoved 不起作用

转载 作者:行者123 更新时间:2023-12-05 07:03:03 25 4
gpt4 key购买 nike

我已经解决这个问题很多天了,看不到尽头,所以任何帮助都将不胜感激!这是问题所在:

我有一个房间数据库和一个回收站 View ,我在其中显示了该数据库的一部分。通常一切正常。但是,如果我尝试拖动(从而切换顺序)两个具有不同 View 类型的项目,适配器会将项目放回原来的位置。

之前的堆栈溢出问题很好地展示了问题:Android RecyclerView: drag and drop over multiple ViewType

代码:(在 fragment 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;
}

我相信问题是在 adapter.notifyitemmoved 中路由的,但我一直无法解决它!有些人在其他帖子中建议我使用 sethasStableIds(),但是这个函数只会导致 notifyItemMoved 产生类似的效果,即使这两个项目具有相似的 View 类型。

我认为没有必要用适配器中的代码重载这篇文章,但如果有人需要,请告诉我。谢谢!!!

最佳答案

NotifyItemMoved 应该适用于不同的 ViewType。

我假设您正在使用 AsyncDiffer?检查您是否覆盖适配器 getItemViewType(position) 功能正确,也许您正在从尚未同步的 AsyncDiffers 当前列表中获取位置?

关于android - 不同 ViewType 之间的 NotifyItemMoved 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63367374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com