Pinterest like Grid in Android

放肆的年华 提交于 2020-01-22 05:31:26

问题


I want to build a grid like the one in Pinterest app on Android.

I started extending an AdapterView<ListAdapter> but I cannot make many things working (for example the overscroll effect) so, after abandoning the idea to extend AbsListView, now I am starting thinking it is better to extend a ListView and override the layoutChildren() method.

What do you think?

Thanks


回答1:


We won’t be needing any external library for this as Android’s native RecyclerView makes implementing a Pinterest masonry layout simply by changing the RecyclerView’s Layout Manager

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

Cool. it's Very easy ,but margin on my LinearLayout didn’t seem to work. So here’s a quick fix.

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

SpacesItemDecoration class:

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




回答2:


I have solved this by copying and updating Android's StaggeredGridView. https://github.com/maurycyw/StaggeredGridView . Seems to work great for creating the effect you are looking for. Before this I went down the rabbit hole of copying Android's source to a separate library (which I got working) until I saw the now experimental StaggeredGridView. It is much simpler than my old "BrickView" library project so I replaced it.




回答3:


AntipodalWall is an standalone library designed to provide a so called "masonry" grid layout for Android (much like Pinterest app).

Check this project AntipodalWall




回答4:


In my projects I'm using: https://github.com/GDG-Korea/PinterestLikeAdapterView

It's very easy to use it and it's memory consumption efficient




回答5:


This can be achieved by small change to existing recyclerview and it's adapter

Change to be done are : 1.In you Activity / Frgament

uploadMediaAdapter = new UploadMediaAdapter(getActivity(),mediaArrayList);    
rv_uploaded.setAdapter(uploadMediaAdapter);
int spanCount = 2;
rv_uploaded.setLayoutManager(new StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.VERTICAL));

2.Your Recyclerview list item is :

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/dim_5"
    app:cardCornerRadius="@dimen/dim_5"
    app:cardUseCompatPadding="true"
    android:clipToPadding="true"
    >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/img_prev_photo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:adjustViewBounds="true"
            />

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/txt_progress_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8th August 2019"
            android:textSize="@dimen/text_size_17"
            android:textStyle="bold"
            android:layout_gravity="bottom|center_horizontal"
            android:textColor="@color/color_white"
            />

    </FrameLayout>


</android.support.v7.widget.CardView>

By doing this you can achieve what you are looking for!



来源:https://stackoverflow.com/questions/12442795/pinterest-like-grid-in-android

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