Today, I'm going to post about view binding tutorial in android. So, first of all, what is view binding? View binding is a term which connects your code objects with all the views, and these views are displayed to the end user for showing the data. For example, you use
TextView title = (TextView) findViewById(R.id.tvSample);
title.setText("Hello !!!");
This is the basic view binding that we do in android. So far so good to work with it when you have only one or two views to display. But what, if you have a user profile filled with 100's of TextView's relating to users detailed info? You will have to write 100's of this lines of code, which is very time-consuming and very tiring work, plus developers are a bit lazy to do this kind of work. To overcome this issue, Mr. Jake Wharton has added a view binding library called Butter Knife. Most of you already know about it and use it in your code. Here's the link to this library.
Here is an example for binding the above TextView with your coding object.
@BindView(R.id.tvSample) TextView title;Here, @BindView annotation is used to bind the tvSample to title object. With the latest update in the library, you will have to use an Unbinder Object to unbind or remove all the bound views when your app gets destroyed. unbinder.unbind() method to unbind all the views. For initializing it,
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); unbinder = ButterKnife.bind(this); // TODO Use fields... }onClick listener object binding as follows:
@OnClick(R.id.submit) public void submit(View view) { // TODO submit data to server... }This is the basic view binding tutorial to know about this library. Further, we will see how this library helps in recyclerview and all other lists views adapter classes to bind their row layout items with View Objects.
0 Comments:
Post a Comment