NestedScrollView is modeled after the H5 anchor implementation

Original link: http://i.lckiss.com/?p=8115

For some reasons, it is necessary to imitate an H5 anchor point effect natively. Unfortunately, after an iteration, the effect is cut off again, but the implementation is still recorded.

Because the linkage code of tab click and pager sliding is not elegant, it is necessary to deal with those jitter problems, which are not a problem except that the code is not beautiful. It mainly records the search of View in ScrollView and the positioning of View according to ID. Of course, these are also network It can be copied on the ready-made.

 fun NestedScrollView.findNearestView(ids: Array<Int>): Int {     val parentLocation = IntArray(2)     getLocationOnScreen(parentLocation)     val parentY = parentLocation[1]     var pos = 0     for (index in ids.indices) {         val viewId = ids[index]         val moveToView = findViewById<View>(viewId)         val viewLocation = IntArray(2)         moveToView.getLocationOnScreen(viewLocation)         val childY = viewLocation[1]         if (parentY > childY) {             pos = index         } else {             break         }     }     return pos }
 fun NestedScrollView.scrollToView(viewId: Int, offset: Int) {     val moveToView = findViewById<View>(viewId)     moveToView ?: return     val parentLocation = IntArray(2)     getLocationOnScreen(parentLocation)     val viewLocation = IntArray(2)     moveToView.getLocationOnScreen(viewLocation)     val moveViewY = viewLocation[1] - parentLocation[1]     val needScrollY = (moveViewY - offset)     if (moveViewY == 0) return     smoothScrollBy(0, needScrollY) }

Then the problem that the head cannot slide in the CoordinatorLayout and AppBarLayout is mainly solved by this:

 /**  * Solve the problem that the head cannot slide after the bottom slide of AppBarLayout*/ private fun updateAppBarStatus() {     val params: CoordinatorLayout.LayoutParams =         mBinding.appBarLayout.layoutParams as CoordinatorLayout.LayoutParams     val behavior = params.behavior as AppBarLayout.Behavior     behavior.setDragCallback(object : AppBarLayout.Behavior.DragCallback() {         override fun canDrag(@NonNull appBarLayout: AppBarLayout): Boolean = true     }) }

After scrolling, call updateAppBarStatus once to update the status.

These things also seem relatively primitive, and there are not many occasions to use them now, followed by more and more complex multi-state coordination and element sharing pages, but there is no such demand yet.

above.

Reference: https://github.com/taixiang/tabScroll

This article is reprinted from: http://i.lckiss.com/?p=8115
This site is for inclusion only, and the copyright belongs to the original author.