14

Tôi đang sử dụng RecyclerView để hiển thị Danh sách các mục và tôi cần xóa khoảng cách mặc định giữa các mục. Tôi đang cố gắng để thiết lập một RecyclerView.LayoutParams và thiết lập lề đến số không trên LinearLayoutManager của tôi nhưng không hoạt động!Xóa khoảng cách giữa các mục trong RecyclerView android

Ở đây mã của tôi:

bố trí/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> 

bố trí/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); 

tôi cần sự giúp đỡ ...

+0

Tại sao bạn xác định nhưng không bao giờ sử dụng params trong mã của bạn? –

Trả lời

7

chỉ loại bỏ các cardview trong bố trí/cardview_recycler.xml của bạn, android đặt rằng khoảng cách mà bạn không muốn

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

tất cả mọi thứ ở lại khác vì nó là

+0

Thật tuyệt vời! cảm ơn bạn! – fvasquezjatar

+5

Không có cách nào để điều chỉnh khoảng cách mà chế độ xem thẻ đặt? Tôi muốn giữ khoảng cách, nhưng tôi không muốn không gian 20dp? – Zapnologica

+0

thanks..Điều này cũng làm việc cho tôi .. –

7

cho android: layout_height = wrap_content thay vì match_parent để bố trí gốc hàng của bạn

<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

Cố gắng sử dụng cardElevation = 0dp. Điều này sẽ loại bỏ khoảng cách thêm giữa các mục 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 /> 
0

loại bỏ các dòng setVisibiilty(gone)

sau khi gỡ bỏ nó hoạt động hoàn hảo.

0

Trong trường hợp của tôi dưới đây đang làm việc

<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" /> 
Các vấn đề liên quan