In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you the relevant knowledge of how Android binds data. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
1. Basic structure
1.1 JavaBeans object
Public class User {private final String firstName; public User (String firstName) {this.firstName = firstName;} public String getFirstName () {return this.firstName;}}
1.2 layout file
1.3 Activity
@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); ActivityMainBinding binding = DataBindingUtil.setContentView (this, R.layout.main_activity); User user = new User ("Test"); binding.setUser (user);}
The above three steps complete the basic data binding
two。 Dynamic update
When the data of the JavaBean object changes, the View updates automatically.
Normal data sources can only be bound. If you want to update dynamically, you must use Observable to store data. Here are three ways to implement Observable.
2.1 Observable object
Implement android.databinding.Observable or inherit its implementation class
Private static class User extends BaseObservable {private String firstName; @ Bindable public String getFirstName () {return this.firstName;} public void setFirstName (String firstName) {this.firstName = firstName; notifyPropertyChanged (BR.firstName);}}
2.2 Observable field
Basic data types have corresponding Observable classes
Public final ObservableField firstName = new ObservableField ()
2.3 Observable Collection
ObservableArrayMap mapUser = new ObservableArrayMap ()
User.put ("firstName", "Google")
2.4 referenc
Whether it is an object, a field, or a collection, data is stored in the form of Observable. The next step is to quote it in the layout.
3. RecyclerView dynamic binding
Private static class RecyclerViewAdapter extends RecyclerView.Adapter {private List mModels; public static class BindingHolder extends RecyclerView.ViewHolder {private final ViewDataBinding binding; public BindingHolder (ViewDataBinding binding) {super (binding.getRoot ()); this.binding = binding; / / you can binding.getRoot () .findViewById here, and then bind the event in onBindViewHolder. There is no more convenient method} public ViewDataBinding getBinding () {return binding. } @ Override public BindingHolder onCreateViewHolder (ViewGroup parent, int viewType) {ViewDataBinding binding = DataBindingUtil.inflate (LayoutInflater.from (parent.getContext ()), R.layout.list_item, parent, false); BindingHolder holder = new BindingHolder (binding). That's all of the article "how to bind data in Android". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.