How to add dividers and spaces between items in RecyclerView

Exact power complete point spacing and dividers is important for creating visually interesting and person-affable RecyclerViews. A fine-structured RecyclerView enhances person education, making accusation simpler to digest and navigate. This station offers a blanket usher connected however to adhd dividers and areas betwixt gadgets successful your RecyclerView, masking assorted strategies and champion practices for reaching polished and nonrecreational outcomes.

Knowing RecyclerView Point Ornament

The cardinal to manipulating spacing and dividers successful a RecyclerView lies successful the ItemDecoration people. This almighty people permits you to intercept the drafting procedure of the RecyclerView, giving you granular power complete the quality of areas and dividers betwixt gadgets. By extending this people, you tin specify customized logic for drafting dividers, including margins, and controlling the spacing betwixt gadgets.

Deliberation of ItemDecoration arsenic a bed that sits connected apical of your RecyclerView, permitting you to adhd ocular parts with out modifying the underlying adapter oregon structure. This separation of considerations promotes cleaner codification and simpler care.

Location are respective pre-constructed ItemDecoration courses disposable, similar DividerItemDecoration, which provides a speedy resolution for including elemental dividers. Nevertheless, for much custom-made power, creating your ain customized ItemDecoration is the manner to spell.

Creating a Customized Point Ornament

Creating a customized ItemDecoration includes extending the RecyclerView.ItemDecoration people and overriding circumstantial strategies. The capital strategies to direction connected are getItemOffsets() and onDraw().

getItemOffsets() permits you to adhd spacing about all point by manipulating the outRect parameter. You tin specify antithetic offsets for apical, bottommost, near, and correct sides of all point, enabling versatile power complete spacing.

onDraw() is wherever you gully the existent dividers. Utilizing the Canvas offered, you tin gully strains, shapes, oregon immoderate another ocular component betwixt gadgets. This methodology gives the flexibility to make extremely custom-made dividers.

  • Override getItemOffsets() for spacing.
  • Override onDraw() for drafting dividers.

Implementing Dividers

Fto’s make a elemental divider illustration. Successful onDraw(), we’ll gully a horizontal formation beneath all point, but for the past 1. We’ll usage c.drawLine() for this intent, specifying the commencement and extremity coordinates of the formation.

To adhd abstraction betwixt objects vertically, we’ll make the most of getItemOffsets() and adhd a bottommost offset to all point. This volition efficaciously make the spacing we demand.

// Wrong your customized ItemDecoration people national void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView genitor, @NonNull RecyclerView.Government government) { // ... drafting logic ... } national void getItemOffsets(@NonNull Rect outRect, @NonNull Position position, @NonNull RecyclerView genitor, @NonNull RecyclerView.Government government) { // ... offset logic ... } 

Precocious Customization

Past elemental traces, you tin make much elaborate dividers utilizing customized drawables, shapes, oregon equal by including views arsenic dividers. This flat of customization permits you to accomplish alone ocular kinds and absolutely lucifer your app’s plan.

For illustration, you mightiness make a rounded area divider by utilizing a ShapeDrawable. Oregon, you might adhd a tiny representation arsenic a divider, creating a visually distinctive separation betwixt objects.

  1. Usage customized drawables for precocious dividers.
  2. Research utilizing views arsenic dividers for analyzable designs.
  3. See utilizing libraries for pre-constructed divider kinds.

Retrieve to grip border instances, similar the archetypal and past gadgets, to guarantee accordant spacing and divider quality passim your RecyclerView.

Larn much astir precocious RecyclerView customizations.

Infographic Placeholder: Ocular cooperation of ItemDecoration procedure.

Often Requested Questions

Q: However bash I grip antithetic divider kinds for antithetic point sorts?

A: You tin cheque the point kind inside your ItemDecoration and use antithetic drafting logic oregon offsets primarily based connected the kind.

Efficaciously managing spacing and dividers successful your RecyclerView importantly improves the ocular position and usability of your app. By leveraging the powerfulness of ItemDecoration, you tin make cleanable, fine-structured lists that heighten person education. Experimentation with antithetic methods and customization choices to accomplish the clean equilibrium of signifier and relation successful your RecyclerViews. See exploring additional sources similar the authoritative Android documentation and Stack Overflow for much successful-extent accusation and assemblage activity. Eventually, don’t bury to trial your implementation completely connected assorted surface sizes and units to guarantee a accordant person education. Cheque retired these champion practices for RecyclerView improvement.

Question & Answer :
This is an illustration of however it may person been finished antecedently successful the ListView people, utilizing the divider and dividerHeight parameters:

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

Nevertheless, I don’t seat specified expectation successful the RecyclerView people.

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

Successful that lawsuit, is it fine to specify margins and/oregon adhd a customized divider position straight into a database point’s structure oregon is location a amended manner to accomplish my end?

October 2016 Replace

The interpretation 25.zero.zero of Android Activity Room launched the DividerItemDecoration people:

DividerItemDecoration is a RecyclerView.ItemDecoration that tin beryllium utilized arsenic a divider betwixt objects of a LinearLayoutManager. It helps some HORIZONTAL and VERTICAL orientations.

Utilization:

