How to implement StackLayoutManager in android Vertical RecyclerView

一笑奈何 提交于 2019-12-24 07:50:45

问题


I want to implement stack of cards from bottom to top swipe each overlap previous card stack.Someone implement this using RecyclerView with custom Layout manager.I don't have enough code.

How to do this using Recyclerview.


回答1:


Try this way:

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());
        }
    }
}

Than use it with your RecyclerView:

recyclerView.setLayoutManager(new StackLayoutManager(this));

And finally add ItemTouchHelper for handling swiping:

 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);


来源:https://stackoverflow.com/questions/43894614/how-to-implement-stacklayoutmanager-in-android-vertical-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!