简体   繁体   English

Recyclerview - 从下到上重叠项目

[英]Recyclerview - Overlap items bottom to top

I have set the negative margin for items like this:-我已经为这样的项目设置了负边距:-

ItemDecoration.java ItemDecoration.java

public class ItemDecorator extends RecyclerView.ItemDecoration {
    private final int mSpace;

    public ItemDecorator(int space) {
        this.mSpace = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view);
        if (position != 0)
            outRect.top = mSpace;
    }
}

MainActivity.java MainActivity.java

recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new ItemDecorator(-80));

This causes top item to stack lowest and next item after the top item overlaps it.这会导致顶部项目在顶部项目重叠后堆叠最低和下一个项目。 I want top item to overlap its next item and so on.我希望顶部项目与其下一个项目重叠,依此类推。

Current View目前看来

在此处输入图像描述

Required View所需视图

在此处输入图像描述

Try this way and render your recycler view in reverse direction.尝试这种方式并以相反的方向呈现您的回收站视图。

LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setReverseLayout(true);
        layoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(layoutManager);

Here is the working example GitHub Link这是工作示例GitHub 链接

As of 2020 there is new interface ChildDrawingOrderCallback .截至 2020 年,有新的接口ChildDrawingOrderCallback It defines the order of drawing elements in recycler view.它定义了回收器视图中绘制元素的顺序。 Can be used like so:可以这样使用:

class BackwardsDrawingOrderCallback : RecyclerView.ChildDrawingOrderCallback {
    override fun onGetChildDrawingOrder(childCount: Int, i: Int) = childCount - i - 1
}

And then然后

recyclerView.setChildDrawingOrderCallback(BackwardsDrawingOrderCallback())

So there is no need to set neither reverse order nor stack from end anymore.因此,不再需要设置反向顺序和堆栈。

You can create overlapping Ite Decorator by using: Check this link您可以使用以下方法创建重叠的装饰器:检查此链接

class OverlappingItemDecoration : RecyclerView.ItemDecoration() {
    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        if (parent.getChildAdapterPosition(view) != RecyclerView.NO_POSITION) {
            outRect.set(0, 0, 0, - 60)
        }
    }
}

The easiest way to achieve overlap is using margin with minus values inside recycler view item like so:实现重叠的最简单方法是在回收器视图项中使用带负值的边距,如下所示:

image_item.xml image_item.xml

<com.google.android.material.imageview.ShapeableImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/authorIV"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_marginStart="-8dp"
    android:scaleType="centerCrop"
    android:src="@drawable/ic_default_avatar"
    app:roundPercent="50" />

Try overriding this method.尝试覆盖此方法。

@Override
public int getItemViewType(int position) {
    return super.getItemViewType(position);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM