在recyclerview中重叠位置/视图

时间:2016-02-04 17:23:28

标签: android android-recyclerview

我一直试图在Recyclerview中重叠两个相邻位置,但未能这样做。我希望第一个位置的一部分重叠/绘制在第二个位置。

我一直在使用自定义的RecyclerView.ItemDecoration和它的getItemOffsets,但是它似乎只能在一个位置上添加填充/边距,而不是将其转换为外部的新位置它是viewparent。

任何人都有关于如何通过recyclerview实现这一目标的任何指示?

1 个答案:

答案 0 :(得分:7)

嗯,您还可以在RecyclerView.ItemDecoration中添加负余量

示例:

<强> ItemDecorator.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) {
        outRect.left = mSpace;
        outRect.right = mSpace;
        outRect.bottom = mSpace;
        outRect.top = mSpace;
    }
}

<强> MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("Hi");
        arrayList.add("World");
        arrayList.add("What");

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rec1);
        //Negative margin!
        ItemDecorator itemDecorator = new ItemDecorator(-30);     
        recyclerView.setAdapter(new CustomAdapter(arrayList));
        recyclerView.addItemDecoration(itemDecorator);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

    }
}