2016-09-27 2 views
0

Привет У меня есть простой CardView, где я хочу показать разделитель между каждыми Cardview. Проблема в том, что мой View, который я использую в качестве разделителя, никогда не отображается последним CardView в моем RecyclerView. Вот моя попытка:Вид не отображается в нижней части CardView?

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:fresco="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="100dp" 
    card_view:cardElevation="0dp" 
    android:id="@+id/cv_news_feed"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="16dp"> 

      ...Content... 


     </RelativeLayout> 

    </LinearLayout> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:background="@color/material_color_grey_300" /> 

</android.support.v7.widget.CardView> 

Делитель показывает на каждой CardView для последнего по какой-либо причине, кроме и я не знаю, почему это происходит. Любая помощь будет оценена, спасибо!

EDIT: Изображение разместил: enter image description here

+0

не согласны поделиться скриншотом? – raxelsson

+0

@raxelsson опубликовал снимок экрана – user1871869

+0

Разделитель фактически отображается в верхней части «CardView», потому что он расширяет «FrameLayout», и вы не придаете ему никакого значения. Сейчас есть несколько ответов, которые помогут вам. – raxelsson

ответ

0

CardView является продолжением FrameLayout, что означает, что ваш делитель не в нижней части ваш CardView, но в верхней части.

Вместо этого разместите свой разделитель в своем LinearLayout.

+0

Эй, это действие сделало это. Пометьте, это любимый ответ. – user1871869

+0

Прохладный, счастливый код! – raxelsson

0

Попробуйте поставить делитель в LinearLayout, то есть:

1

Удалить следующий вид из вашего пункта

<View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:background="@color/material_color_grey_300" /> 

и попытаться использовать ItemDecoration следующим

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(
       Utility.ItemDecorationConst); 
recyclerView.addItemDecoration(5); 

DividerItemDecoration.java

public class DividerItemDecoration extends RecyclerView.ItemDecoration { 
    private int space; 

    public DividerItemDecoration(int space) { 
     this.space = space; 
    } 

    @Override 
    public void getItemOffsets(Rect outRect, View view, 
           RecyclerView parent, RecyclerView.State state) { 
     outRect.left = space; 
     outRect.right = space; 
     outRect.top = space; 
     outRect.bottom = space; 
    } 
} 
0

Добавить layout_gravity атрибута в View:

<View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:layout_gravity="bottom" 
     android:background="@color/material_color_grey_300" /> 
-1

Вы должны использовать как этот

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="16dp"> 

     ...Content... 


    </RelativeLayout> 

    <View 
    android:layout_width="match_parent" 
    android:layout_height="1dp" 
    android:background="@color/material_color_grey_300" /> 

</LinearLayout> 

Поскольку CardView расширяет FrameLayout так что ваш devider будет за содержание LinearLayout.

+0

Зачем участвовать в матче? – MinWan

0

Вы можете попробовать этот код, надеюсь, что это может помочь вам

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     xmlns:fresco="http://schemas.android.com/tools" 
     android:id="@+id/cv_news_feed" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:minHeight="100dp" 
     card_view:cardElevation="0dp"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:padding="16dp"> 

       ...Content... 

      </RelativeLayout> 
     </LinearLayout> 
    </android.support.v7.widget.CardView> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:layout_below="@+id/cv_news_feed" 
     android:layout_marginTop="5dp" 
     android:background="@android:color/holo_red_dark" /> 
</RelativeLayout> 
0

Создать класс DividerItemDecoration:

import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Canvas; 
import android.graphics.Rect; 
import android.graphics.drawable.Drawable; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.view.View; 

/** 
* Created by Lincoln on 30/10/15. 
*/ 
public class DividerItemDecoration extends RecyclerView.ItemDecoration { 

    private static final int[] ATTRS = new int[]{ 
      android.R.attr.listDivider 
    }; 

    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; 

    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; 

    private Drawable mDivider; 

    private int mOrientation; 

    public DividerItemDecoration(Context context, int orientation) { 
     final TypedArray a = context.obtainStyledAttributes(ATTRS); 
     mDivider = a.getDrawable(0); 
     a.recycle(); 
     setOrientation(orientation); 
    } 

    public void setOrientation(int orientation) { 
     if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { 
      throw new IllegalArgumentException("invalid orientation"); 
     } 
     mOrientation = orientation; 
    } 

    @Override 
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { 
     if (mOrientation == VERTICAL_LIST) { 
      drawVertical(c, parent); 
     } else { 
      drawHorizontal(c, parent); 
     } 
    } 

    public void drawVertical(Canvas c, RecyclerView parent) { 
     final int left = parent.getPaddingLeft(); 
     final int right = parent.getWidth() - parent.getPaddingRight(); 

     final int childCount = parent.getChildCount(); 
     for (int i = 0; i < childCount; i++) { 
      final View child = parent.getChildAt(i); 
      final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child 
        .getLayoutParams(); 
      final int top = child.getBottom() + params.bottomMargin; 
      final int bottom = top + mDivider.getIntrinsicHeight(); 
      mDivider.setBounds(left, top, right, bottom); 
      mDivider.draw(c); 
     } 
    } 

    public void drawHorizontal(Canvas c, RecyclerView parent) { 
     final int top = parent.getPaddingTop(); 
     final int bottom = parent.getHeight() - parent.getPaddingBottom(); 

     final int childCount = parent.getChildCount(); 
     for (int i = 0; i < childCount; i++) { 
      final View child = parent.getChildAt(i); 
      final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child 
        .getLayoutParams(); 
      final int left = child.getRight() + params.rightMargin; 
      final int right = left + mDivider.getIntrinsicHeight(); 
      mDivider.setBounds(left, top, right, bottom); 
      mDivider.draw(c); 
     } 
    } 

    @Override 
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 
     if (mOrientation == VERTICAL_LIST) { 
      outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); 
     } else { 
      outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); 
     } 
    } 
} 

Затем установите оформление элементов с помощью addItemDecoration() метода перед установка адаптера.

recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL)); 

// set the adapter 
recyclerView.setAdapter(mAdapter); 

N.B. Удалите представление, которое вы используете в макете просмотра, чтобы показать разделитель.

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