DataBinding and ListView of Android development and event _android

Source: Internet
Author: User
Tags android data binding

2015 Google IO Conference distributed the DataBinding library, can be faster Czech convenience to achieve MVVM structure mode. However, through the study of databinding, which stepped through the pit, today to be recorded here. For the databinding some of the basic use, here is not recorded, after all, now Google, out a lot of tutorials, and, Android developer official website, has also been the basic use of its methods to do a detailed introduction, there are English based children's shoes, or to see more official articles. If the English Foundation is not very good, https://realm.io/cn/news/data-binding-android-boyar-mount/recommend this blog, there will be a great harvest, at the same time, thank you cotton candy This article, resolved a lot of doubts.

About configuring the environment:

More than 2.0 of Android Studio already has built-in support for the Android Data Binding Framework, and it's simple to configure, just add the following in the app's Build.gradle file.

databinding{
enabled = True
}

However, the Gradle version, at least 1.5 0 or more, otherwise the configuration will be very cumbersome. Because I use the version of the Android studio is 2.1.3,gradle also changed to 2.1.3, so there is no need to do too many settings. But one thing, Android Studio support for DataBinding is not entirely compatible, and some places do have a bit of a hole.

About using:

Recently, a small project that was previously written has been changed to the databinding architecture pattern. It feels like the Android studio2.1.3 version is already very new, but the hints for some of the attributes are not very good yet and are not fully supported. Comparison of the basis of the use of methods, here is not mentioned, mainly to write about the use of ListView and the GridView, as well as the adapter, and click on the Jump event.

First, write a ListView or GridView XML file with the following code:

<?xml version= "1.0" encoding= "Utf-8"?> <layout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
xmlns:app=" Http://schemas.android.com/apk/res-auto ">
<data>
<variable
name= "adapter"
type= "Android.widget.BaseAdapter"/>
</data>
<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "match_parent"
android:orientation= " Vertical ">
<listview
android:id=" @+id/list_view "android:layout_width=" Match_parent "
android:layout_height= "match_parent"
app:adapter= "@{adapter}"/>
</LinearLayout>
</layout>

In the app:adapter= "@{adapter}", the main point is to customize a adapter to bind the data to ListView or GridView.

Then, the main thing is actually the way the adapter is written. In the past, Baseadapter certainly needed viewholder to bind the view and cache it. Well, in databinding, there is no need to viewholder, and, for a single layout, can write a generic adapter, for the general Small project, this adapter fully enough, then, Now let's write an XML file of the adapter item, as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <layout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns:app= "Http://schemas.android.com/apk/res-auto" > <data> <variable name= "Userbean" Com.lqy.newtestdemo.UserBean "/> </data> <relativelayout android:layout_width=" Match_parent "Android: layout_height= "Match_parent" android:padding= "10DP" > <imageview android:id= "@+id/image" android:layout_width
= "150DP" android:layout_height= "100DP" android:layout_marginright= "5DP" App:imageurl= "@{userbean.picurl}"/> <linearlayout android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_ torightof= "@id/image" android:orientation= "vertical" > <textview android:layout_width= "wrap_content" Android: layout_height= "Wrap_content" android:text= "@{userbean.title}" android:textcolor= "@android: Color/black" android: Textsize= "20sp"/> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_margintop= "5DP" android:text= "@{userbean.ctime}"/> <textview android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" android:layout_margintop= "5DP" android:text= "@{userbean.description}"/> </LinearLayout> </RelativeLayout> </layout>

The

can be seen, in the layout, mainly through the variable property in data to identify a variable name, in the control, only need android:text= "@{userbean.title}", you can do variable assignment, which is explained in the basic usage, This is not a discussion here. The following is the key, about Baseadapter's writing, nonsense not to say, directly on the code:

