fondesa / recycler-view-divider Goto Github PK
View Code? Open in Web Editor NEWA library which configures a divider for a RecyclerView.
License: Apache License 2.0
A library which configures a divider for a RecyclerView.
License: Apache License 2.0
Is it possible to hide the vertical divider in a GrideLayoutMananger? I cannot find a way with VisibilityManager.
val dataList = MutableList<Any>(1000) { index ->
A()
}
dataList.add(0, B())
val layoutManager = GridLayoutManager(requireActivity(), 3)
layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return when (dataList.getItem(position)) {
B -> 3
else -> 1
}
}
}
requireActivity().dividerBuilder()
.showFirstDivider()
.showSideDividers()
.size(8.dp)
.asSpace()
.build()
.addTo(recyclerView)
i want to give the gridView an different spacing like Flutter that use mainAxisSpacing and crossAxisSpacing
i know i must use the SizeProvider, but i want to use the position to accumulate it, but it donnt provided
Add please divider to top of first item in LinearLayoutManager like in this gist
https://gist.github.com/lapastillaroja/858caf1a82791b6c1a36
One of my project has design where I need this feature. Maybe this is usefull for other also.
使用说明可以详细一些么,目前提供的使用说明不是很详细。想要使用又看不到。有这么多人收藏肯定是一个好的框架,也很希望可以运用到它。
Support at least GradientDrawable
and ColorDrawable
.
Robolectric since 4.4 supports Q
It would be nice to find a reliable way to cache the Grid
and the Divider
instances used in DividerItemDecoration.getItemOffsets()
and DividerItemDecoration.onDraw()
.
In case, add a method on DividerBuilder
to toggle this feature.
Should it be enabled or disabled by default?
Attached sample project and video to show the issue.
The video shows before and after using the Application class.
For now, the only workaround is to do it manually, or maybe use something like this nice extension function:
fun RecyclerView.setDefaultDividers() {
val context = this.context
context.obtainStyledAttributes(intArrayOf(android.R.attr.listDivider)).use { typedArray ->
typedArray.getDrawable(0)
?.let { RecyclerViewDivider.with(context).hideLastDivider().drawable(it).build().addTo(this) }
}
}
如果在 adapter 中设置 item 的背景色,divider 使用的也是背景色,会导致 item 背景色盖住 divider 的,我猜应该重写 ItemDecoration 的 onDrawOver 方法 而不是 onDraw ?
The size of the horizontal divider will get bigger and bigger when removing items in a RecyclerView with a GridLayoutManager repeatedly.
RecyclerViewDivider version: 3.5.0
A Test Project:
MyApplication.zip
Hello!
Builder for RecyclerViewDivider have context, so you can add method that can make code more clean
RecyclerViewDivider.with(context).color(ContextCompat.getColor(context, R.color.divider)).build()
will be:
RecyclerViewDivider.with(context).colorRes(R.color.divider).build()
Divider now working in RTL while using GridLayoutManager.
cannot resolve method with(),addTo(),removeFrom(). I had included gradle also. Can you please explain what is error.
Hey, how would i go about equal spacing?
I need to use a recyclerview to use the drag/move ItemTouchHelper functionality, but i would like the items to be spaced out equally in the recyclerview (match_parent) - no scrolling?
Is there a setting or how would i do this?
RecyclerViewDivider version:
version:3.3.0
API level:
androidx:1.1.0
ANDROID_BUILD_MIN_SDK_VERSION : 23,
ANDROID_BUILD_TARGET_SDK_VERSION: 29,
ANDROID_COMPILE_SDK_VERSION : 29,
How to reproduce it:
Use GridLayoutManager
with VERTICAL
spanCount
is 3
add the divider as the follwing sample code
as the uppper steps ,it will reporduce.
remove visibilityProvider
method,it works fine.
Sample code:
recycler.addItemDecoration(new DividerBuilder(getContext())
.colorRes(R.color.transparent)
.visibilityProvider(new VisibilityProvider() {
@Override
public boolean isDividerVisible(@NotNull Grid grid, @NotNull Divider divider) {
return true;
}
})
.sizeProvider(new SizeProvider() {
int dp16 = Tool.dimens().dp2px(16);
@Override
public int getDividerSize(@NotNull Grid grid, @NotNull Divider
divider, @NotNull Drawable drawable) {
return dp16;
}
}) . build());
Stack trace:
Paste the stack trace here.
var insetBefore: Int = insetManager.itemInsetBefore(groupCount, groupIndex)
var insetAfter: Int = insetManager.itemInsetAfter(groupCount, groupIndex)
if (spanCount > 1 && (insetBefore > 0 || insetAfter > 0)) {
insetBefore = 0
insetAfter = 0
Log.e(TAG, "the inset won't be applied with a span major than 1.")
}
...
// 1. insetBefore and insetAfter always 0 when spanCount > 2
// 2. insetBefore and insetAfter never used when spanCount == 1
if (orientation == RecyclerView.VERTICAL) {
when {
// LinearLayoutManager or GridLayoutManager with 1 column
spanCount == 1 || spanSize == spanCount -> setRect(0, 0, 0, size)
// first element in the group
lineAccumulatedSpan == spanSize -> setRect(0, 0, halfSize + insetAfter, size)
// last element in the group
lineAccumulatedSpan == spanCount -> setRect(halfSize + insetBefore, 0, 0, size)
// element in the middle
else -> setRect(halfSize + insetBefore, 0, halfSize + insetAfter, size)
}
}
What's the Android API to detect if the item of a StaggeredGridLayoutManager
is shown in the first position or not?
Consider also the span count and the full span items.
E.g.
How does the top line show up? @fondesa
Sometime I need to know index of the cell
It would be useful to specify the defaults of RecyclerViewDivider
programmatically.
Example:
RecyclerViewDivider.Defaults.color(Color.RED)
.size(context.getResources().getDimensionPixelSize(R.dimen.example))
.insetBefore(0)
.insetAfter(0);
// firstMarginInGroup and lastMarginInGroup maybe added to SizeManager
if (orientation == RecyclerView.VERTICAL) {
when {
// LinearLayoutManager or GridLayoutManager with 1 column
spanCount == 1 || spanSize == spanCount -> setRect(0, 0, 0, size)
// first element in the group
lineAccumulatedSpan == spanSize -> setRect(firstMarginInGroup, 0, halfSize, size)
// last element in the group
lineAccumulatedSpan == spanCount -> setRect(halfSize, 0, lastMarginInGroup, size)
// element in the middle
else -> setRect(halfSize, 0, halfSize, size)
}
} else {
when {
// LinearLayoutManager or GridLayoutManager with 1 row
spanCount == 1 || spanSize == spanCount -> setRect(0, 0, size, 0)
// first element in the group
lineAccumulatedSpan == spanSize -> setRect(0, firstMarginInGroup, size, halfSize)
// last element in the group
lineAccumulatedSpan == spanCount -> setRect(0, halfSize, size, lastMarginInGroup)
// element in the middle
else -> setRect(0, halfSize, size, halfSize)
}
}
Are they compatible with Kotlin without breaking the usages of the current APIs?
Are they compatible with Java without breaking the usages of the current APIs?
The interface RecyclerViewDivider.BuilderProvider
should override the theme configuration and be used for dynamic configs.
RecyclerViewDivider version:
v3.1.0
API level:
How to reproduce it:
Sample code:
RecyclerViewDivider.with(rv.getContext())
.insetManager(new DefaultInsetManager())
.hideLastDivider()
.size(size)
.color(ResUtils.getColor(color))
.build().addTo(rv);
object : SizeManager {
override fun itemSize(drawable: Drawable, orientation: Int, groupCount: Int, groupIndex: Int): Int {
// I want to know which item is
}
}
Currently we have to do it on our own, otherwise we get a very white one.
Example of such workaround:
obtainStyledAttributes(intArrayOf(android.R.attr.listDivider)).use { typedArray ->
typedArray.getDrawable(0)?.let { RecyclerViewDivider.with(this).drawable(it).build().addTo(recyclerView) }
}
All deprecated APIs must be removed.
Related to #41
Sometimes extra space available at the top of view item. I have RecyclerView inside RecyclerView. It reproduces in the inner RecyclerView.
RecyclerViewDivider version:
implementation 'com.github.fondesa:recycler-view-divider:3.1.0'
API level:
API level of my emulator 27
How to reproduce it:
I don't know how to reproduce. But the last time I compiled project everything was OK. Than send emulator to background. After about 10 minutes I came back to emulator, than I tried to expand inner recycler view item, than I saw this bug.
Sample code:
This is code of the outer RecyclerView
class GroupAdapter @Inject constructor() : RecyclerView.Adapter<GroupAdapter.GroupViewHolder>() {
val items = ArrayList<Pair<Group, List<Service>>>()
val icons =
arrayListOf(R.drawable.ic_globe_1, R.drawable.ic_envelope_1, R.drawable.ic_outgoing_call)
val viewPool = RecyclerView.RecycledViewPool()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GroupViewHolder {
return GroupViewHolder(
ViewHolderGroupBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
)
}
override fun getItemCount() = items.size
override fun onBindViewHolder(holder: GroupViewHolder, position: Int) {
holder.bind(items[position], icons[position % icons.size])
}
inner class GroupViewHolder(val binding: ViewHolderGroupBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(item: Pair<Group, List<Service>>, icon: Int) {
binding.item = item.first
binding.ivIcon.setImageResource(icon)
val adapter = ServiceAdapter()
adapter.items.addAll(item.second)
val childLayoutManager =
LinearLayoutManager(binding.root.context, LinearLayoutManager.VERTICAL, false)
childLayoutManager.initialPrefetchItemCount = item.second.size
binding.rvServices.layoutManager = childLayoutManager
binding.rvServices.adapter = adapter
binding.rvServices.setRecycledViewPool(viewPool)
binding.root.context.getDrawable(R.drawable.divider_background)?.let {
RecyclerViewDivider.with(binding.root.context)
.drawable(it)
.hideLastDivider()
.build()
.addTo(binding.rvServices)
}
binding.root.setOnClickListener {
item.first.isExpanded = !item.first.isExpanded
notifyItemChanged(adapterPosition)
}
}
}
}
This is code of inner RecyclerView
class ServiceAdapter : RecyclerView.Adapter<ServiceAdapter.ServiceViewHolder>() {
val items = ArrayList<Service>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ServiceViewHolder {
return ServiceViewHolder(
ViewHolderServiceBinding.inflate(
LayoutInflater.from(
parent.context
), parent, false
)
)
}
override fun getItemCount() = items.size
override fun onBindViewHolder(holder: ServiceViewHolder, position: Int) {
holder.bind(items[position])
}
class ServiceViewHolder(val binding: ViewHolderServiceBinding) :
BaseViewHolder<Service>(binding.root) {
override fun bind(item: Service) {
binding.item = item
binding.tvPrice.text = "${item.price} Coin"
}
}
}
// position == -1
// MoreViewItem is last item
spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
val item = adapter.items[position] // ArrayIndexOutOfBoundsException
if (item is MoreViewItem) {
return 2
}
return 1
}
// notify insert
items.add(moreViewItem)
adapter.items = items
adapter.notifyItemInserted(items.lastIndex)
java.lang.ArrayIndexOutOfBoundsException: length=108; index=-1
at java.util.ArrayList.get(ArrayList.java:439)
at cn.soloho.xvideos.view.HomeFragment$onActivityCreated$$inlined$also$lambda$1.getSpanSize(HomeFragment.kt:57)
at android.support.v7.widget.GridLayoutManager$SpanSizeLookup.getSpanGroupIndex(GridLayoutManager.java:971)
at com.fondesa.recyclerviewdivider.extension.LayoutManagerExtensionsKt.getGroupIndex(LayoutManagerExtensions.kt:70)
at com.fondesa.recyclerviewdivider.RecyclerViewDivider.getItemOffsets(RecyclerViewDivider.kt:104)
at android.support.v7.widget.RecyclerView.getItemDecorInsetsForChild(RecyclerView.java:4792)
at android.support.v7.widget.RecyclerView$LayoutManager.calculateItemDecorationsForChild(RecyclerView.java:9320)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:590)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3763)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3527)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
Sometimes the required decoration height is not an integer 。
Can the
DividerBuilder.size(size: Int, sizeUnit: Int = TypedValue.COMPLEX_UNIT_PX)
be adjusted to
DividerBuilder.size(size: Float, sizeUnit: Int = TypedValue.COMPLEX_UNIT_PX)
RecyclerViewDivider version:
v3.4.0
API level:
26,27,28,29,30
Stack trace:
java.lang.IndexOutOfBoundsException: The grid doesn't contain the item at position 48.
at com.fondesa.recyclerviewdivider.DividersAroundCellKt.lineAndCellRelativeIndexesOf(DividersAroundCellKt.java:55)
at com.fondesa.recyclerviewdivider.DividersAroundCellKt.dividersAroundCell(DividersAroundCellKt.java:26)
at com.fondesa.recyclerviewdivider.DividerItemDecoration.drawDividers(DividerItemDecoration.java:113)
at com.fondesa.recyclerviewdivider.DividerItemDecoration.onDraw(DividerItemDecoration.java:103)
at com.fondesa.recyclerviewdivider.BaseDividerItemDecoration.onDraw(BaseDividerItemDecoration.java:105)
at androidx.recyclerview.widget.RecyclerView.onDraw(RecyclerView.java:4494)
at android.view.View.draw(View.java:21618)
at androidx.recyclerview.widget.RecyclerView.draw(RecyclerView.java:4429)
at android.view.View.updateDisplayListIfDirty(View.java:20483)
at android.view.View.draw(View.java:21350)
at android.view.ViewGroup.drawChild(ViewGroup.java:4446)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4205)
at android.view.View.draw(View.java:21621)
at android.view.View.updateDisplayListIfDirty(View.java:20483)
at android.view.View.draw(View.java:21350)
at android.view.ViewGroup.drawChild(ViewGroup.java:4446)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4205)
at android.view.View.draw(View.java:21621)
at android.view.View.updateDisplayListIfDirty(View.java:20483)
at android.view.View.draw(View.java:21350)
at android.view.ViewGroup.drawChild(ViewGroup.java:4446)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4205)
at android.view.View.draw(View.java:21621)
at androidx.viewpager.widget.ViewPager.draw(ViewPager.java:2426)
at android.view.View.updateDisplayListIfDirty(View.java:20483)
at android.view.View.draw(View.java:21350)
at android.view.ViewGroup.drawChild(ViewGroup.java:4446)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4205)
at android.view.View.draw(View.java:21621)
at androidx.core.widget.NestedScrollView.draw(NestedScrollView.java:1998)
at android.view.View.updateDisplayListIfDirty(View.java:20483)
at android.view.View.draw(View.java:21350)
at android.view.ViewGroup.drawChild(ViewGroup.java:4446)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4205)
at android.view.View.updateDisplayListIfDirty(View.java:20474)
at android.view.View.draw(View.java:21350)
at android.view.ViewGroup.drawChild(ViewGroup.java:4446)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4205)
at android.view.View.updateDisplayListIfDirty(View.java:20474)
at android.view.View.draw(View.java:21350)
at android.view.ViewGroup.drawChild(ViewGroup.java:4446)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4205)
at android.view.View.draw(View.java:21621)
at android.view.View.updateDisplayListIfDirty(View.java:20483)
at android.view.View.draw(View.java:21350)
at android.view.ViewGroup.drawChild(ViewGroup.java:4446)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4205)
at android.view.View.updateDisplayListIfDirty(View.java:20474)
at android.view.View.draw(View.java:21350)
at android.view.ViewGroup.drawChild(ViewGroup.java:4446)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4205)
at android.view.View.updateDisplayListIfDirty(View.java:20474)
at android.view.View.draw(View.java:21350)
at android.view.ViewGroup.drawChild(ViewGroup.java:4446)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4205)
at android.view.View.draw(View.java:21621)
at android.view.View.updateDisplayListIfDirty(View.java:20483)
at android.view.View.draw(View.java:21350)
at android.view.ViewGroup.drawChild(ViewGroup.java:4446)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4205)
at android.view.View.updateDisplayListIfDirty(View.java:20474)
at android.view.View.draw(View.java:21350)
at android.view.ViewGroup.drawChild(ViewGroup.java:4446)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4205)
at android.view.View.updateDisplayListIfDirty(View.java:20474)
at android.view.View.draw(View.java:21350)
at android.view.ViewGroup.drawChild(ViewGroup.java:4446)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4205)
at android.view.View.updateDisplayListIfDirty(View.java:20474)
at android.view.View.draw(View.java:21350)
at android.view.ViewGroup.drawChild(ViewGroup.java:4446)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4205)
at android.view.View.updateDisplayListIfDirty(View.java:20474)
at android.view.View.draw(View.java:21350)
at android.view.ViewGroup.drawChild(ViewGroup.java:4446)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4205)
at android.view.View.draw(View.java:21621)
at com.android.internal.policy.DecorView.draw(DecorView.java:964)
at android.view.View.updateDisplayListIfDirty(View.java:20483)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:584)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:590)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:668)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:3840)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3648)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2954)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1857)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8089)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1057)
at android.view.Choreographer.doCallbacks(Choreographer.java:875)
at android.view.Choreographer.doFrame(Choreographer.java:776)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1042)
at android.os.Handler.handleCallback(Handler.java:888)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8178)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
Don't know if the author has plans to support it?
Hide the first line-->hideFirstDivider();
Change the color of the line-->changeDividerColor(int postion,int color);
Can you create a method of removing itemdecoration on pull to refresh? Currently when a recyclerview is refreshed it adds the divider along with the existing one.
Make each item's divider has same size, this is good for use ConstraintLayout's layout_constraintDimensionRatio. Otherwise, the height of each item is inconsistent
such as:
<ConstraintLayout>
<View app:layout_constraintDimensionRatio="1:1" />
</ConstraintLayout>
val left = (lineAccumulatedSpan - 1) * size / spanCount
val right = size - lineAccumulatedSpan * size / spanCount
when {
// LinearLayoutManager or GridLayoutManager with 1 column
spanCount == 1 || spanSize == spanCount -> setRect(0, 0, 0, size)
// first element in the group
lineAccumulatedSpan == spanSize -> setRect(left, 0, right, size)
// last element in the group
lineAccumulatedSpan == spanCount -> setRect(left, 0, right, size)
// element in the middle
else -> setRect(left, 0, right, size)
}
sample:
column: 3,
divider size: 26px
your code: [0-13] [13-13] [13-0]
my code: [0-18] [8-9] [17-0]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.