我想从底部到顶部实现一叠卡片,每次重叠以前的卡片堆叠。有人使用RecyclerView和自定义布局管理器实现这一点。我没有足够的代码。
如何使用Recyclerview执行此操作。
答案 0 :(得分:0)
尝试这种方式:
public class StackLayoutManager extends LinearLayoutManager {
public StackLayoutManager(Context context) {
super(context);
setStackFromEnd(true);
}
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
@Override
public boolean canScrollHorizontally() {
return false;
}
@Override
public boolean canScrollVertically() {
return false;
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
super.onLayoutChildren(recycler, state);
updatePosition();
}
private void updatePosition() {
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
ViewCompat.setTranslationY(view, -view.getTop());
}
}
}
与您的RecyclerView一起使用:
recyclerView.setLayoutManager(new StackLayoutManager(this));
最后添加ItemTouchHelper来处理滑动:
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
int pos = viewHolder.getAdapterPosition();
if (pos != RecyclerView.NO_POSITION) {
adapter.onRemove(viewHolder);
}
}
});
itemTouchHelper.attachToRecyclerView(recyclerView);