Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

android - Could not inflate Behavior subclass custom class

I'm trying to implement this inside of my project: https://github.com/SergeyBurlaka/CollapsingAvatarToolbarSample

In the xml file of my main fragment i have

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/cl_root_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/top_head_height"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

When i try to lunch the app, the app crash with this log

android.view.InflateException: Binary XML file line #10 in it.archeometra.q8.quaser.debug:layout/fragment_main_test: Could not inflate Behavior subclass it.archeometra.q8.quaser.debug.utils.FlingBehavior
    Caused by: java.lang.RuntimeException: Could not inflate Behavior subclass it.archeometra.q8.quaser.debug.utils.FlingBehavior

This is the code of FlingBehavior class

class FlingBehavior : AppBarLayout.Behavior() {


    companion object {
        private val TOP_CHILD_FLING_THRESHOLD = 3
    }

    private var isPositive = false

    override fun onNestedFling(
        coordinatorLayout: CoordinatorLayout,
        child: AppBarLayout,
        target: View,
        velocityX: Float,
        velocityY: Float,
        consumed: Boolean
    ): Boolean {
        var velocityY = velocityY
        var consumed = consumed

        if (velocityY > 0 && !isPositive || velocityY < 0 && isPositive) {
            velocityY *= -1
        }
        if (target is RecyclerView && velocityY < 0) {
            val recyclerView = target
            val firstChild = recyclerView.getChildAt(0)
            val childAdapterPosition = recyclerView.getChildAdapterPosition(firstChild)
            consumed = childAdapterPosition > TOP_CHILD_FLING_THRESHOLD
        }
        return super
            .onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed)
    }

    override fun onNestedPreScroll(
        coordinatorLayout: CoordinatorLayout,
        child: AppBarLayout,
        target: View,
        dx: Int,
        dy: Int,
        consumed: IntArray,
        type: Int
    ) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type)
        isPositive = dy > 0
    }
}

What i'am doing wrong?

EDIT: i also tried to use the exact FlingBehavior class from the github project, but it crashes anyway.

question from:https://stackoverflow.com/questions/66066943/could-not-inflate-behavior-subclass-custom-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...