其他分享
首页 > 其他分享> > android – AppBarLayout将我的自定义视图与layout_behavior重叠

android – AppBarLayout将我的自定义视图与layout_behavior重叠

作者:互联网

我有一个自定义标头,其自定义行为与协调器布局交互.此标头取决于包含collapsingToolbarLayout和工具栏的appBarLayout.当工具栏布局折叠时,自定义标题调整其属性并按照我想要的方式定位,但第二个我达到布局的最小高度,appBarLayout重叠自定义标题,直到我开始扩展它才能看到它.

enter image description here

enter image description here

这是布局的代码:

<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/toolbar_layout"
    android:layout_width="match_parent"
    android:layout_height="@dimen/mk_appbar_height">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_collapse"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minHeight="@dimen/mt_toolbar_height"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:titleEnabled="false">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            style="@style/ToolbarStyle"
            android:layout_width="match_parent"
            android:layout_height="@dimen/mt_toolbar_height"
            app:layout_collapseMode="pin"
            />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<!-- This is a layout intended for containing MonkeyChatFragment and/or MonkeyConversationsFragment
RelativeLayout has an issue that doesn't render the RecyclerView with the whole size of its
container linear layout.
The only viable solution is FrameLayout -->
<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<FrameLayout
    android:id="@+id/viewStatus"
    android:layout_width="match_parent"
    android:layout_height="@dimen/status_height"
    android:layout_marginTop="@dimen/status_inverse_height"
    android:background="@color/mk_status_connected"/>

<com.criptext.monkeykitui.toolbar.HeaderView
    android:id="@+id/custom_toolbar"
    layout="@layout/custom_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="56dp"
    app:layout_behavior="com.criptext.monkeykitui.toolbar.HeaderViewBehavior"
    app:behavior_overlapTop="64dp"
    /></android.support.design.widget.CoordinatorLayout>

这是layout_behavior的代码:

class HeaderViewBehavior(context: Context, attrs: AttributeSet? = null) : CoordinatorLayout.Behavior<HeaderView>(context, attrs){

private val MIN_AVATAR_PERCENTAGE_SIZE = 0.3f
private val EXTRA_FINAL_AVATAR_PADDING = 80

private val TAG = "behavior"
private val mContext: Context = context

private val mCustomFinalHeight: Float = 0.toFloat()

private var mStartToolbarPosition: Float = 0.toFloat()
private var mStartYPosition: Int = 0
private var mFinalYPosition: Int = 0
private var mStartHeight: Int = 0
private var mfontSize: Float = 20.toFloat()
private var mChangeBehaviorPoint: Float = 0.toFloat()

override fun layoutDependsOn(parent: CoordinatorLayout?, child: HeaderView?, dependency: View?): Boolean {
    var hello = (dependency is AppBarLayout)
    return hello
}

override fun onDependentViewChanged(parent: CoordinatorLayout, child: HeaderView, dependency: View): Boolean {
    maybeInitProperties(child, dependency)

    val maxScrollDistance = - mContext.resources.getDimension(R.dimen.mk_header_scroll)
    val expandedPercentageFactor = dependency.y / maxScrollDistance
    Log.d("TEST", dependency.y.toString())
    Log.d("TEST", maxScrollDistance.toString())


    if (expandedPercentageFactor < mChangeBehaviorPoint) {
        val heightFactor = (mChangeBehaviorPoint - expandedPercentageFactor) / mChangeBehaviorPoint

        val distanceYToSubtract = (mStartYPosition - mFinalYPosition) * (1f - expandedPercentageFactor) + child.getHeight() / 2

        child.setY(mStartYPosition - distanceYToSubtract)

        val heightToSubtract = (mStartHeight - mCustomFinalHeight) * heightFactor

        val lp = child.layoutParams as CoordinatorLayout.LayoutParams

        child.layoutParams = lp
    } else {
        val distanceYToSubtract = (mStartYPosition - mFinalYPosition) * (1f - expandedPercentageFactor)

        child.setY(mStartYPosition - distanceYToSubtract)

        if(mStartYPosition - distanceYToSubtract < mStartYPosition){
            child.setY(mStartYPosition.toFloat())
        }else if(mStartYPosition - distanceYToSubtract > mFinalYPosition){
            child.setY(mFinalYPosition.toFloat())
        }

        child.title.textSize = mfontSize - (mfontSize - 25) * (1f - expandedPercentageFactor)
        child.subtitle.textSize = 15 - (15 - 20) * (1f - expandedPercentageFactor)

        child.imageView.layoutParams.height = (126 - (126 - 226) * (1f - expandedPercentageFactor)).toInt()
        child.imageView.layoutParams.width = (126 - (126 - 226) * (1f - expandedPercentageFactor)).toInt()

    }
    return true
}

private fun maybeInitProperties(child: HeaderView, dependency: View) {
    if (mStartYPosition === 0)
        mStartYPosition = 0

    if (mFinalYPosition === 0)
        mFinalYPosition = mContext.resources.getDimension(R.dimen.mk_header_scroll).toInt()

    if (mStartHeight === 0)
        mStartHeight = child.getHeight()

    if (mStartToolbarPosition === 0.toFloat())
        mStartToolbarPosition = dependency.y

    if (mChangeBehaviorPoint === 0.toFloat())
        mChangeBehaviorPoint = (child.height - mCustomFinalHeight) / (2f * (mStartYPosition - mFinalYPosition))
}
}

解决方法:

为了防万一其他人正在寻找相同的答案,我可以说我已经在另一个SO问题中找到了解决方案,在浪费了6个小时后尝试各种解决方案无济于事.

为了使View在折叠后可视地覆盖AppBarLayout,您需要将android:elevation =“8dp”(或更高值)属性添加到覆盖的View中.

因此,需要更改的问题中的代码部分是:

<com.criptext.monkeykitui.toolbar.HeaderView
    android:id="@+id/custom_toolbar"
    layout="@layout/custom_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="56dp"
    app:layout_behavior="com.criptext.monkeykitui.toolbar.HeaderViewBehavior"
    app:behavior_overlapTop="64dp"
/>

至:

<com.criptext.monkeykitui.toolbar.HeaderView
    android:id="@+id/custom_toolbar"
    layout="@layout/custom_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="56dp"
    app:layout_behavior="com.criptext.monkeykitui.toolbar.HeaderViewBehavior"
    app:behavior_overlapTop="64dp"
    android:elevation="8dp"
/>

所有学分都在@kris larson的答案:CoordinatorLayout custom behavior with AppBarLayout

标签:android,android-appbarlayout
来源: https://codeday.me/bug/20190711/1429552.html