6

I have this simple layout for recycler view items.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/details"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="?android:attr/selectableItemBackground"
android:paddingEnd="15dp"
android:paddingStart="15dp"
>

<org.tchouaffe.ftinfosystem.utils.CustomTextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:textColor="@color/primaryTextColor"
    android:textSize="15sp"/>


<org.tchouaffe.ftinfosystem.utils.CustomTextView
    android:id="@+id/level"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@id/name"
    android:gravity="center_vertical|right"
    android:textColor="@color/primaryTextColor"
    android:textSize="15sp"/>
</RelativeLayout>

And the following code does sets up the recycler view:

 RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    MyAdapter adapter = new MyAdapter(activity, elementList);
    adapter.setHasStableIds(true);
    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView
            .getContext(), DividerItemDecoration.VERTICAL);
    recyclerView.addItemDecoration(dividerItemDecoration);
    recyclerView.setHasFixedSize(false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);

For an unknown reason the divider is not showing up. Any ideas?

I am using android.support.v7.widget.DividerItemDecoration.

Thanks

DeKekem
  • 589
  • 10
  • 20

2 Answers2

6

You also need to call setDrawable() on DividerDecoration.

Provide a drawable resource. An XML shape drawable can be a good start. The shape will be rendered as the divider between recycler item views.

Alternatively, when declaring the app theme, you can set android:listDivider item to the desired drawable.

S.D.
  • 29,290
  • 3
  • 79
  • 130
  • 6
    or setup `android:listDivider`, at least it seems to be working so, see [DividerItemDecoration.java#47](https://android.googlesource.com/platform/frameworks/testing/+/android-support-test/espresso/sample/src/main/java/android/support/test/testapp/DividerItemDecoration.java#47) – pskink Jan 25 '17 at 13:40
  • @pskink Indeed. – S.D. Jan 25 '17 at 13:41
  • ooops posted wrong file, should be: https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v7/recyclerview/src/android/support/v7/widget/DividerItemDecoration.java?autodive=0%2F#65, but still `android:listDivider` is used the same way – pskink Jan 25 '17 at 13:48
  • Hi @pskink. android:listDivider was missing. Big thanks – DeKekem Jan 25 '17 at 13:49
  • @S.D. it is not in layout file but in your current theme: `@drawable/divider` – pskink Jan 25 '17 at 14:44
  • @pskink Updated. Thanks. – S.D. Jan 27 '17 at 05:50
0

try using this vector as divider line

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="2dp"
android:viewportWidth="24"
android:viewportHeight="2">
<path
    android:pathData="M0,0L100,0.5"
    android:strokeWidth="2"
    android:fillColor="#00000000"
    android:strokeColor="@color/lessons_divider_line_background"/>
Abhinav Chauhan
  • 1,304
  • 1
  • 7
  • 24