Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom ScrollingViewBehavior doesn't load

I wrote a custom ScrollingViewBahavior:

package app.library.util.view;

//***

public class FillParentViewBehavior extends AppBarLayout.ScrollingViewBehavior {

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child,
                                          View dependency) {
        super.onDependentViewChanged(parent, child, dependency);

        resizeChildAsNeeded(child, dependency);

        return false;
    }
}

Next, I declared a class path in resources (by analogy with appcompat declaration):

<resources>
    <string name="appbar_fill_parent_view_behavior" translatable="false">app.library.util.view.FillParentViewBehavior</string>
</resources>

And set it in my layout:

<layout
    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.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="app.library.activity.MainActivity">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <!-- Inner views -->

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

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_fill_parent_view_behavior"/>

    </android.support.design.widget.CoordinatorLayout>
</layout>

But my app became to crash with a log output:

java.lang.RuntimeException: Unable to start activity ComponentInfo{app/app.library.activity.MainActivity}: java.lang.RuntimeException: Could not inflate Behavior subclass app.library.util.view.FillParentViewBehavior

I tried to rebuild a project but it didn't help me. It's strange because behaviours from the AppCompat library work perfect.
What do I doing wrong?

like image 370
Шах Avatar asked Apr 02 '26 07:04

Шах


1 Answers

Because you are trying to create this class from code, you need to add one more constructor(generally, it's true for any view in XML you create from code).

So, your class should look like:

public class FillParentViewBehavior extends AppBarLayout.ScrollingViewBehavior {

    public FillParentViewBehavior() {
        super();
    }
    
    public FillParentViewBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child,
                                          View dependency) {
        super.onDependentViewChanged(parent, child, dependency);

        resizeChildAsNeeded(child, dependency);

        return false;
    }
}
like image 65
Mr.Stan Avatar answered Apr 03 '26 21:04

Mr.Stan