【问题标题】:Remove spacing between items in RecyclerView android删除 RecyclerView android中项目之间的间距
【发布时间】:2015-01-20 02:53:38
【问题描述】:

我正在使用 RecyclerView 显示项目列表,我需要删除项目之间的默认间距。我正在尝试在我的 LinearLayoutManager 上设置 RecyclerView.LayoutParams 并将边距设置为零,但没有成功!

这是我的代码:

layout/fragment_recycler.xml

   <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/freshSwipeView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/freshRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />
    </android.support.v4.widget.SwipeRefreshLayout>

layout/cardview_recycler.xml

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <mypackagename.ui.view.RecyclingImageView
            android:id="@+id/avatar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:adjustViewBounds="true"
            android:src="@drawable/img_no_avatar" />

        <TextView
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:singleLine="true"
            android:ellipsize="end"
            android:padding="@dimen/spacing"
            android:textColor="@color/black"
            android:layout_alignParentBottom="true"
            android:textSize="@dimen/text_smallest" />
    </LinearLayout>
</android.support.v7.widget.CardView>

RecyclerFragment.java

    /* Setting Layout Manager */
    mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT);
    params.setMargins(0, 0, 0, 0);
    mLayoutManager.canScrollVertically();
    mRecyclerView.setLayoutManager(mLayoutManager);

我需要帮助...

【问题讨论】:

  • 为什么在代码中定义但从不使用参数?

标签: android material-design android-recyclerview android-cardview


【解决方案1】:

只需删除 layout/cardview_recycler.xml 中的卡片视图,android 会放置您不想要的间距

<LinearLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <mypackagename.ui.view.RecyclingImageView
        android:id="@+id/avatar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:src="@drawable/img_no_avatar" />

    <TextView
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:ellipsize="end"
        android:padding="@dimen/spacing"
        android:textColor="@color/black"
        android:layout_alignParentBottom="true"
        android:textSize="@dimen/text_smallest" />
</LinearLayout>

其他一切都保持原样

【讨论】:

  • 有没有办法调整卡片视图放置的间距?我想保持间距,但我不想像 20dp 的空间?
  • 为什么cardview要加间距?
【解决方案2】:

为您的项目根布局提供 android:layout_height = wrap_content 而不是 match_parent

<android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
       -------
       -------
</android.support.v7.widget.CardView>

【讨论】:

    【解决方案3】:

    在我的例子中,下面的代码有效

    <android.support.v7.widget.RecyclerView
                android:id="@+id/freshRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipToPadding="false"
                android:scrollbars="vertical" />
    

    【讨论】:

      【解决方案4】:

      删除setVisibiilty(gone)这一行

      删除后效果很好。

      【讨论】:

        【解决方案5】:

        尝试使用 cardElevation=0dp。这应该会删除 recyclerview 项目之间的额外间距。

        <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="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginRight="2dp"
           android:layout_marginEnd="2dp"
           app:cardElevation="0dp">
        
          <LinearLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
        
            <mypackagename.ui.view.RecyclingImageView
                android:id="@+id/avatar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="fitXY"
                android:adjustViewBounds="true"
                android:src="@drawable/img_no_avatar" />
        
            <TextView
                android:id="@+id/username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:singleLine="true"
                android:ellipsize="end"
                android:padding="@dimen/spacing"
                android:textColor="@color/black"
                android:layout_alignParentBottom="true"
                android:textSize="@dimen/text_smallest" />
         </LinearLayout>
        <android.support.v7.widget.CardView />
        

        【讨论】: