Android - 如何使GridLayoutManager包装RecycledView项目

时间:2015-08-13 07:23:07

标签: android xamarin android-recyclerview android-gridlayout

如果屏幕上有足够的空间,我希望包装RecycledView中的项目,就像Google Play商店的设计方式一样。我怎样才能做到这一点?

回收视图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:weightSum="1">
    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.9">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/dashboard_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />
    </RelativeLayout>
</LinearLayout>

产品:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:clickable="true" >
        <ImageView
            android:id="@+id/sensor_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true" />
        <TextView
            android:id="@+id/sensor_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/sensor_image" />
    </LinearLayout>
</android.support.v7.widget.CardView>

OnCreateView():

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.Inflate(Resource.Layout.Dashboard, container, false);
        recyclerView = view.FindViewById<RecyclerView>(Resource.Id.dashboard_recycler_view);
        layoutManager = new GridLayoutManager(Activity, 2);

        recyclerView.HasFixedSize = true;

        recyclerView.SetLayoutManager(layoutManager);
        recyclerView.SetItemAnimator(new DefaultItemAnimator());
        recyclerView.AddItemDecoration(new SpaceItemDecoration(20));

        return view;
    }

到目前为止看起来如何:

enter image description here enter image description here

正如您所看到的,这些物品不会被包裹,它们会缩小,以便它们适合视图。

0 个答案:

没有答案