Android 自定义视图中的数据绑定

San*_*isy 1 java android android-layout android-fragments

我创建了一个自定义布局,如下所示。我想用这个布局执行数据绑定。我该如何执行此任务。

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">
        <TextView
            android:id="@+id/no_internetConnection_Text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_toRightOf="@+id/icon_noInternet_connection"
            android:text="@string/no_Internet_Connection_text" />
    </LinearLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)

这是现有的代码。如何用数据绑定代码替换现有代码。

public class NetworkConnectionCheck {
    private  Context _context;

    public NetworkConnectionCheck(Context _context) {
        this._context = _context;
    }

    public void CustomToastShow() {
        LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate( R.layout.customtoast, null );
        TextView text = (TextView) view.findViewById(R.id.no_internetConnection_Text);  
        ImageView imageView = (ImageView) view.findViewById(R.id.icon_noInternet_connection);
        Toast toast = new Toast(_context);
        toast.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(view);
        toast.show();
    }
}
Run Code Online (Sandbox Code Playgroud)

目前我收到一个错误。这是因为 Layout 标签。

Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class layout
Run Code Online (Sandbox Code Playgroud)

小智 6

你需要添加

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <merge>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">
        <TextView
            android:id="@+id/no_internetConnection_Text"
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_toRightOf="@+id/icon_noInternet_connection"
        android:text="@string/no_Internet_Connection_text" />
</LinearLayout>
<merge/>
</layout>
Run Code Online (Sandbox Code Playgroud)

在您的自定义视图中

public class NetworkConnectionCheck {
    private  Context context;

    public NetworkConnectionCheck(Context context) {
        super(context);
        init();
    }
    public NetworkConnectionCheck(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public NetworkConnectionCheck(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void CustomToastShow() {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.customtoast, this, true);
        //do what you need to do with the binding
    }
}
Run Code Online (Sandbox Code Playgroud)