【问题标题】:Android data-binding not working when using inflate() of binding class使用绑定类的 inflate() 时,Android 数据绑定不起作用
【发布时间】:2016-11-03 16:10:35
【问题描述】:

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

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

任何想法为什么这不起作用?

【问题讨论】:

  • 因为你的字段是私有的
  • @Beloo 所有字段都有公共吸气剂......抱歉没有附上该代码以保持简短,但就像我在问题中提到的那样:)
  • 只是补充一下,布局也很好,因为它在使用常量字符串而不是 android:text 中的绑定数据时显示

标签: java android android-databinding


【解决方案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);
}

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

【讨论】:

  • 非常感谢,从开发人员指南中我没有意识到所有这些 API 都存在!
猜你喜欢
  • 2016-08-01
  • 2018-03-22
  • 1970-01-01
  • 1970-01-01
  • 2021-02-22
  • 2016-06-23
  • 1970-01-01
  • 2016-12-11
  • 1970-01-01
相关资源
最近更新 更多