MainActivityBinding.inflate(getLayoutInflater()) 页面未更新
MainActivityBinding.inflate(getLayoutInflater()) page is not updated
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Main2Binding binding1 = Main2Binding.inflate(getLayoutInflater());
User user = new User("Test", "User");
MyHandlers myHandlers = new MyHandlers(this);
MyStringUtils myStringUtils= new MyStringUtils();
binding1.setUser(user);
binding1.setHandlers(myHandlers);
}
页面未更新,
MainActivityBinding binding1 = DataBindingUtil.setContentView (this, R.layout.main_activity); there is no problem
如何解决这个问题?
如果您想将布局与 Activity
绑定,您需要使用
MainActivityBinding binding1 = DataBindingUtil.setContentView (this, R.layout.main_activity);
但是当你使用 Fragment
时,你可以使用 inflate
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
MainActivityBinding binding = DataBindingUtil.inflate(inflater, R.layout.main_activity, container, false);
}
注意:绑定名称将基于布局名称,如果布局文件名称是 activity_main.xml 那么您的绑定将是 ActivityMainBinding。
通过inflate方法使用DataBinding的正确方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main2); //<---comment it
Main2Binding binding1 = Main2Binding.inflate(getLayoutInflater());
setContentView(binding1.getRoot());
...
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Main2Binding binding1 = Main2Binding.inflate(getLayoutInflater());
User user = new User("Test", "User");
MyHandlers myHandlers = new MyHandlers(this);
MyStringUtils myStringUtils= new MyStringUtils();
binding1.setUser(user);
binding1.setHandlers(myHandlers);
}
页面未更新,
MainActivityBinding binding1 = DataBindingUtil.setContentView (this, R.layout.main_activity); there is no problem
如何解决这个问题?
如果您想将布局与 Activity
绑定,您需要使用
MainActivityBinding binding1 = DataBindingUtil.setContentView (this, R.layout.main_activity);
但是当你使用 Fragment
时,你可以使用 inflate
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
MainActivityBinding binding = DataBindingUtil.inflate(inflater, R.layout.main_activity, container, false);
}
注意:绑定名称将基于布局名称,如果布局文件名称是 activity_main.xml 那么您的绑定将是 ActivityMainBinding。
通过inflate方法使用DataBinding的正确方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main2); //<---comment it
Main2Binding binding1 = Main2Binding.inflate(getLayoutInflater());
setContentView(binding1.getRoot());
...
}