AppBarLayout扩展滚动范围

时间:2019-06-11 10:50:54

标签: android

我在滚动时遇到问题,AppBarLayout无法完全滚动到屏幕外。

我如何继续滚动到状态栏高度?

但是,如果我将其设置为根布局(CoordinatorLayout)fitsSystemWIndows = false,则滚动不会出现问题,但是状态栏将变为无Alpha通道。

with a as (
      select a.*,
             row_number() over (order by dbms_random.random) as seqnum
      from a
      order by db
     ),
     b as (
      select b.*, count(*) over () as cnt,
             row_number() over (order by dbms_random.random) as seqnum
      from b
     )
select a.col, b.col
from a join
     b
     on mod(a.seqnum - 1, b.cnt) = b.seqnum - 1;

enter image description here

2 个答案:

答案 0 :(得分:0)

将滚动行为添加到位于AppbarLayout内的工具栏中。

app:layout_scrollFlags="scroll|enterAlways"

编辑:-用以下代码替换CollapsingToolbarLayout的滚动行为。

app:layout_scrollFlags="scroll|exitUntilCollapsed"

答案 1 :(得分:0)

我找到了解决方法。

也许有人帮忙。

  1. 创建包:com.google.android.material.appbar
  2. 创建扩展AppBarLayout.Behavior的类

显示图片

package com.google.android.material.appbar

import ...

class OffsetBehavior(c: Context, a: AttributeSet) : AppBarLayout.Behavior(c, a) 
{
    private var mOffset = 0

    fun setOffset(value: Int) {
        mOffset = value
    }

    // completely copy the method onNestedPreScroll from AppBarLaout.Behavior
    // and add some changes
    override fun onNestedPreScroll(coordinatorLayout: CoordinatorLayout, child: AppBarLayout, target: View, dx: Int, dy: Int, consumed: IntArray, type: Int)
    {
        if (dy != 0) {
            val min: Int
            val max: Int

            // scroll down
            if (dy < 0) {
                min = -(child.totalScrollRange + mOffset)
                max = min + child.downNestedPreScrollRange + mOffset
            }
            // scroll up
            else {
                min = -(child.upNestedPreScrollRange + mOffset)
                max = 0
            }
            if (min != max) {
                consumed[1] = scroll(coordinatorLayout, child, dy, min, max)
            }
        }
        if (child.isLiftOnScroll) {
            child.setLiftedState(child.shouldLift(target))
        }
    }
}

,然后将其设置为XML格式的AppBarLayout

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/search_appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="com.google.android.material.appbar.OffsetBehavior">
    ...
</com.google.android.material.appbar.AppBarLayout>

在活动中:

(1)增添乐趣

// getting statusbar height
// my phone with Android9 = 96px
// my phone with Android7 = 72px
fun getStatusBarHeight(): Int {
    val id = resources.getIdentifier("status_bar_height", "dimen", "android")
    return if (id > 0) {
        resources.getDimensionPixelSize(id)
    } else {
        0
    }
}

(2)inonCreate()

val lp = search_appbar?.layoutParams as? CoordinatorLayout.LayoutParams
val bh = lp?.behavior as? com.google.android.material.appbar.OffsetBehavior

bh?.setOffset(getStatusBarHeight())

结果:

https://i.stack.imgur.com/pNeze.gif