DividerItemDecoration dividerItemDecoration = fresh DividerItemDecoration(recyclerView.getContext(), layoutManager.getOrientation()); recyclerView.addItemDecoration(dividerItemDecoration); 

Former reply

Any solutions both usage strategies that person since go deprecated, oregon don’t springiness a absolute resolution, truthful I tried to bash a abbreviated, ahead-to-day wrapper-ahead.


Dissimilar ListView, the RecyclerView people doesn’t person immoderate divider-associated parameters. Alternatively, you demand to widen ItemDecoration, a RecyclerView’s interior people:

An ItemDecoration permits the exertion to adhd a particular drafting and structure offset to circumstantial point views from the adapter’s information fit. This tin beryllium utile for drafting dividers betwixt objects, highlights, ocular grouping boundaries and much.

Each ItemDecorations are drawn successful the command they have been added, earlier the point views (successful onDraw()) and last the objects (successful onDrawOver(Canvas, RecyclerView, RecyclerView.Government).

Vertical spacing ItemDecoration

Widen ItemDecoration, adhd a customized constructor which takes abstraction tallness arsenic a parameter and override the getItemOffsets() methodology:

national people VerticalSpaceItemDecoration extends RecyclerView.ItemDecoration { backstage last int verticalSpaceHeight; national VerticalSpaceItemDecoration(int verticalSpaceHeight) { this.verticalSpaceHeight = verticalSpaceHeight; } @Override national void getItemOffsets(Rect outRect, Position position, RecyclerView genitor, RecyclerView.Government government) { outRect.bottommost = verticalSpaceHeight; } } 

If you don’t privation to insert abstraction beneath the past point, adhd the pursuing information:

if (genitor.getChildAdapterPosition(position) != genitor.getAdapter().getItemCount() - 1) { outRect.bottommost = verticalSpaceHeight; } 

Line: you tin besides modify outRect.apical, outRect.near and outRect.correct properties for the desired consequence.

Divider ItemDecoration

Widen ItemDecoration and override the onDraw() technique:

national people DividerItemDecoration extends RecyclerView.ItemDecoration { backstage static last int[] ATTRS = fresh int[]{android.R.attr.listDivider}; backstage Drawable divider; /** * Default divider volition beryllium utilized */ national DividerItemDecoration(Discourse discourse) { last TypedArray styledAttributes = discourse.obtainStyledAttributes(ATTRS); divider = styledAttributes.getDrawable(zero); styledAttributes.recycle(); } /** * Customized divider volition beryllium utilized */ national DividerItemDecoration(Discourse discourse, int resId) { divider = ContextCompat.getDrawable(discourse, resId); } @Override national void onDraw(Canvas c, RecyclerView genitor, RecyclerView.Government government) { int near = genitor.getPaddingLeft(); int correct = genitor.getWidth() - genitor.getPaddingRight(); int childCount = genitor.getChildCount(); for (int i = zero; i < childCount; i++) { Position kid = genitor.getChildAt(i); RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) kid.getLayoutParams(); int apical = kid.getBottom() + params.bottomMargin; int bottommost = apical + divider.getIntrinsicHeight(); divider.setBounds(near, apical, correct, bottommost); divider.gully(c); } } } 

You tin both call the archetypal constructor that makes use of the default Android divider attributes, oregon the 2nd 1 that makes use of your ain drawable, for illustration drawable/divider.xml:

<?xml interpretation="1.zero" encoding="utf-eight"?> <form xmlns:android="http://schemas.android.com/apk/res/android" android:form="rectangle"> <measurement android:tallness="1dp" /> <coagulated android:colour="#ff992900" /> </form> 

Line: if you privation the divider to beryllium drawn complete your gadgets, override the onDrawOver() methodology alternatively.

Utilization

To usage your fresh people, adhd VerticalSpaceItemDecoration oregon DividerSpaceItemDecoration to RecyclerView, for illustration successful your fragment’s onCreateView() methodology:

backstage static last int VERTICAL_ITEM_SPACE = forty eight; backstage RecyclerView recyclerView; backstage LinearLayoutManager linearLayoutManager; @Override national Position onCreateView(LayoutInflater inflater, ViewGroup instrumentality, Bundle savedInstanceState) { Position rootView = inflater.inflate(R.format.fragment_feed, instrumentality, mendacious); recyclerView = (RecyclerView) rootView.findViewById(R.id.fragment_home_recycler_view); linearLayoutManager = fresh LinearLayoutManager(getActivity()); recyclerView.setLayoutManager(linearLayoutManager); //adhd ItemDecoration recyclerView.addItemDecoration(fresh VerticalSpaceItemDecoration(VERTICAL_ITEM_SPACE)); //oregon recyclerView.addItemDecoration(fresh DividerItemDecoration(getActivity())); //oregon recyclerView.addItemDecoration( fresh DividerItemDecoration(getActivity(), R.drawable.divider)); recyclerView.setAdapter(...); instrument rootView; } 

Location’s besides Lucas Rocha’s room which is expected to simplify the point ornament procedure. I haven’t tried it although.

Amongst its options are:

  • A postulation of banal point decorations together with:
  • Point spacing Horizontal/vertical dividers.
  • Database point