Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
671 views
in Technique[技术] by (71.8m points)

android - How to Overlap items in LinearLayoutManager - RecyclerView (like stacking cards)


Is that possible to overlap the items from RecyclerView ?
I am trying that with LinearLayoutManager.
My requirements are just the same as in LinearLayoutManager and I just need to overlap the items inside from top to bottom. (like stacking cards)

I have seen some variant by using ListView. So, I figure it would be possible in RecyclerView too.But, after some times of exploration, I feel like its gonna take quite some time to implement a custom layout manager and quite an extent of understanding on this.(I looked into Dave's post about Building Custom LayoutManager )

So, now I am thinking I might just need to use ListView variation based on my requirement instead of dealing with this much complexity of Custom Layout Manager.

But, I just need to make the items inside to overlap. I feel like there might be some other direction that I haven't aware yet. Pls let me know if there is any apart from above ListView varient and Custom Layout Manager.

I will also post my findings below here.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I assume you're looking for a partial overlap (e.g. deck of cards slightly fanned out). If so, this seems fairly simple with RecyclerView and a custom ItemDecoration. Here's a trivial example of one that overlaps the items by 90px vertically:

public class OverlapDecoration extends RecyclerView.ItemDecoration {

  private final static int vertOverlap = -90;

  @Override
  public void getItemOffsets (Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

    outRect.set(0, vertOverlap, 0, 0);

  }
}

This example hard codes the offset, but if your list items vary in height you'll need to measure and add logic for this.

Add this decoration to the RV before setting the layoutmanager. I've tried it with a StaggeredGrid but it should work with LinearLayout and Grid LM's as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...