2

我想实现从下到上的卡片堆栈,每次重叠之前的卡片堆栈。有人使用 RecyclerView 和自定义布局管理器来实现这个。我没有足够的代码。

如何使用 Recyclerview 做到这一点。

4

1 回答 1

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);
于 2017-06-28T13:40:40.880 回答