首先,我整天都在努力,但无法完成任何事情.我有一个带有适配器的RecyclerView,它使用RecyclerView的SortedList.我尝试使用回调类实现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);
我的适配器实现了OnItemChangeListener接口
@Override
public void onItemSwiped(int position) {
notifyItemChanged(position);
removalPendingTimers.add(timerList.get(position));
}
我的ViewHolder读取状态,当状态为TimerHolder.TIMER_PENDING_DELETE时,它会隐藏视图的其余部分并显示带有撤消按钮的界面.但是直到我滚动视图并再次向后滚动它才会发生这种情况.有什么建议我缺少什么?