-
-
Save danaimset/abacaa50d746a4537686a08ecc33c1a9 to your computer and use it in GitHub Desktop.
package com.danaimset.widget | |
import android.content.Context | |
import android.support.v4.widget.NestedScrollView | |
import android.support.v7.widget.RecyclerView | |
import android.util.AttributeSet | |
import android.view.View | |
import android.view.ViewGroup | |
open class SmartNestedScrollView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : NestedScrollView(context, attrs, defStyleAttr) { | |
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) | |
constructor(context: Context) : this(context, null) | |
override fun measureChildWithMargins(child: View?, parentWidthMeasureSpec: Int, widthUsed: Int, parentHeightMeasureSpec: Int, heightUsed: Int) { | |
if (findNestedRecyclerView(child) != null) { | |
val lp = child?.layoutParams as ViewGroup.MarginLayoutParams | |
val childHeightMeasureSpec = View.MeasureSpec.makeMeasureSpec( | |
lp.topMargin + lp.bottomMargin, View.MeasureSpec.AT_MOST) | |
child.measure(parentWidthMeasureSpec, childHeightMeasureSpec) | |
} else { | |
super.measureChildWithMargins(child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed) | |
} | |
} | |
private fun findNestedRecyclerView(view: View?): RecyclerView? { | |
when (view) { | |
is RecyclerView -> return view | |
is ViewGroup -> { | |
var index = 0 | |
do { | |
val child = view.getChildAt(index) | |
val recyclerView = findNestedRecyclerView(child) | |
if (recyclerView == null) { | |
index += 1 | |
} else { | |
return recyclerView | |
} | |
} while (index < view.childCount) | |
} | |
else -> return null | |
} | |
return null | |
} | |
} |
Hey there!
Make sure that RecyclerView is keeping it's height as wrap_content and don't constraint it to the bottom of the parent container.
If you need more details, please let me know and I'll share my latest layout with the same issue (Scrollable RecyclerView only)
Cool one. works fine.
<com.awantunai.app.custom.SmartNestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="@id/layoutSkuCategory"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<com.awantunai.app.component.HorizontalSkuList
android:id="@+id/horizontalSkuList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/layoutSkuCategory"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvSku"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/at_medium"
android:orientation="vertical"
android:scrollbars="none"
android:nestedScrollingEnabled="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/horizontalSkuList"
tools:listitem="@layout/item_cart_layout" />
</com.awantunai.app.custom.SmartNestedScrollView>
This is my layout, I still face the same problem of infinite loading
Here is another approach, wich will also allow horizontal RecyclerViews and only does its magic for vertical RecyclerViews: