22

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

这是我的代码:

布局/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>

布局/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);

我需要帮助...

4

5 回答 5

12

只需删除您的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>

其他一切都保持原样

于 2014-11-21T21:30:29.720 回答
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>
于 2016-05-02T09:38:59.053 回答
1

在我的情况下,下面的代码有效

<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" />
于 2018-01-23T12:05:46.230 回答
0

删除线setVisibiilty(gone)

删除后完美运行。

于 2017-05-15T10:28:02.913 回答
-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 />
于 2017-02-03T09:51:46.797 回答