This article summarizes the notes of the Android ListView and Recyclerview that I learned this two days, as well as the understanding and learning of the two controls from the perspective of my iOS developer, comparing the controls in their iOS that are consistent and can be used for comparison.
One: The simplest usage of the ListView
The easiest way to use the ListView is simply to write a basic list interface that shows the data that is our most basic and common piece of data, step-by-step:
The creation of the ListView, first of all the creation of its layout, the direct code, a very simple listview of the width, height and ID:
After the layout file is created is our registration in the mainactivity, some of their own understanding of the comments are below, can help understand:
look at the above code and comments, will also know that our focus on this adapter, if we use the system's arrayadapter, we again through the Initfruits method to create a good data source, using the following method:
arrayadapter<string> adapter = new Arrayadapter<string> (Mainactivity.this, Android. R.layout.simple_list_item_1, data);
The successful construction of our adapter, this is our simplest listview.
In iOS, (if you understand iOS) you know that our tableview use a lot of time, need you to customize the cell, we said below the custom Android ListView adapter is our iOS said custom cell, this is the focus, I believe you know.
Two: Custom Listviwe adapter (custom ListView interface)
Here we customize an adapter called Fruitadapter, and let's look at the contents of this adapter:
The main point is to look at the Fruitadapter adapter inside the GetView this method, the specific code of this method is as follows, and this method is already the ListView is optimized to see the specific comment content:
talk about the process of optimization that you understand:
One: We use the view to Contentview cache, judging if Contentview is empty, we use Layoutinflater to load the layout, if this contentview is not empty, we will directly reuse, In fact, the reuse here I believe that every iOS do understand, and cell reuse is a meaning.
Second: Let's look at another viewholder, the role of this is to cache the control, because if you do not cache the GetView every time you get to the control of the time to do a Findviewbyid method to read, So we can use this viewholder, we don't get the control every time through the Findviewbyid method.
With the optimization of the above two steps, this listview is running very well!
Three: The Click event of the ListView
This is like when you use the TableView, you write so many cells must be able to click, we say the ListView Click event:
We are here to simply write its click event and let it pop up a toast, of course this toast we also said before, we use Setonitemclicklistener to register a listener for the ListView, When the user clicks on any of the items in the ListView, The Onitemclick () method is recalled, and the parameters in this method are as follows, you can command + (click Onitemclicklistener) method to look inside the parameters of this onitemclick, to let you fully remember how to write, I think it is very difficult, learn to see the parent class method is a good way:
Android Learning--listview