Skip to content

Commit 7ed4a7a

Browse files
author
xmuSistone
committed
compat vivo detach
1 parent 23efd1f commit 7ed4a7a

File tree

5 files changed

+17
-30
lines changed

5 files changed

+17
-30
lines changed

PersistentRecyclerView.apk

-120 Bytes
Binary file not shown.

app/src/main/java/com/stone/persistent/recyclerview/extensions/ActivityExtensions.kt

+6-16
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package com.stone.persistent.recyclerview.extensions
22

3+
import android.app.Activity
34
import android.graphics.Color
45
import android.os.Build
56
import android.util.DisplayMetrics
67
import android.view.View
78
import android.view.WindowManager
8-
import androidx.fragment.app.FragmentActivity
99

1010
/**
1111
* 沉浸式状态栏
1212
*/
13-
fun FragmentActivity.immerseStatusBar() {
13+
fun Activity.immerseStatusBar() {
1414
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
1515
window.statusBarColor = Color.TRANSPARENT
1616
window.decorView.systemUiVisibility =
@@ -26,25 +26,15 @@ fun FragmentActivity.immerseStatusBar() {
2626
/**
2727
* 获取状态栏高度
2828
*/
29-
fun FragmentActivity.getStatusBarHeight(): Int {
30-
var statusBarHeight = 0
31-
try {
32-
var clz = Class.forName("com.android.internal.R\$dimen")
33-
var obj = clz!!.newInstance()
34-
var field = clz.getField("status_bar_height")
35-
var status_bar_height = Integer.parseInt(field!!.get(obj).toString())
36-
statusBarHeight = resources.getDimensionPixelSize(status_bar_height)
37-
} catch (e: Exception) {
38-
e.printStackTrace()
39-
}
40-
41-
return statusBarHeight
29+
fun Activity.getStatusBarHeight(): Int {
30+
val statusBarHeightId = resources.getIdentifier("status_bar_height", "dimen", "android")
31+
return resources.getDimensionPixelSize(statusBarHeightId)
4232
}
4333

4434
/**
4535
* 获取屏幕宽度
4636
*/
47-
fun FragmentActivity.getScreenWidth(): Int {
37+
fun Activity.getScreenWidth(): Int {
4838
val dm = DisplayMetrics()
4939
windowManager.defaultDisplay.getMetrics(dm)
5040
return dm.widthPixels

app/src/main/java/com/stone/persistent/recyclerview/helper/SyncScrollHelper.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class SyncScrollHelper(mainActivity: MainActivity) {
2121

2222
private val statusBarHeight = mainActivity.getStatusBarHeight()
2323
private val toolbarHeight = mainActivity.dp2px(50f)
24-
private var screenHeight = mainActivity.getScreenWidth()
25-
private var searchBarHeight = mainActivity.dp2px(46f)
24+
private val screenHeight = mainActivity.getScreenWidth()
25+
private val searchBarHeight = mainActivity.dp2px(46f)
2626

2727
private val activity = mainActivity
2828
private val toolBarLayout = mainActivity.main_toolbar

library/src/main/java/com/stone/persistent/recyclerview/library/BaseRecyclerView.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ open class BaseRecyclerView @JvmOverloads constructor(
4343
/**
4444
* 获取垂直方向的速率
4545
*/
46-
fun getVelocityY(): Int = (velocityYField.get(scrollerYObj) as Float).toInt()
46+
fun getVelocityY(): Int = (overScroller.currVelocity).toInt()
4747

4848
/**
4949
* 停止滑动fling

library/src/main/java/com/stone/persistent/recyclerview/library/ChildRecyclerView.kt

+8-11
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,14 @@ class ChildRecyclerView @JvmOverloads constructor(
3535
mTouchSlop = configuration.scaledTouchSlop
3636
}
3737

38-
override fun onScrollStateChanged(state: Int) {
39-
super.onScrollStateChanged(state)
40-
41-
// 是否已经停止scrolling
42-
if (state == SCROLL_STATE_IDLE) {
43-
// 这里是考虑到当整个childRecyclerView被detach之后,及时上报parentRecyclerView
44-
val velocityY = getVelocityY()
45-
if (velocityY < 0 && computeVerticalScrollOffset() == 0) {
46-
parentRecyclerView?.fling(0, velocityY)
47-
}
48-
}
38+
/**
39+
* 当整个childRecyclerView被detach之后,及时上报parentRecyclerView
40+
*/
41+
override fun onDetachedFromWindow() {
42+
super.onDetachedFromWindow()
43+
44+
val velocityY = getVelocityY()
45+
parentRecyclerView?.fling(0, -velocityY)
4946
}
5047

5148
override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {

0 commit comments

Comments
 (0)