Package Com.lqy.newtestdemo;
Import Android.content.Context;
Import Android.databinding.DataBindingUtil;
Import android.databinding.ViewDataBinding;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import java.util.List;
/** * Universal Adapter * Created by lqy on 2016/10/10. * * public class Listadapter<t> extends Baseadapter {private context; private list<t> List;
T layoutid;//single layout private int variableid; Public ListAdapter (context, list<t> List, int layoutid, int variableid) {this.context = context; this.list
= list;
This.layoutid = LayoutID;
This.variableid = Variableid; @Override public int GetCount () {return list.size ():} @Override public Object getitem (int position) {return List.get (
position); @Override public long getitemid (int position) {return position;} @Override public view getview (int position, View conv Ertview, ViewGroup parent) {viewdatabinding binding = null; if (Convertview = = null) {Binding =databindingutil.inflate (Layoutinflater.from (context), layoutid,parent,false);
else {binding = Databindingutil.getbinding (Convertview);} binding.setvariable (Variableid,list.get (position));
return Binding.getroot (); }
}

Here you can see that there is no trace of viewholder, and that just a few lines of code, you can write the adapter well, and you can use multiple ListView or GridView, adapter set Just add two sentences to the activity:

listadapter<userbean> adapter = new Listadapter<> (mainactivity.this, List, R.layout.item, BR.userbean);
Binding.setadapter (adapter);

Binding how to come, here is not discussed, please see the basic use method. So, when writing a generic adapter, we can see that what ListAdapter's generics represent is actually a bean file, which is the file you need to assign a value to. The list represents a list value of the lists that you can parse out in JSON, or you can pass the value attached to your list.add, which depends on the needs of your project. It's the most pits. On BR, BR says it's the same as the project itself, except that BR is an R file produced by databinding and needs to be imported into a BR package, of course, if the project is OK, Android Studio will remind you of this BR value Guide package. The pit I stepped on was, obviously, there was no problem in the code, there is no fault, the first run of success, the second in the run, the prompt br file can not find, the package deleted the guide are not to go in, clean the use of the package or not, rebuild, the hint can not find the BR package, It's not going to get through. In the end, I had to turn off the entire Android studio and turn it back on, and I found that the BR package was in, and then there was no bugs, and the operation was successful ... So, if you're in this situation, shut down Android studio and reopen it, and if it's not, prove your program is wrong and look for it. That's pretty much the way it is.

Here is another question, that is, the question of click Jump. In some other tutorials, you may only write the binding of the OnClick event, which can be achieved by changing the current value or field. However, there are not a few tutorials on how to jump. Now I'm going to talk about how to implement the jump, first look at the code:

Binding.listView.setOnItemClickListener (New Adapterview.onitemclicklistener () {
@Override public
Void Onitemclick (adapterview<?> Parent, view view, int position, long id) {
Intent Intent = new Intent (mainactivity.t his, webactivity.class);
Intent.putextra ("url", List.get (position). GETURL ());
StartActivity (intent);
}
);

On the Activity page, you can also use the Setonitemclicklistener method. Before invoking the Setonitemclicklistener method, you first define a ListView variable name, and then Findbyviewid to associate the ListView ID value of the XML file before calling its method. After using the databinding, As long as the use of Binding.listview can directly invoke the click event, there is no need in the Findbyviewid, and ListView is actually the ID value in the XML, and the ID value of this deformation is actually databinding based on the ID value automatically generated, you just need to remember your name is what , according to the general rules to find their own definition of the ID is good, there is no difficulty. Not only ListView can be used this way, but the same applies to the GridView. I also used the GridView in the project, and the pro-Test ListAdapter also applies to the GridView. And, in my project, is a page to use two GridView, only need to define two different variable values in the data, and the name of the definition to define a different name, so you can use a listadapter, later I will be the source of the stacking, This is just a record of the methods I use and the places I need to be aware of.

The above is a small set to introduce the Android development of the DataBinding and ListView and events, I hope to help you, if you have any questions welcome to my message, small series will promptly reply to everyone!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.