我正在使用android数据绑定并且还使用Dragger问题是当我这样做时

MyLayoutbinding = DataBindingUtil.setContentView(this, R.layout.my_Layout);


我得到了错误

Caused by: java.lang.RuntimeException: view tag isn't correct on view:null
   at com.rkmax.myapp.databinding.MyLayoutBinding.bind(MyLayoutBinding.java:191)
   at android.databinding.DataBinderMapper.getDataBinder(DataBinderMapper.java:15)
   at android.databinding.DataBindingUtil.bind(DataBindingUtil.java:185)
   at android.databinding.DataBindingUtil.bindToAddedViews(DataBindingUtil.java:299)
   at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:279)
   at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:261)
   at com.rkmax.myapp.MyActivity.onCreate(MyActivity.java:26)


my_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <import type="android.graphics.drawable.Drawable" />
        <variable name="indicatorBg" type="Drawable" />
        <variable name="categoryName" type="String" />
        <variable name="progress" type="int" />
    </data>

    <RelativeLayout android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_blackboard">

        <ProgressBar
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="match_parent"
                    android:layout_height="15.3dp"
                    android:id="@+id/progressBar2"
                    android:max="10"
                    android:progress="@{progress}" />


    </RelativeLayout>
</layout>

最佳答案

经过大量的试验和错误,我所做的是将DraggerActivity源复制到并创建自己的DraggerActivity,然后添加以下方法

private void configViews(View root) {
    this.draggerPanel.addViewOnDrag(root);
    if(this.shadowResID == -1) {
        this.shadowResID = com.github.ppamorim.dragger.R.layout.layout_shadow;
    }

    this.draggerPanel.addViewOnShadow(this.inflateLayout(this.shadowResID));
    super.setContentView(this.draggerPanel);
}


public void setContentView(View root) {
    this.configDraggerView();
    this.configViews(root);
    super.setContentView(this.draggerPanel);
}


然后在onCreate方法上

View rootView = getLayoutInflater().inflate(R.layout.my_Layout, null, false);
mBinding = DataBindingUtil.bind(rootView);

setContentView(rootView);


这样,我就可以将数据绑定到我的DraggerActivity

10-06 07:10