Android BaseAdapter Pinterest风格

时间:2013-06-10 19:31:59

标签: android android-layout jquery-masonry pinterest

有哪些方法可以使视图之间的垂直间距相同,如下图所示?

我有什么enter image description here

我需要什么enter image description here

我尝试过使用LinearLayout的高度来为每个“瓷砖”充气,但似乎无法弄清楚这一点。

2 个答案:

答案 0 :(得分:6)

您可以查看此库。它处于测试状态:

AntipodalWall

Web

答案 1 :(得分:1)

我们不需要任何外部库,因为Android的原生RecyclerView只需更改RecyclerView的布局管理器即可实现Pinterest砌体布局

mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
RecyclerAdapter adapter = new RecyclerAdapter(this);
mRecyclerView.setAdapter(adapter);

冷却。它非常简单,但我的LinearLayout上的保证金似乎不起作用。所以这是一个快速修复。

SpacesItemDecoration decoration = new SpacesItemDecoration(16);
mRecyclerView.addItemDecoration(decoration);

SpacesItemDecoration类:

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

public SpacesItemDecoration(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;

    // Add top margin only for the first item to avoid double space between items
    if (parent.getChildAdapterPosition(view) == 0)
        outRect.top = mSpace;
    }
}

Github link of example

my final Results