移除RecyclerView中项之间的空格(Android)。

24

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

这是我的代码:

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);

我需要帮助...


为什么你在代码中定义了参数却从未使用过? - Varvara Kalinina
5个回答

13

只需在您的layout/cardview_recycler.xml中移除CardView,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>

其他所有内容保持不变


8
有没有办法调整卡片视图的间距?我想保留间距,但是不想要像20dp这样的间隔? - Zapnologica
1
为什么CardView会添加间距? - blackHawk

8

将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>

2
在我的情况下,以下代码有效
<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" />

0

移除代码行 setVisibiilty(gone)

移除该代码行后,程序运行完美。


-4
尝试使用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"
    app:cardMaxElevation="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 />

我在CarView中进行了以下更改,以减少RecycleView项目之间的间隙。 app:cardElevation =“0dp” app:cardMaxElevation =“0dp” - Android Developer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接