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 в коде? –

ответ

7

просто удалить cardview в вашем макете/cardview_recycler.xml, андроид ставит, что расстояние, что вы не хотите

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

всего остального, пребывания as is

+0

Ничего себе! Спасибо! – fvasquezjatar

+5

Невозможно отрегулировать интервал, который ставится на карту? Я хочу сохранить интервал, но я не хочу, чтобы 20dp пространство? – Zapnologica

+0

спасибо .. Это сработало для меня тоже .. –

7

Дайте андроид: 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. Это должно устранить дополнительное расстояние между элементами 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

удалить строку 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" /> 

 Смежные вопросы

  • Нет связанных вопросов^_^