java - 使用 notifyPropertyChanged(BR.xx) 时 DataBinding 不更新 TextView
问题描述
我有一个用于从 XML 调用的函数的工作数据绑定(即“@{user.mUCL}”)。但是当我在任何方法或任何地方使用 notifyPropertyChanged(BR.mUCL) 新值没有得到更新时,我在下面提供了一个示例问题,其中 for-loop 使用值更新了 Text1,但其他 Text2 无法使用相同的值更新“不是-工作”。
Java片段
public class FragmentTwo extends Fragment {
Update_observable updateObservable;
TextView fragment_quote_open_val;
public FragmentTwo() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Fragment2Binding fragmentTwoBinding = DataBindingUtil.inflate(inflater,R.layout.fragment2,null,false);
View view = fragmentTwoBinding.getRoot();
updateObservable = new Update_observable();
fragmentTwoBinding.setUser(updateObservable);
fragment_quote_open_val=(TextView)view.findViewById(R.id.fragment_quote_open_val);
bindingLoop();
return view;
}
@BindingAdapter({"mUCL"})
public static void runMe(TextView view, String message) {
if (message != null)
view.setText(message);
}
public void bindingLoop(){
final int[] k = {0};
final Update_observable ss=new Update_observable();
final Handler handler = new Handler();
final int delay = 3000; //milliseconds
handler.postDelayed(new Runnable(){
public void run(){
//do something
k[0]++;
fragment_quote_open_val.setText(String.valueOf(k[0]));
ss.setmUCL(String.valueOf(k[0]));
handler.postDelayed(this, delay);
}
}, delay);
}
}
BaseObservable
public class Update_observable extends BaseObservable {
public String mUCL= "not-worked";
@Bindable
public String getmUCL() {
return this.mUCL;
}
public void setmUCL(String mUCL) {
this.mUCL = mUCL;
notifyPropertyChanged(BR.mUCL);
}
}
XML
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="user"
type="com.journaldev.androidmvvmbasics.fragments.Update_observable"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff04"
android:orientation="vertical"
android:gravity="center"
tools:context="com.journaldev.androidmvvmbasics.fragments.FragmentTwo">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/fragment_quote_open_val"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:textColor="#000000"
android:textSize="16dp" />
<TextView
android:id="@+id/fragment_quote_ucl_val"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.mUCL}"
android:layout_margin="20dp"
bind:mUCL="@{user.mUCL}"
android:textColor="#000000"
android:textSize="16dp" />
</LinearLayout>
</layout>
解决方案
请确保您在 Base 类中执行 DataBindingUtil.inflate。
Fragment1Binding fragmentOneBinding = DataBindingUtil.inflate(inflater,R.layout.fragment1,null,false);
View view = fragmentOneBinding.getRoot();
fragmentOneBinding.setUsermodel(new UserModel("XXXX","XXXX"));
或者
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this,
R.layout.activity_main);
activityMainBinding.setUser(new User());
activityMainBinding.executePendingBindings();
然后在BaseObservable class
做
@Bindable
public String mEmail= "....";
重建项目,只是为了确保确定@Bindable
完成
现在打电话,根据您的要求。
notifyPropertyChanged(BR.xxx);
推荐阅读
- android - Android 11:图像捕获后方向更改时相机 Intent 崩溃
- flutter - 如何用flutter实现这个效果?
- reactjs - 使用 Git 存储库时出现 VS Code 错误
- python - 卷积生成对抗网络的鉴别器的输出是如何工作的,它可以有一个全连接层吗?
- javascript - Vue路由器导航栏元素不显示div
- django - 欢迎来到 CentOS - Django、Nginx 和 uwsgi
- javascript - 将变量从功能组件传递给同级组件到 index.js
- r - 来自 R 中的 JM 包的错误,特征(y,对称 = TRUE)在 R 中的联合模型中的“x”中的缺失值无限
- javascript - 向 Semantic UI React 下拉列表的每一行添加一个按钮/图标
- r - 如何填充/着色ggplot2中重叠的两条线的区域?