我有RecyclerView
哪个是HORIZONTAL
。其中每件物品的宽度几乎等于回收站的总宽度。最多只能ViewHolders
看到 3 个。
这是一些模型,可以更好地理解结果的外观。
我想添加重新定位卡片的功能,RecyclerView
所以我创建ItemTouchHelper.Callback
并将其附加到RecyclerView
.
一旦我长按任何卡片,我就可以将它向左和向右拖动。当我将它移到屏幕边缘附近时,它会RecyclerView
自动开始滚动,然后我的视图就会消失。
据我所知,我猜它只会被回收 -ItemTouchHelper#clearView(RecyclerView, RecyclerView$ViewHolder)
被称为并且它起源于RecyclerView#removeDetachedView(View)
.
有什么我可以绕过这种行为吗?
ItemTouchHelper.Callback drag = new ItemTouchHelper.Callback() {
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.START | ItemTouchHelper.END | ItemTouchHelper.UP | ItemTouchHelper.DOWN;
return makeMovementFlags(dragFlags, ItemTouchHelper.ACTION_STATE_IDLE);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
int oldPosition = viewHolder.getAdapterPosition();
int newPosition = target.getAdapterPosition();
Log.d(getClass().getSimpleName(), String.format("View moved from %d to %d", oldPosition, newPosition));
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
// Not applicable
}
};
mView.recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
mView.recycler.setAdapter(mAdapter = new SimpleAdapter());
ItemTouchHelper touchHelper = new ItemTouchHelper(drag);
touchHelper.attachToRecyclerView(mView.recycler);