2017-03-04 82 views
1

的底部我找不到這個問題的答案在網上的任何地方,所以答案一定是如此簡單,沒有人關心問它,我根本不知道這一點。的Android RecyclerView addItemDecoration不會增加裝飾RecyclerView

從本質上講,我想添加的每個項目之間的水平線,我recyclerView並向RecyclerView的底部。我已經找到了一種方法來在RecyclerView每個項目將之間的分隔,但沒有一個在年底

根據該文件(這裏找到:https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#addItemDecoration(android.support.v7.widget.RecyclerView.ItemDecoration,INT))

把負INT到addItemDecoration(RecyclerView.ItemDecoration裝飾, INT指數)。應該解決這個問題,說明:

如果這個值是負值,裝飾將被添加到最後。

這是我的自定義RecyclerView構造方法:

public CustomRecyclerView(Context context, @Nullable AttributeSet attrs) { 
    super(context, attrs); 
    setHasFixedSize(true); 
    setLayoutManager(new LinearLayoutManager(context)); 
    decor = new HorizontalDividerItemDecoration.Builder(context).build(); 
    addItemDecoration(decor, -1); 
} 

正如你所看到的,我使用addItemDecoration()負整數,我想不通爲什麼它不工作它應該的方式。

任何幫助將不勝感激!

+1

看起來你正在做的一切都是正確的。也許addItemDecoration()的目的不是在Recyclerview中的最後一個項目之後畫一個Divider。我在文檔中找不到有關此行爲的任何信息。我可以建議的唯一的事情是,你檢查你的填充或邊緣在底部,也許最後itemDecoration被切斷? –

回答

3

按賈尼絲卡蒂卡的建議,我複製並粘貼到我的項目的代碼。我複製並粘貼Janice的代碼後,Android的工作室給了我這個建議:

替換android.support.v7.widge.DividerItemDecoration?

較舊版本的RycyclerView庫不包含分隔裝飾器,但其中一個作爲支持演示中的樣本提供。這個分頻器類被廣泛地複製/粘貼到各種項目中。

在支持庫的最新版本中,現在包含了divider修飾器,因此您可以使用「內置」版本android.support.v7.widget替換自定義拷貝。的DividerItemDecoration

因此,而不是使用Horizo​​ntalDividerItemDecoration(這是我們進口了外部庫的一部分)是這樣的:

decor = new HorizontalDividerItemDecoration.Builder(context).build(); 
addItemDecoration(decor, -1); 

我使用默認的 「DividerItemDecoration」 是這樣的:

decor = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL); 
addItemDecoration(decor); 

它工作。如果你沒有把「int index」放入addItemDecoration,它會默認爲-1,默認情況下將裝飾添加到底部。

+1

嗨@SethCoast,我剛剛閱讀了Android Studio 2.3的發佈說明,我很高興他們最終將其包含在RecyclerView中!我也會更新我的代碼,謝謝。 –

1

我得到了很久很久以前的相同的問題,我能夠通過複製粘貼DividerItemDecoration類從this鏈接解決此問題。但現在看來,像一些方法現在已經過時,所以我做了輕微修改它:

public class DividerItemDecoration extends RecyclerView.ItemDecoration { 
    private static final int[] ATTRS = new int[]{ 
      android.R.attr.listDivider 
    }; 
    private static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; 
    private 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); 
     } 
    } 

    private 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); 
     } 
    } 

    private 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); 
     } 
    } 
} 

複製 - 粘貼到您的項目,並使用它像這樣:

public CustomRecyclerView(Context context, @Nullable AttributeSet attrs) { 
    super(context, attrs); 
    setHasFixedSize(true); 
    setLayoutManager(new LinearLayoutManager(context)); 
    addItemDecoration(new DividerItemDecoration(context, LinearLayoutManager.VERTICAL)); 
}