简体   繁体   English

自定义布局行为

[英]Custom Layout Behavior

How can I acheive such behavior for bottom bar? 如何为底栏实现这种行为 It works like scroll aware behavior for floating action button but when the recyckerview scrolls to the bottom, then it pulls that panel up (like the last element in the recyclerview is that panel). 它的工作方式类似于浮动操作按钮的滚动感知行为,但是当recyckerview滚动到底部时,它会向上拉动该面板(就像recyclelerview中的最后一个元素就是那个面板)。 I don't think that the developer duplicated those views. 我不认为开发人员重复这些观点。 Maybe it is possible to extend behavior somehow? 也许有可能以某种方式扩展行为?

Here is the code for scroll aware behavior: 以下是滚动感知行为的代码:

class ScrollAwareBehavior : CoordinatorLayout.Behavior<View> {
    constructor() : super()
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout?, child: View?, directTargetChild: View?, target: View?, nestedScrollAxes: Int): Boolean {
        return nestedScrollAxes==ViewCompat.SCROLL_AXIS_VERTICAL
    }

    override fun onNestedScroll(coordinatorLayout: CoordinatorLayout?, child: View?, target: View?, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed)
        if (dyConsumed > 0) {
            val layoutParams = (child?.layoutParams as? CoordinatorLayout.LayoutParams?)?:return
            val fab_bottomMargin = layoutParams.bottomMargin
            child?.animate()?.translationY(child.height.toFloat() + fab_bottomMargin)?.setInterpolator(AccelerateDecelerateInterpolator())?.start()
        } else if (dyConsumed < 0) {
            child?.animate()?.translationY(0f)?.setInterpolator(AccelerateDecelerateInterpolator())?.start()
        }
    }
}

One way of doing that it's to listen the scroll event on the RecyclerView and check the last visible item and check if it's the last one. 这样做的一种方法是在RecyclerView上监听滚动事件并检查最后一个可见项并检查它是否是最后一项。
For that, there is findLastCompletelyVisibleItemPosition() in LinearLayoutManager and getItemCount() in Adapter . 为此,在LinearLayoutManager findLastCompletelyVisibleItemPosition() ,在Adapter getItemCount()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM