-
-
Save danaimset/abacaa50d746a4537686a08ecc33c1a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
<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