Android 内部数据绑定 class 未更新 TextView
Android databinding on inner class not updating TextView
应用程序运行但 TextView
没有得到更新这里是相关代码。
activity_picker_dashboard.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Top header -->
<include layout="@layout/layout_header" /> This layout has databinding
<!-- DrawerLayout -->
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/gray"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar">
.....
这里是layout_header.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="profilePayload"
type="myms.models.ProfileResponse.Payload"/>
</data>
<Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<android.support.constraint.ConstraintLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="180dp"
android:layout_height="60dp"
android:adjustViewBounds="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:src="@drawable/logo_color"/>
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="@{profilePayload.firstName}" Here it is
android:textColor="@color/red"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/role"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
...............
</Toolbar>
</layout>
这是我的模型ProfileResponse.java
public class ProfileResponse
{
public Payload getPayLoad() {
return payLoad;
}
@SerializedName("Payload")
private Payload payLoad;
public static class Payload
{
public String getProfileId() {
return profileId;
}
public String getUserId() {
return userId;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
@SerializedName("ProfileId")
private String profileId;
@SerializedName("UserId")
private String userId;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@SerializedName("FirstName")
private String firstName;
@SerializedName("LastName")
private String lastName;
}
}
最后是我的 activity
public class PickerDashboardActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picker_dashboard);
LayoutHeaderBinding binding = LayoutHeaderBinding.inflate(getLayoutInflater());
ProfileResponse.Payload profilePayload = new ProfileResponse.Payload();
profilePayload.setFirstName("Test");
binding.setProfilePayload(profilePayload);
ButterKnife.bind(this);
}
请提出解决方案。我已经尝试了一段时间了。
P.S: 我也在用黄油刀
问题在于您如何将数据绑定布局与常规布局混合。
如果您想从常规布局中包含数据绑定布局,您需要找到该布局的根视图并对其调用 bind()
。也许是这样的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picker_dashboard);
View bindingRoot = findViewById(R.id.toolbar);
LayoutHeaderBinding binding = LayoutHeaderBinding.bind(bindingRoot);
ProfileResponse.Payload profilePayload = new ProfileResponse.Payload();
profilePayload.setFirstName("Test");
binding.setProfilePayload(profilePayload);
}
但是,最好让您的 Activity 布局成为数据绑定布局,这样您就不必做那些额外的工作了:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="profilePayload"
type="myms.models.ProfileResponse.Payload"/>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Top header -->
<include layout="@layout/layout_header" app:profilePayload="@{profilePayload}" />
<!-- DrawerLayout -->
</android.support.constraint.ConstraintLayout>
</layout>
然后你的绑定代码更简单,更不容易出错:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityPickerDashboardBinding binding =
DataBindingUtil.setContentView(this, R.layout.activity_picker_dashboard);
ProfileResponse.Payload profilePayload = new ProfileResponse.Payload();
profilePayload.setFirstName("Test");
binding.setProfilePayload(profilePayload);
}
另一方面,我认为 Android 数据绑定和 butterknife 在功能上有很大的重叠,我建议选择其中之一,但不要同时选择两者。
应用程序运行但 TextView
没有得到更新这里是相关代码。
activity_picker_dashboard.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Top header -->
<include layout="@layout/layout_header" /> This layout has databinding
<!-- DrawerLayout -->
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/gray"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar">
.....
这里是layout_header.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="profilePayload"
type="myms.models.ProfileResponse.Payload"/>
</data>
<Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<android.support.constraint.ConstraintLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="180dp"
android:layout_height="60dp"
android:adjustViewBounds="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:src="@drawable/logo_color"/>
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="@{profilePayload.firstName}" Here it is
android:textColor="@color/red"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/role"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
...............
</Toolbar>
</layout>
这是我的模型ProfileResponse.java
public class ProfileResponse
{
public Payload getPayLoad() {
return payLoad;
}
@SerializedName("Payload")
private Payload payLoad;
public static class Payload
{
public String getProfileId() {
return profileId;
}
public String getUserId() {
return userId;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
@SerializedName("ProfileId")
private String profileId;
@SerializedName("UserId")
private String userId;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@SerializedName("FirstName")
private String firstName;
@SerializedName("LastName")
private String lastName;
}
}
最后是我的 activity
public class PickerDashboardActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picker_dashboard);
LayoutHeaderBinding binding = LayoutHeaderBinding.inflate(getLayoutInflater());
ProfileResponse.Payload profilePayload = new ProfileResponse.Payload();
profilePayload.setFirstName("Test");
binding.setProfilePayload(profilePayload);
ButterKnife.bind(this);
}
请提出解决方案。我已经尝试了一段时间了。 P.S: 我也在用黄油刀
问题在于您如何将数据绑定布局与常规布局混合。
如果您想从常规布局中包含数据绑定布局,您需要找到该布局的根视图并对其调用 bind()
。也许是这样的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picker_dashboard);
View bindingRoot = findViewById(R.id.toolbar);
LayoutHeaderBinding binding = LayoutHeaderBinding.bind(bindingRoot);
ProfileResponse.Payload profilePayload = new ProfileResponse.Payload();
profilePayload.setFirstName("Test");
binding.setProfilePayload(profilePayload);
}
但是,最好让您的 Activity 布局成为数据绑定布局,这样您就不必做那些额外的工作了:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="profilePayload"
type="myms.models.ProfileResponse.Payload"/>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Top header -->
<include layout="@layout/layout_header" app:profilePayload="@{profilePayload}" />
<!-- DrawerLayout -->
</android.support.constraint.ConstraintLayout>
</layout>
然后你的绑定代码更简单,更不容易出错:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityPickerDashboardBinding binding =
DataBindingUtil.setContentView(this, R.layout.activity_picker_dashboard);
ProfileResponse.Payload profilePayload = new ProfileResponse.Payload();
profilePayload.setFirstName("Test");
binding.setProfilePayload(profilePayload);
}
另一方面,我认为 Android 数据绑定和 butterknife 在功能上有很大的重叠,我建议选择其中之一,但不要同时选择两者。