Created
April 30, 2023 04:22
-
-
Save iamkingalvarado/f6167e57defb12dfe0c5c287975beeb4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 tv.standard.ui.view.custom | |
import android.content.Context | |
import android.graphics.Rect | |
import android.util.AttributeSet | |
import android.view.View | |
import androidx.core.widget.NestedScrollView | |
import androidx.recyclerview.widget.LinearLayoutManager | |
const val TAG = "MyNestedScroll" | |
/** | |
* Solution found here https://stackoverflow.com/questions/52218109/findlastvisibleitemposition-is-returning-wrong-value-if-recyclerview-inside-nest | |
* to deal with a RecyclerView inside NestedScrollView | |
*/ | |
class NestedScrollViewWithRecyclerView : NestedScrollView { | |
private var screenHeight = 0 | |
private var isBottomOfList: IsBottomOfList? = null | |
private var linearLayoutManager: LinearLayoutManager? = null | |
constructor(context: Context) : super(context) { | |
init() | |
} | |
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { | |
init() | |
} | |
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super( | |
context, | |
attrs, | |
defStyleAttr | |
) { | |
init() | |
} | |
private fun init() { | |
screenHeight = context.resources.displayMetrics.heightPixels | |
} | |
override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) { | |
super.onScrollChanged(l, t, oldl, oldt) | |
isBottomOfList?.isBottomOfList(isVisible) | |
} | |
val isVisible: Boolean | |
get() { | |
val childCount = linearLayoutManager?.childCount ?: 0 | |
val view: View = linearLayoutManager?.getChildAt(childCount - 1) ?: return false | |
if (!view.isShown) { | |
return false | |
} | |
val actualPosition = Rect() | |
view.getGlobalVisibleRect(actualPosition) | |
val height1 = view.height | |
val height2 = actualPosition.bottom - actualPosition.top | |
return actualPosition.bottom < screenHeight && height1 == height2 | |
} | |
fun setIsBottomOfList(isBottomOfList: IsBottomOfList?) { | |
this.isBottomOfList = isBottomOfList | |
} | |
fun setLinearLayoutManager(linearLayoutManager: LinearLayoutManager?) { | |
this.linearLayoutManager = linearLayoutManager | |
} | |
interface IsBottomOfList { | |
fun isBottomOfList(isBottom: Boolean) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment