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

时间:2016-02-23 09:46:03

标签: 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>

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

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

目前我收到错误消息。这是因为布局标签。

Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class layout

4 个答案:

答案 0 :(得分:3)

您需要添加

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

并在您的自定义视图中

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

答案 1 :(得分:0)

我希望您已经在模块gradle文件中启用了dataBinding。

android {
    ....
    dataBinding {
        enabled = true
    }
}

答案 2 :(得分:0)

未生成MyLayoutBinding,因为您的xml文件不包含数据标记。将数据标记放在布局中并在数据中声明变量后,应生成MyLayoutBinding

答案 3 :(得分:0)

对于遇到同样问题的其他人,请查看:

  • XML文件必须具有布局标记
  • 将根据XML文件名生成绑定类名称。 my_file.xml将导致名称'myFileBinding'
  • 如果找不到绑定类,请尝试重建项目。
相关问题