The new Android component RecyclerView is introduced to improve efficiency and androidrecycler

Source: Internet
Author: User

The new Android component RecyclerView is introduced to improve efficiency and androidrecycler


Today, we will first explain why we should introduce this new component RecyclerView, because a few days ago I published an article about the reuse of the frequently used panel question ListView and how to optimize it to some developers, however, I can see the related feedback that listview is no longer used, instead of RecyclerView. It is true, but after all, it is a minority, so many people may not know this component very well. Let's introduce it!

1. What is RecyclerView?

RecyclerView is a new Widgets in the android-support-v7-21 version, the official introduction to it is: RecyclerView is an upgraded version of ListView, more advanced and flexible.

Simply put, RecyclerView is a new view group designed to provide similar rendering methods for any adapter-based view. It is supported in the latest support-V7 version as the successor to the ListView and GridView controls.

2. What are precautions for using RecyclerView and how to use it?

Although RecyclerView fully considers its scalability, it is easier to use and more flexible, but it is also troublesome to use. To use RecyclerView, consider the following:

(1), RecyclerView. Adapter

(2) LayoutManager

(3) ItemAnimator

Point 1: RecyclerView. Adapter

RecyclerView. the Adapter contains a new type of Adapter, which is basically similar to the one we used before, but slightly different. For example, viewholder encapsulates the Adapter for us, you do not need to write viewholder as you previously used the listview adapter. Therefore, its performance should be much better than before. The following is an example of an adapter:

PackageCom. pandastudios. pandastudios. adapter;

ImportAndroid. support. v7.widget. RecyclerView;
ImportAndroid. view. View;
ImportAndroid. view. ViewGroup;
ImportAndroid. widget. TextView;

ImportCom. pandastudios. pandastudios. R;

ImportJava. util. List;

/**
* Created by loonggg on 15/7/18.
*/
Public classMyAdapterExtendsRecyclerView. Adapter <MyAdapter. ViewHolder> {
PrivateList <String>List;

/**
* Similar to the adapter used when using listview, the data set must be imported.
*
*
@ ParamList
*/
PublicMyAdapter (List <String> list ){
This.List= List;
}

@ Override
PublicViewHolder onCreateViewHolder (ViewGroup viewGroup,IntI ){
View view = View. inflate (viewGroup. getContext (), R. layout.Rv_item,Null);
ViewHolder holder =NewViewHolder (view );
ReturnHolder;
}

@ Override
Public voidOnBindViewHolder (ViewHolder viewHolder,IntI ){
ViewHolder.TV. SetText (List. Get (I ));
}

@ Override
Public intGetItemCount (){
ReturnList. Size ();
}

Public static classViewHolderExtendsRecyclerView. ViewHolder {
PublicTextViewTV;

PublicViewHolder (View itemView ){
Super(ItemView );
TV= (TextView) itemView. findViewById (R. id.Item_ TV);
}
}

Public voidAdd (String item,IntPosition ){
List. Add (position, item );
Policyiteminserted (position );
}

Public voidRemove (String item ){
IntPosition =List. IndexOf (item );
List. Remove (position );
Policyitemremoved (position );
}
}

Second: LayoutManager

This LayoutManager Class determines the position of a view on the screen, but this is only one of its many responsibilities. It can manage scrolling and recycling. LayoutManager has only one implementation class called LinearLayoutManager. We can set it horizontally and vertically.

Third: ItemAnimator

In short, ItemAnimator will modify, add, and delete the display components of the animation based on the notifications received by the adapter. It automatically adds and removes the item animation. The built-in default effect is also good, and it is already very good.

3. Advantages and Disadvantages of RecyclerView

Advantages:

RecyclerView itself does not care about view-related issues. Due to the tight coupling of ListView, google's improvement is that RecyclerView itself does not participate in any view-related issues. It does not care about how to place the child View in the appropriate position, nor how to split the child View, or the appearance of each child View. Furthermore, RecyclerView is only responsible for recycling and reuse, which is also the origin of its name.

All layout, rendering, and other related issues, that is, all issues related to data presentation, are assigned to some "ins" classes for processing. This makes the RecyclerView API very flexible. Do you need a new layout? Access another LayoutManager! Do you want different animations? Access a new ItemAnimator, and so on.

Disadvantages:

In RecyclerView, there is no onItemClickListener method. Therefore, it is better to process such events in the adapter. To add or remove entries from the adapter, you must explicitly notify the adapter. This is slightly different from the previous policydatasetchanged () method. The specific operations can be reflected in the adapter code.


The overall summary is as follows:

Adapter: Wrap a data set and create a view for each entry.

ViewHolder: Save the Child View used to display each data entry.

LayoutManager: place the view of each entry in the appropriate position.

ItemDecoration: draws decoration views around or above each view.

ItemAnimator: adds an animation effect when an entry is added, removed, or reordered.


Also: forward this article to the circle of friends, send shares, ask for source code.


I personally organized a high-end Android communication group. If you are interested, you can first share this article to your circle of friends, add me, and send it to your circle of friends. I will pull you in. If you learn more, you can receive the dry goods push immediately.


No.: smart_android (persistent copy)

Introduction: A non-famous programmer, who has the character "straight left" and "right", loves machine and programming. He is a programmer crawling on the mobile Internet!

Personal ID: loonggg

Weibo: qilang

QQ group: 413589216 more information and source code are available in QQ group files

Toutiao: search for "non-famous programmers" to subscribe to more information

Work: focus on the development and research of mobile Internet. I am dedicated to sharing my experience in IT technology and programmers. You are welcome to pay attention to and repost.


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.