使用绑定类

时间:2016-11-03 16:10:36

标签: java android android-databinding

我是数据绑定的新手。我有一个布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="userProfile" type="com.demo.entity.UserProfile"/>
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp">
        <TextView
            android:id="@+id/username_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:textSize="20sp"
            android:textStyle="bold"
            android:text="@{userProfile.username}" />
    </RelativeLayout>
</layout>

我正在尝试按照文档的第二种方法here在android中使用数据绑定。我没有使用第一种方法,因为我将相关的布局膨胀为一个框架布局,它是抽象类中DrawerLayout的一部分,因此可以跨活动使用相同的导航抽屉[我打算避免复杂的嵌套片段导致到]。

因此我的代码如下所示:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    inflateActivityViewInFrame(R.layout.activity_profile, R.string.action_profile);
    initialiseData();
}

private void initialiseData() {
    ActivityProfileBinding binding = ActivityProfileBinding.inflate(getLayoutInflater());
    UserProfile userProfile = new UserProfile("Croaking Tiger Riding Dragon", 8, 16, 32);
    binding.setUserProfile(userProfile);
}

父类中的inflateActivityViewInFrame()方法仅执行此操作:

protected void inflateActivityViewInFrame(int layoutId, int titleStringId) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(layoutId, mFrameLayout, false);
    mFrameLayout.addView(view);

    setupActionBarAndDrawerToggle(titleStringId);
}

代码编译正常,运行时也没有日志消息。 实体类是(后跟所有属性的公共getter)

public class UserProfile {
    private final String username;
    private final int numContributions;
    private final int numBookmarks;
    private final int numLikes;

    public UserProfile(String username, int numContributions, int numLikes, int numBookmarks) {
        this.username = username;
        this.numContributions = numContributions;
        this.numBookmarks = numBookmarks;
        this.numLikes = numLikes;
    }

有什么想法,为什么这不起作用?

1 个答案:

答案 0 :(得分:1)

问题是,您在inflateActivityViewInFrame中对布局进行了充气,​​然后在initialiseData中再次对其进行充气(未附加)。你可以选择做其中一个。要么:

protected <T extends ViewDataBinding> T inflateActivityViewInFrame(
        int layoutId, int titleStringId) {
  LayoutInflater inflater = (LayoutInflater)
      getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  T binding = DataBindingUtil.inflate(inflater, layoutId, mFrameLayout, true);
  setupActionBarAndDrawerToggle(titleStringId);
}

然后使用返回ViewDataBinding来设置数据:

ActivityProfileBinding binding = 
    inflateActivityViewInFrame(R.layout.activity_profile, R.string.action_profile); 
UserProfile userProfile = new UserProfile("Croaking Tiger Riding Dragon", 8, 16, 32);
binding.setUserProfile(userProfile);

或者,你可以绑定已经膨胀的布局。

protected View inflateActivityViewInFrame(int layoutId, int titleStringId) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(layoutId, mFrameLayout, false);
    mFrameLayout.addView(view);

    setupActionBarAndDrawerToggle(titleStringId);
    return view;
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View view = inflateActivityViewInFrame(
        R.layout.activity_profile, R.string.action_profile);
    initialiseData(view);
}

private void initialiseData(View view) {
    ActivityProfileBinding binding = ActivityProfileBinding.bind(view);
    UserProfile userProfile = new UserProfile("Croaking Tiger Riding Dragon", 8, 16, 32);
    binding.setUserProfile(userProfile);
}

我总是更喜欢前者和后者,因为它让绑定尽可能接近通货膨胀。