Android 使用绑定的 inflate() 时数据绑定不起作用 class
Android data-binding not working when using inflate() of binding class
我是数据绑定的新手。我有一个布局文件如下:
<?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 中使用数据绑定。我没有使用第一种方法,因为我将相关布局扩展到框架布局中,该框架布局是抽象 class 中 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);
}
父级 class 中的 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);
}
代码编译正常,运行时也没有日志消息。
实体 class 是(后跟所有属性的 public 吸气剂)
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;
}
知道为什么这不起作用吗?
问题是您在 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);
}
然后使用returnViewDataBinding设置数据:
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);
}
我总是更喜欢前者而不是后者,因为它让绑定发生在尽可能靠近 inflation 的地方。
我是数据绑定的新手。我有一个布局文件如下:
<?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 中使用数据绑定。我没有使用第一种方法,因为我将相关布局扩展到框架布局中,该框架布局是抽象 class 中 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);
}
父级 class 中的 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);
}
代码编译正常,运行时也没有日志消息。 实体 class 是(后跟所有属性的 public 吸气剂)
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;
}
知道为什么这不起作用吗?
问题是您在 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);
}
然后使用returnViewDataBinding设置数据:
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);
}
我总是更喜欢前者而不是后者,因为它让绑定发生在尽可能靠近 inflation 的地方。