2016-09-14 188 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

是的,它也会发生在这个和项目装饰器上。它显示项目装饰器上的空白部分。因为它是单一颜色,你不会看到任何副作用。 –