2016-09-14 1 views
3

Je suis en train d'ajouter diviseur personnalisé dans RecyclerView avec GridLayoutManager mais pas obtenir le succès, j'ai beaucoup cherché et examiné mentionner ci-dessous réponse, mais il ne m'a pas aidéGridLayoutManager avec diviseur personnalisé

link 1
link 2
Je veux mettre une ligne noire entre chaque article de RecyclerView, quelque chose comme ci-dessous.

enter image description here

J'ai ligne horizontale entre chaque rangée, mais pas en mesure de trouver comment obtenir ces lignes entre les colonnes aussi.

chintan soni's réponse a parfaitement fonctionné, mais il crée problème dans un seul cas, quand j'ai 5 vues, il montre diviseur de 3 autres articles aussi, comme ci-dessous:

enter image description here

+0

cette réponse fonctionne correctement. http://stackoverflow.com/a/29168276/2900893 –

+0

@ShabbirDhangot il est juste laisser de l'espace entre les éléments, ne permet pas de définir un diviseur personnalisé, je l'ai déjà essayé. Et dans cette réponse, je n'ai pas vu la possibilité de définir la couleur aussi, sinon je peux aussi utiliser –

+0

Comment avez-vous eu un diviseur personnalisé entre les lignes? –

Répondre

2

Check this out: https://bignerdranch.github.io/simple-item-decoration/

Ajouter à votre niveau application gradle et synchronisation:

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

Ensuite, le code s'appliquent comme ci-dessous:

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

Mon line_divider.xml était la suivante:

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

Ceci est juste une réponse rapide de ma part. Mais cela devrait fonctionner, je suppose ..

Sortie:enter image description here

+2

Ceci est la solution de la bonne voie à vie :) – Piyush

+0

@Piyush oui .. il est .. :) –

+0

Merci, cela a fonctionné :) –

0

Selon vous J'ai quatre colonnes que je crée comme cela. Cela aura trois ligne droite verticale pour quatre colonnes

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

vieux et bien connu, solution à vie ..: D –

+0

:) va essayer, j'espère que cela va fonctionner. Mais j'ai une question, et si j'ai 9 articles? les 2 premières rangées seront remplies et dans la 3ème rangée j'obtiendrai un objet, mais il me montrera quand même 2 lignes de 3ème et 4ème colonnes? –

+0

oui ça va arriver avec ce décorateur et aussi.Il montre la partie vide là-bas sur le décorateur d'article. Parce que c'est une couleur unique, vous ne verrez aucun effet secondaire. –