gpt4 book ai didi

android - notifyItemChanged 不刷新 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:03:17 27 4
gpt4 key购买 nike

首先,我为此工作了一整天,但一事无成。我有一个 RecyclerView 和一个使用 RecyclerViewSortedList 的适配器。我尝试使用回调类实现 TouchHelper:

public class TimerListTouchHelperCallback extends ItemTouchHelper.SimpleCallback {

private OnItemChangeListener onItemChangeListener;

public TimerListTouchHelperCallback(OnItemChangeListener listener, int dragDirs, int swipeDirs) {
super(dragDirs, swipeDirs);

this.onItemChangeListener = listener;

}

@Override
public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
TimerHolder holder = (TimerHolder) viewHolder;

int holderState = holder.getState();

if (holderState == TimerHolder.TIMER_PENDING_DELETE) return 0;
else return super.getSwipeDirs(recyclerView, viewHolder);
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}




@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
TimerHolder holder = (TimerHolder) viewHolder;

int position = holder.getAdapterPosition();
// ViewHolder's state is changed that should handle the layout change.
holder.setState(TimerHolder.TIMER_PENDING_DELETE);

if (onItemChangeListener != null) onItemChangeListener.onItemSwiped(position);
}


protected interface OnItemChangeListener{
void onItemSwiped(int position);
}
}

这里是TouchHelper的初始化

timerAdapter = new TimerAdapter(this, timerList);
timerListView.setAdapter(timerAdapter);

TimerListTouchHelperCallback touchHelperCallback = new TimerListTouchHelperCallback(
timerAdapter,
ItemTouchHelper.LEFT,
ItemTouchHelper.LEFT);

ItemTouchHelper swipeToDismissTouchHelper = new ItemTouchHelper(touchHelperCallback);

swipeToDismissTouchHelper.attachToRecyclerView(timerListView);

我的适配器实现接口(interface) OnItemChangeListener

@Override
public void onItemSwiped(int position) {
notifyItemChanged(position);
removalPendingTimers.add(timerList.get(position));
}

我的 ViewHolder 读取状态,当状态为 TimerHolder.TIMER_PENDING_DELETE 时,它会隐藏 View 的其余部分并显示带有撤消按钮的界面。但这不会发生,直到我将 View 滚动出来并再次滚动回来。有什么建议我缺少什么吗?

Problem case

链接

Adapter class ViewHolder class

最佳答案

终于可以运行了。由于 notifyItemChanged 没有削减它(它应该有),我使用了 notifyItemRemoved,然后是 notifyItemInserted

只是有点滞后,但有效。

关于android - notifyItemChanged 不刷新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39021179/

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