Android ListView vs. Recycleview use

Source: Internet
Author: User
Tags pear

The ListView, just like its name, is used to display a list of a view, while the Recycleview, which is its enhanced version, today brings the contrast between these two almost identical functions

Let's start with the ListView.

ListView:

1. Use the listview in the layout file and define an ID for it to make it easier for us to call later, with the same width and height as the parent control

2. Prepare the data and add the data to the Arrayadapter adapter

3. Use Findviewbyid in the activity's Java file to locate the ListView instance and set it adapter

4. Implement the ListView item click event and directly use the Setitemonclick method defined by the ListView.

The most important step here is actually the second step, we can see the following code, define a set of string type array, then create the Arrayadapter adapter, note that string, is a generic, the parameters in the following constructor is the context of Android. R.layout.simple_list_item 1 is an item layout given by the Android system, and the last item is a string array, which is the data, then looks for the instance of the ListView and calls the Setadapter method to set the adapter

   Public classMainactivityextendsappcompatactivity {Privatestring[] data = {"Apple", "Banana", "Orange", "Watermelon" "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", " Mango ",                "Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape",                "Pineapple", "Strawberry", "Cherry", "Mango"}; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity.main); Arrayadapter <String> adapter = new arrayadapter<string (Mainactivity.this, Android. R.layout.simple_list_item 1 , data); ListView ListView=(ListView) Findviewbyid (r.id.list _view);    Listview.setadapter (adapter); }}

This is a simple example, but we know that the second parameter is an item layout provided by Android, a layout that is too simple, what do we do if we want to implement a custom layout? We need to retrofit the Arrayadapter adapter.

Here are the steps to customize the layout and use:

1. Create a layout of item, that is, our custom layout

I have defined two textview of each, and remember the high use of the root tag wrap_content

2. Define a T class, as the generic for the adapter, (similar to the previous string), which requires a constructor method, a member variable and a GET method

Do not know why there is a warning, the use of public, can be used normally, if you change to another, the other side of the call will be wrong

3. Create an adapter class that inherits Arrayadapter<t> (T is the class defined in the second step), requires a construction method, and requires a replication GetView method

4. Prepare the data, add the data to the third step of the adapter class, and then, like the steps above, locate the instance of the ListView, set the adapter

Effect

Recycleview:

Since this is a new Android team, we need to add dependencies to use, remember to click Sync Now

Steps to use:

1. Use Recyclerview in the layout file, define the ID, and the same width and height as the space

2. Prepare the child item layout for Recyclerview, then use it in the adapter class, not here .

3. Define a generic class, such as the T class previously mentioned in the ListView, the generic type of the adapter

3. Define an adapter T class that inherits Recyclerview.adapter<t.viewholder> (Viewholder is an inner class in the T Class), requires a constructor method to receive the data source, and a replication of three methods

Recycleradapter is inherited recyclerview.adapter<recycleradapter.viewholder>

The angle brackets are supposed to be recyclerview.viewholder, but we define an inner class viewholder in the Recycleradapter, So that it inherits the Recyclerview.viewholder, so we can call the inner class directly in the angle brackets.

Let's take a look at this inner class of Viewholder, which has two TextView member variables, and then adds a constructor, which is a view, for this two textview to find an instance by Findviewbyid

Let's look at the Recycleadapter class, at the beginning we declare a list of member variable students, and then add a constructor method, pass in a list, assign it to recycleadapter member variable students

After that, three methods need to be replicated,Oncreateviewholder,onbindviewholder,getitemcount

In the Oncreateviewholder method, the first is to receive a context through the Layoutinflater.from () method, and then, through inflate, put the item layout into the overall layout, The view is then passed as a parameter to the Viewholder, returning a Viewholder

The Onbindviewholder method, obtained by the list's Get method, obtains an object from the list, and after that, it is set by the holder call two TextView SetText method, extrapolate, if it is ImageView, You can also use Setresource to set the image that it displays, set the parameters, and we can get it directly from the Get method in the student class.

GetItemCount is to get the total number of items, where the item number is actually the item in the list, directly return its size on the line

4. Prepare the data and add it to the adapter

5. Create a layout manager LayoutManager, easy to set up the layout of Recycleview, where the layout manager has several

A vertical arrangement like a ListView, Linearlayoutmanager

We can also change it to a horizontal arrangement, just call Linearlayoutmanger's setorientation, set to Linearlayoutmanager.horizontal

There are giidlayout Grid layout Staggergildlayoutmanager waterfall flow layout, using the method as above, however, the parameters are different, want to know more please go Baidu

6. Use the Findviewbyid method to find an instance of Recycleview, set the LayoutManager for it, set the adapter

7. Set the OnClick method, there is no OnClick method in Recycleview, we need to write ourselves, where to write it? In the Onbindviewholder method in Recycleadapter

We use Viewholder to find the subkey, set a Onclicklistener for it, and then set the onclick event handling for it

The above is a relatively simple one, there are several other methods, I do not test each of these, interested in the words can be self-study

Reference Address: http://blog.csdn.net/RaphetS/article/details/51254538

For more Recycleview advanced use, please refer to the address below

http://blog.csdn.net/skykingf/article/details/50827141

Http://www.jianshu.com/p/58ca20e1a5d6


Android ListView vs. Recycleview use

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.