我已经实现了ItemTouchHelper,用于在滑动完成后设置从下到上,从上到下的拖放,然后它应该刷新列表。但是我必须刷新列表的回调方法不起作用。
当我滑动列表时,滑动完成后并没有呼叫onMoved
或onSwiped
。
源代码:
ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN,0) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder dragged, RecyclerView.ViewHolder target) {
int position_dragged = dragged.getAdapterPosition();
int position_target = target.getAdapterPosition();
adapter.notifyItemMoved(position_dragged,position_target);
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
//Toast.makeText(getActivity(),"swiped",Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
}
@Override
public void onMoved(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, int fromPos, @NonNull RecyclerView.ViewHolder target, int toPos, int x, int y) {
super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
adapter.notifyDataSetChanged();
}
});
helper.attachToRecyclerView(null);
helper.attachToRecyclerView(myRecyclerView);
答案 0 :(得分:0)
YourAdapter
ItemTouchHelperAdapter
接口 public class DragSwipeHelper extends ItemTouchHelper.Callback {
// implement this interfaces in your adapter class
public interface ItemTouchHelperAdapter {
boolean onItemMove(int fromPosition, int toPosition);
void onItemSwipe(int position);
void onAfterMove();
}
private Context _context;
private final YourAdapter mAdapter;
public DragSwipeHelper(Context context, YourAdapter adapter) {
_context = context;
mAdapter = adapter;
}
@Override
public boolean isLongPressDragEnabled() {
return true;
}
@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
}
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
mAdapter.onItemSwipe(viewHolder.getAdapterPosition());
}
@Override
public void onChildDrawOver(@NonNull Canvas c, @NonNull RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
if (!isCurrentlyActive && actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mAdapter.onAfterMove();
}
}, 250);
}
}
}
DragSwipeHelper
附加到RecyclerView
ItemTouchHelper.Callback callback = new DragSwipeHelper(this, adapter);
ItemTouchHelper helper = new ItemTouchHelper(callback);
helper.attachToRecyclerView(mRecyclerView);