2016-09-27 28 views
0

嗨我有一個簡單的CardView我想在每個Cardview之間顯示分頻器。問題是,我用作分隔線的View從未在我的RecyclerView的最後CardView上顯示。這裏是我的嘗試:查看沒有出現在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除了最後不管是什麼原因,我不知道這是爲什麼發生。任何幫助將不勝感激,謝謝!

編輯:圖片貼: enter image description here

+0

你介意分享截圖嗎? – raxelsson

+0

@raxelsson發佈了屏幕截圖 – user1871869

+0

該分割器實際上呈現在'CardView'的頂部,因爲它擴展了'FrameLayout',並且不會引起任何重力。現在有幾個答案可以幫助你。 – raxelsson

回答

0

CardViewFrameLayout的延伸,這意味着你的分頻器是不是在底部您的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

因爲CardView延伸的FrameLayout所以你devider會的LinearLayout內容背後你應該使用這樣

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

+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.刪除您在cardview佈局中使用的視圖以顯示分隔線。