简体   繁体   中英

Hide some item dividers in recyclerview using DividerItemDecoration

I have a recycler view with multiple item types and i want to hide selected dividers (from image). I'm currently using DividerItemDecoration for both horizontal and vertical decoration.

DividerItemDecoration verticalDecoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.HORIZONTAL);
verticalDecoration.setDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.divider_2dp));
DividerItemDecoration horizontalDecoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
horizontalDecoration.setDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.divider_2dp));

mRecyclerView.addItemDecoration(verticalDecoration);
mRecyclerView.addItemDecoration(horizontalDecoration);

在此处输入图片说明

As Micha commented on your answer, you should remove the handling of your dividers in the level of your Class and add it in the Adapter views. Add to your viewholder xml a view:

<View
    android:id="@+id/divider"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/light_grey"/>

either horizontal or vertical per your liking and control its visibility through a condition in the adapter:

View tipsDivider = itemView.findViewById(R.id.divider);
myViewHolder.divider.setVisibility(View.GONE);

In particular to your question, do NOT include a divider for the outer edge of your rows and also hide it when you have a title ("ADVANCED") or when it's about the element at the position == arraylist.size() - 1;

Hope that helps whoever lands at this question as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM