Using DataBinding for Custom Views

I am trying to create a custom view:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <FrameLayout
        android:id="@+id/container_fl"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:indeterminateTint="#FFFFFF" />

    </FrameLayout>

</layout>

And Java:

public class ImageSlideshowView extends FrameLayout {
    private ViewImageSlideshowBinding binding;

    public ImageSlideshowView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        init(context);
    }

    public ImageSlideshowView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(context);
    }

    public ImageSlideshowView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        init(context);
    }

    private void init(@NonNull Context context) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        binding = ViewImageSlideshowBinding.inflate(layoutInflater, this, true);
    }
}

This works, but not right.

If I use a tag mergeinstead of FrameLayout without data binding, I will get the following layout (I finally want to get this layout):

- FrameLayout

--- ProgressBar

But I can’t use mergewith data binding, so using the XML above I get the following layout:

- FrameLayout

--- FrameLayout

---- ProgressBar

As you can see, another FrameLayout is being created. How to avoid this?

+4
source share
1 answer

- FrameLayout. FrameLayout ProgressBar.

:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <com.yourcompany.yourapp.ImageSlideshowView
        android:id="@+id/container_fl"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:indeterminateTint="#FFFFFF" />

    </com.yourcompany.yourapp.ImageSlideshowView>

</layout>

, , .

<?xml version="1.0" encoding="utf-8"?>
<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">

    <data>


    </data>

    <com.yourcompany.yourapp.ParentView
        android:id="@+id/parent_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

        <com.yourcompany.yourapp.ImageSlideshowView
            android:id="@+id/slideshow_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </LinearLayout>
    </com.yourcompany.yourapp.ParentView>
</layout>
0

Source: https://habr.com/ru/post/1673266/


All Articles