2016-09-14 52 views
3

我想添加自定義分隔在RecyclerViewGridLayoutManager,但沒有得到成功,我尋覓了很多,看着下面提及的答案,但它並沒有幫助我GridLayoutManager自定義分隔

link 1
link 2
link 3

我想把黑線放在RecyclerView的各個項目之間,如下圖所示。

enter image description here

我有在各行之間的水平線,但無法找到如何讓這些線也列之間。

chintan soni's答案非常完美,但它是建立在一個場景中的問題而已,當我有5次,它顯示的其他3項分頻器也,象下面這樣:

enter image description here

+0

這個答案正常工作。 http://stackoverflow.com/a/29168276/2900893 –

+0

@ShabbirDhangot它只是留下物品之間的空間,不允許設置自定義分隔線,我已經嘗試過它。在這個答案我還沒有看到任何設置顏色的範圍,否則int這樣我也可以使用 –

+0

你是如何得到自定義分隔線之間? –

回答

2

看看這個三條連勝垂直線:https://bignerdranch.github.io/simple-item-decoration/

添加到您的應用程序級的gradle產出和同步:

compile 'com.bignerdranch.android:simple-item-decoration:1.0.0' 

然後,必須遵守以下代碼:

Drawable horizontalDivider = ContextCompat.getDrawable(this, R.drawable.line_divider); 
    Drawable verticalDivider = ContextCompat.getDrawable(this, R.drawable.line_divider); 
    recyclerView.addItemDecoration(new GridDividerItemDecoration(horizontalDivider, verticalDivider, 4)); 

我的line_divider.xml如下:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 

    <size 
     android:width="1dp" 
     android:height="1dp" /> 

    <solid android:color="@android:color/black" /> 

</shape> 

這只是我的一個快速答案。但是,這應該工作,我想..

輸出:enter image description here

+2

這是一生適當的方式解決:) – Piyush

+0

@Piyush啊..這是.. :) –

+0

謝謝,它的工作:) –

0

按你我有四個專欄。這將有四列

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"> 
     <View 
      android:layout_width="1dp" 
      android:layout_height="match_parent" 
      android:background="@android:color/transparent"/> 
    </RelativeLayout> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"> 
     <View 
      android:layout_width="1dp" 
      android:layout_height="match_parent" 
      android:background="@color/black"/> 
    </RelativeLayout> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"> 
     <View 
      android:layout_width="1dp" 
      android:layout_height="match_parent" 
      android:background="@color/black"/> 
    </RelativeLayout> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"> 
     <View 
      android:layout_width="1dp" 
      android:layout_height="match_parent" 
      android:background="@color/black"/> 
    </RelativeLayout> 
</LinearLayout> 
+1

舊的和知名的,終生的解決方案..:D –

+0

:)會嘗試,希望它會工作。但我有一個問題,如果我有9個項目?前兩行將被填滿,在第三行我會得到一個項目,但它仍然會顯示我的2行第3和第4列嗎? –

+0

是的,它也會發生在這個和項目裝飾器上。它顯示項目裝飾器上的空白部分。因爲它是單一顏色,你不會看到任何副作用。 –