【问题标题】:how can I center items in each row in recyclerview staggeredgridlayout?如何在 recyclerview staggeredgridlayout 中将每行中的项目居中?
【发布时间】:2016-01-17 21:17:39
【问题描述】:

关于recyclerview的例子很少,我还没有找到任何相关的解决方案,如果有人帮助我,我会很高兴。我只是想让我的视图看起来像照片的左侧部分

The right view is what I've done and the left view is what I want to achieve

这是片段中的recyclerview

    <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_marginTop="15dp"
    android:scrollbars="vertical"
    />

这是单项xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="2dp">

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    card_view:cardBackgroundColor="#16AFCA"
    card_view:cardCornerRadius="3dp"
    card_view:contentPaddingLeft="10dp"
    card_view:contentPaddingRight="10dp"
    card_view:contentPaddingBottom="5dp"
    card_view:contentPaddingTop="5dp">

    <TextView
        android:id="@+id/tag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:textAllCaps="true"
        android:text="#love"
        android:gravity="center"/>

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

</RelativeLayout>

这是java代码

        recyclerView = (RecyclerView)     view.findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    staggeredGridLayoutManager = new   StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
    staggeredGridLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
    recyclerView.setLayoutManager(staggeredGridLayoutManager);
    adapter = new StaggeredLayoutAdapter(getActivity(),tags);
    recyclerView.setAdapter(adapter);

【问题讨论】:

    标签: android android-layout android-recyclerview staggeredgridlayout


    【解决方案1】:

    StaggeredGridLayout 用于 Pinterest 类型的视图,其中列对齐且行参差不齐。您想要相反:行对齐,列锯齿状。

    您会对这个问题的答案感兴趣:Does anyone know how to do flow layout using RecyclerView?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-04
      • 2018-10-24
      • 2017-07-18
      • 1970-01-01
      相关资源
      最近更新 更多