DataBinding and ListView and events, databinding
At the Google IO conference in 2015, the DataBinding library was distributed to facilitate the implementation of the MVVM structure model. However, we have learned about DataBinding, but we have to record it here today. Some basic usage of DataBinding is not recorded here. After all, there are a lot of tutorials on Google Now, and on the official android developer website, I have also made a detailed introduction to its basic usage. I 'd like to read more official articles on children's shoes with basic English skills. If the English basics are not good, explain.
About the configuration environment:
More than 2.0 of Android Studio has built-in support for the Android Data Binding framework, which is easy to configure. You only need to add the following content to the build. gradle file of the app.
1 dataBinding{2 enabled = true3 }
However, the gradle version must be at least 1.5.0 or later. Otherwise, the configuration will be very troublesome. Because I use Android studio 2.1.3 and gradle is changed to 2.1.3, too many settings are not required. However, Android studio's support for DataBinding is not fully compatible, and it is a bit difficult in some places.
Usage:
Recently, I changed a small project I wrote to the architecture mode of DataBinding. It seems that the Android studio2.1.3 version is quite new, but some attribute prompts are not very good and not fully supported. The basic usage method is not mentioned here. It mainly describes the usage of ListView and GridView, as well as the writing of adapter and click jump events.
First, write an xml file of ListView or GridView. The Code is as follows:
1 <?xml version="1.0" encoding="utf-8"?> 2 <layout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto"> 5 6 <data> 7 8 <variable 9 name="adapter"10 type="android.widget.BaseAdapter"/>11 12 </data>13 14 <LinearLayout15 android:layout_width="match_parent"16 android:layout_height="match_parent"17 android:orientation="vertical">18 19 <ListView20 android:id="@+id/list_view"21 android:layout_width="match_parent"22 android:layout_height="match_parent"23 app:adapter="@{adapter}"/>24 25 </LinearLayout>26 </layout>
View Code
In the app: adapter = "@ {adapter}" sentence, we mainly define an adapter to bind data to ListView or GridView.
Then, the most important thing is actually the adapter method. In the previous writing method, BaseAdapter must be ViewHolder to bind the view and cache it. In DataBinding, ViewHolder is not required at all. In addition, for a single layout, you can write a general adapter. For general small projects, this adapter is enough, now, you can write the xml file of the item of an adapter. The Code is as follows:
1 <?xml version="1.0" encoding="utf-8"?> 2 <layout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto"> 4 5 <data> 6 <variable 7 name="userbean" 8 type="com.lqy.newtestdemo.UserBean"/> 9 </data>10 11 <RelativeLayout12 android:layout_width="match_parent"13 android:layout_height="match_parent"14 android:padding="10dp">15 16 <ImageView17 android:id="@+id/image"18 android:layout_width="150dp"19 android:layout_height="100dp"20 android:layout_marginRight="5dp"21 app:imageUrl="@{userbean.picUrl}"/>22 23 <LinearLayout24 android:layout_width="wrap_content"25 android:layout_height="wrap_content"26 android:layout_toRightOf="@id/image"27 android:orientation="vertical">28 29 <TextView30 android:layout_width="wrap_content"31 android:layout_height="wrap_content"32 android:text="@{userbean.title}"33 android:textColor="@android:color/black"34 android:textSize="20sp"/>35 36 <TextView37 android:layout_width="wrap_content"38 android:layout_height="wrap_content"39 android:layout_marginTop="5dp"40 android:text="@{userbean.ctime}"/>41 42 <TextView43 android:layout_width="wrap_content"44 android:layout_height="wrap_content"45 android:layout_marginTop="5dp"46 android:text="@{userbean.description}"/>47 </LinearLayout>48 </RelativeLayout>49 </layout>
View Code
As you can see, in the layout, a variable name is identified mainly by the variable attribute in data. In the control, you only need android: text = "@ {userbean. title} ", you can assign values to variables, which are described in basic usage and will not be discussed here. The following is the focus. For BaseAdapter writing, let's not talk much about it. Go directly to the Code:
1 package com. lqy. newtestdemo; 2 3 import android. content. context; 4 import android. databinding. dataBindingUtil; 5 import android. databinding. viewDataBinding; 6 import android. view. layoutInflater; 7 import android. view. view; 8 import android. view. viewGroup; 9 import android. widget. baseAdapter; 10 11 import java. util. list; 12 13/** 14 * General adapter15 * Created by LQY on 2016/10/10. 16 */17 public class ListAdapter <T> extends BaseAdapter {18 private Context context; 19 private List <T> list; 20 private int layoutId; // 21 private int variableId in a single layout; 22 23 public ListAdapter (Context context, List <T> list, int layoutId, int variableId) {24 this. context = context; 25 this. list = list; 26 this. layoutId = layoutId; 27 this. variableId = variableId; 28} 29 30 @ Override31 public int getCount () {32 return list. size (); 33} 34 35 @ Override36 public Object getItem (int position) {37 return list. get (position); 38} 39 40 @ Override41 public long getItemId (int position) {42 return position; 43} 44 45 @ Override46 public View getView (int position, View convertView, viewGroup parent) {47 ViewDataBinding binding = null; 48 if (convertView = null) {49 binding = DataBindingUtil. inflate (LayoutInflater. from (context), layoutId, parent, false); 50} else {51 binding = DataBindingUtil. getBinding (convertView); 52} 53 binding. setVariable (variableId, list. get (position); 54 return binding. getRoot (); 55} 56}
View Code
Here we can see that there is no trace of ViewHolder at all, and the adapter can be written in just a few lines of code, and multiple ListView or GridView can be used, after the adapter is set, you only need to add the following two sentences to the Activity:
1 ListAdapter<UserBean> adapter = new ListAdapter<>(MainActivity.this, list, R.layout.item, BR.userbean);2 binding.setAdapter(adapter);
Here we will not discuss how to use binding. Please refer to the basic usage method. When writing a universal adapter, we can see that the generic type of ListAdapter represents a Bean file, which is the file you need to assign values. List indicates the List Value of a list. This list can be the List Value parsed in Json or you can use list. the value attached to add depends on the needs of your project. The most pitfall lies in the BR. It is just like the R file generated by the project itself, except that the BR is an R file generated by DataBinding, you also need to import a BR package. Of course, if there is no problem with the project, Android studio will remind this BR value guide package. The pitfall I step on is that there is no problem or error in the Code clearly. The first operation was successful, and the second operation prompted that the BR file could not be found, if the package is deleted and re-imported, it cannot be imported. If the package is clean, the package cannot be imported, and the Rebuild prompts that the BR package cannot be found, how can this problem be solved. Finally, I can only turn off the entire Android studio and re-open it. I found that the BR package was imported, and there was no BUG, and the operation was successful... Therefore, if you encounter this problem, close Android studio and re-open it. If it is not good, it proves that your program is actually wrong. Just look for it carefully. That's almost the case.
The following is a question about click jump. In some other tutorials, only the binding of The onClick event may be written. What can be implemented in this tutorial is to change the current value or field. However, there are still no tutorials on how to jump. Now let's talk about how to implement the redirection. Let's 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.this, WebActivity.class); intent.putExtra("url", list.get(position).getUrl()); startActivity(intent); } });
You can also use the setOnItemClickListener method on the Activity page. Before calling the setOnItemClickListener method, first define a ListView variable name, and then findByViewId to associate the ListView ID value in the xml file, and then call the method. After DataBinding is used, you only need to use binding. listView can directly call the click event, and does not need to be in findByViewId. listView is actually the ID value in xml, and the ID value of this deformation is actually automatically generated by DataBinding according to the ID value, you only need to remember what your name is and find your own ID according to the general rule. There is no difficulty here. Not only does ListView work like this, but it also applies to the GridView. The GridView is also used in the project. The ListAdapter is also applicable to the GridView. In addition, in my project, two GridView containers are used on a page. You only need to define two different variable values in data, and the name must define different names, in this way, you can use a ListAdapter at the same time. In the future, I will put the source code up. Here, I will record the methods I use and what I need to pay attention.
If something is wrong, I hope everyone can point it out. I also hope this article can help the children's shoes struggling in DataBinding.