android - 在自定义 View 上使用 DataBinding

标签 android android-databinding

我正在尝试创建自定义 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>

和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);
    }
}

它有效,但不正确。

如果我在没有数据绑定(bind)的情况下使用 merge 标签而不是 FrameLayout,我将得到以下布局(我最终想要得到 this 布局):

-- FrameLayout

--- ProgressBar

但是我不能将 merge 与数据绑定(bind)一起使用,所以使用上面的 XML 我得到以下布局:

-- FrameLayout

--- FrameLayout

---- ProgressBar

可以看到,又创建了一个FrameLayout。如何避免?

最佳答案

您的自定义 View 是一个 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>

然后,当您将自定义 View 包含在另一个 View 中时,您可以按其类型引用它。

<?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>

关于android - 在自定义 View 上使用 DataBinding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43038780/

相关文章:

android - Galaxy Nexus 上没有显示背景图片(仅)?

java - Google Play 游戏无法在 Android 中进行身份验证

java - 安卓java |更新 View 布局参数时出现卡顿和滞后

java - 如何将 RadioGroup 验证为必填字段?

带有单选按钮的Android双向数据绑定(bind)

android - 无法在 Phonegap 应用程序中获取设备详细信息

android - 如何抑制由未能生成的类引起的代码生成错误?

android - 重新创建 View 后, fragment 从ViewModel中丢失数据

android - 我可以将错误消息绑定(bind)到 TextInputLayout 吗?

android - 如何使用数据绑定(bind)将来自资源的字符串与 XML 中的动态变量结合起来?