How do I add a dividing line between items in RecyclerView?

Asked 2 years ago, Updated 2 years ago, 108 views

<ListView
    android:id="@+id/activity_home_list_view"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:divider="@android:color/transparent"
    android:dividerHeight="8dp"/>

You can add divider and dividerHeight properties between list viewsYo

<android.support.v7.widget.RecyclerView
    android:id="@+id/activity_home_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"/>

But you don't see that in RecyclerView. Do I just add a custom divider directly to the item view? Is there a better way?

android material-design recyclerview divider

2022-09-22 22:21

1 Answers

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_recycler_view, container, false);

    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
    mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));

    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());

    return rootView;
}

https://gist.github.com/alexfu/0f464fc3742f134ccd1e Please refer to this source and add DividerItemDecoration to addItemDecoration in RecyclerView's onCreateView.


2022-09-22 22:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.