Android databinding and listview and event details

Today, let's learn about the latest data binding framework brought to us by Android - data binding library. The data binding framework brings us greater convenience. In the past, we may need to write a lot of findviewbyids in the activity. Annoying code also increases the coupling of our code. Now we can abandon so many findviewbyids immediately. At this point, some people may have a question: I can use some annotation frameworks without findviewbyid. Yes, but annotations are destined to slow down our code. Data binding will not. The official website document says it will also improve the speed of parsing XML. The most important data binding does not only reduce our findviewbyid. Please read the article below for more benefits.

The 2015 Google IO conference distributed the databinding library, which can realize the MVVM structure mode more quickly and conveniently. However, through the study of databinding, I found that I stepped on the pit. Today, I want to record it here. Some basic uses of databinding are not recorded here. After all, Google has come up with a lot of tutorials. Moreover, the basic usage methods have been introduced in detail on the official website of Android Developer. For children's shoes with English foundation, go to the official articles.

About configuring the environment:

Android studio above 2.0 has built-in support for Android data binding framework, and the configuration is also very simple. You only need to build it in the app Just add the following to the gradle file

However, the gradle version must be at least 1.5 0 or above, otherwise the configuration will be troublesome. Because I use Android studio version 2.1 3. Gradle has also been changed to 2.1 3. Therefore, there is no need to make too many settings. However, Android studio's support for databinding is not fully compatible, and there are some holes in some places.

About using:

Recently, a small project written before was changed to the schema of databinding. Feel Android Studio 2 Version 1.3 is very new, but the prompt for some attributes is not very good and is not fully supported. The basic usage methods are not mentioned here. They are mainly about the use of listview and GridView, the writing of adapter and click jump events.

First, write an XML file of listview or GridView. The code is as follows:

Focus on the sentence app: adapter = "@ {adapter}". It is mainly to customize an adapter to bind data to listview or GridView.

Then, the most important thing is the writing of the adapter. In the previous writing method, baseadapter certainly needs viewholder to bind the view and cache it. Then, in databinding, there is no need for viewholder at all. Moreover, for single layout, you can write a general adapter. For general small projects, this adapter is completely enough. Now, let's write an XML file of the item of the adapter. The code is as follows:

It can be seen that in the layout, a variable name is mainly identified by the variable attribute in data. In the control, you only need Android: text = "@ {userbean. Title}" to assign a variable. This is described in the basic usage, which is not discussed here. The following is the key point. There is no more nonsense about the writing method of baseadapter. Go directly to the code:

It can be seen here that there is no trace of viewholder. Moreover, the adapter can be written in a few lines of code, and can be used in multiple listviews or GridViews. After the adapter is set, just add these two sentences to the activity:

1 listadapter<UserBean> adapter = new listadapter<>(MainActivity.this,list,R.layout.item,BR.userbean); 2 binding. setAdapter(adapter);

How binding came from is not discussed here. Please look at the basic usage methods. Then, when writing a general-purpose adapter, we can see that what the generic type of listadapter represents is actually a bean file, which is the file you need to assign. List represents the list value of a list. This list can be the list value you parsed in JSON or you can use list The value attached to add depends on the needs of your project. The most pitiful place is br. BR is the same as the R file generated by the project itself. However, BR is an R file generated by databinding, and it also needs to import a br package. Of course, if the project is OK, Android studio will remind the br value guide package. The pit I stepped on was that there were no problems in the code and no errors. The first time I ran successfully, the second time I ran, I prompted that the br file could not be found, the package could not be imported after deletion, the clean did not work, the package still could not be imported, and the rebuild prompted that the br package could not be found, so I couldn't get through. Finally, I had to turn off the entire Android studio and reopen it. I found that the br package was imported, and then there was no bug, and the operation was successful... Therefore, if you also encounter this situation, please close Android studio and reopen it. If it is not good, it proves that your program is actually wrong. Just look carefully. That's about it.

There is another question below, that is about click jump. In other tutorials, you may only write about the binding of onclick events. What you can achieve is to change the current value or field. However, there are no tutorials on how to jump. Now let me talk about how to implement jump. First look at the code:

On the activity page, you can also use the setonitemclicklistener method. Previously, when calling setonitemclicklistener method, first define a variable name of listview, and then findbyviewid to associate the ID value of listview in the XML file, and then call its method. After using databinding, just use binding Listview can directly call the click event without findbyviewid. Listview is actually the ID value in XML, and the deformed ID value is automatically generated by databinding according to the ID value. You only need to remember what your name is and find the ID you define according to the general rules. There is no difficulty here. Not only can listview be used in this way, this also applies to GridView. I also used GridView in the project. The listadapter is also applicable to GridView. Moreover, in my project, two GridViews are used in one page. Only two different variable values need to be defined in data, and different names should be defined for the definition name of name, so that one listadapter can be used at the same time. Later, I will put the source code on it. Here is just a record of the methods I use and the points that need attention.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>