14

我正在使用RecyclerView來顯示項目列表,我需要刪除項目之間的默認間距。我試圖設置一個RecyclerView.LayoutParams並在我的LinearLayoutManager上設置邊距爲零,但沒有奏效!刪除RecyclerView中項目之間的間距android

這裏我的代碼:

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

我需要幫助...

+0

爲什麼你定義,但從來沒有在代碼中使用PARAMS? – 2015-11-23 14:14:21

回答

7

只是刪除cardview在佈局/ 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> 

一切住宿因爲它是

+0

哇!謝謝! – fvasquezjatar 2014-11-21 21:54:10

+5

有沒有辦法調整卡片視圖放置的間距?我想保持間距,但我不想要20dp的空間? – Zapnologica 2015-05-19 09:06:25

+0

謝謝..這也爲我工作.. – 2017-07-08 06:13:51

7

給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

嘗試使用cardElevation = 0dp。這應該刪除回收站視圖項目之間的額外間距。

<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 /> 
0

刪除行setVisibiilty(gone)

刪除它完美了。

0

在我的情況下面代碼工作

<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" /> 
相關問題