Android Recyclerview Experience (i)-Introduction

Source: Internet
Author: User
Tags bulk insert

In the online about the basic use of Recyclerview has been introduced in more detail, and its design structure is similar to the ListView, so this article will not focus on how to use, in the text at the end of the reference can be related content. This is mainly about the basic functions of recyclerview, design concepts, and the system to provide the API.

What is Recycleview

Recyclerview is a newly added ViewGroup in Android L (that is, later Lollipop). But because it makes the SUPPORT-V7 library into the Android system, it's not just the lollipop version of the Android system that can be used, as long as the system is introduced in the development project to be used at any API level.

Recyclerview and ListView, GridView are not the most difference from the rendering effect.

But its "flexible" does not mean that it can switch between the ListView and the GridView at will, but that it can create more complex scrollable views, such as a horizontal ListView, or a popular waterfall-streaming layout on the Web ( Masonry). Only most of the layout systems are not available and must be implemented by the developer themselves.

So Recyclerview's "flexible": anything you can do, but do everything yourself.

the main differences between Recyclerview and ListView

With Android-provided Lieanerlayoutmanager and VERTICAL mode, the Recyclerview is perfect for the basic effects of the ListView. The design structure of both is also the way that the data (dataset) is detached from the views (view) and then connected through the adapter (Adapter).

However, Recyclerview has the following major improvements relative to the ListView:

    • No Onitemclicklistener.

The ListView directly inherits the response of the sub-item click from its parent class Adapterview, and the developer can define their own Onitemclicklistner to accept the Click event. But this design also creates some problems, such as the child interior view if the Onclicklisteneris set, then the subproject view itself is not known, which may cause problems such as the view click state is not synchronized. Reyclerview does not provide a simple response to a sub-item being clicked by the listener, although it has a onitemtouchlistener, but in this interface method there is no information about that sub-item being clicked, the interface is just to help developers intercept touch events, For how to handle, the detection is touched by the target object is left to the developer to complete.

    • Detach View from layout

The ListView does the separation of data and views, recyclerview the view and layout further apart, and then LayoutManager. Recyclerview is responsible for managing the reuse of the view and then handing over the layout to the LayoutManager, which allows for different layout effects by configuring or switching layoutmanager . Unlike a ListView is limited to a vertical scrolling layout. Recyclerview also provides itemdecoration, which allows additional views to be added based on existing child views. For example, to make a split line, the ListView needs an extra viewtype to provide the view, and now there is no need to include these auxiliary content in the adapter that are not relevant to the actual logical business .

    • Support for animation effects at the sub-project level

The ListView can also support the animation of the sub-project level, and there are a lot of introductions to this in the Android developers Devbytes Channel, but after looking at its implementation you will find out how ugly and lengthy the solution is. Most of the time, the position state of the child view is calculated and analyzed. The Recyclerview brings a very concise itemanimator interface. When the data in the adapter is changed, the animation can be activated by invoking the corresponding method of adapter. Of course, developers also need to implement specific Itemanimator objects to complete the desired animation, but its clear structure and interface has been a great improvement than the ListView.

the status quo of Recyclerview

The Recyclerview kit mentioned above has been added to the support library V7 and is placed in a separate library, so as long as the gradle compiled files in the Android Studio project dependencies You can start using Recyclerview by adding the following sentence:

' com.android.support:recyclerview-v7:25.x.x '
But by looking at the implementations of those self-contained objects in the library, reflecting the basic operational flow, you will find that Recyclerview can do a lot of things, but it has done too little.
Recyclerview.adaper

Recyclerview provides an abstract adapter class, and then there is no. There are no subclasses that can be used directly, such as ListAdapter, Arrayadapter, Simplecursoradapter-ready classes. Everything is left to the developer to implement the definition themselves.

Think about this is quite normal, I believe that there should be few in the actual product use Arrayadapter, because most of the list will not be a simple line of text. For CursorAdapter, it is also often possible to implement different inheriting classes to provide child views. Moreover Recyclerview adapter and ListAdapter in the concept is still the same, so want to achieve a recyclerviewcursoradapter, directly from cursoradapter material can.

Recyclerview's Adapter relative ListAdapter has several variations on the interface that are also noteworthy.

First, it splits the GetView () method into a Createviewholder ( ) and Bindviewholder ( ) two. But this is nothing to be nervous, in the CursorAdapter has already seen this more reasonable design. The return object also changes from view to Viewholder just mention it.

The key point to note is that the second parameter of Createviewholder (viewgroup parent, int viewtype) is shaped, but it is not the position (position) of the previous current subkey, but instead calls the Getitemviewtype () Gets the category of the subkey. It seems that when the Viewholder is created, Recyclerview deliberately hides the details of the subkey and expects the developer to rely solely on its category to create the corresponding view and Viewholder.

Second, Recyclerview's adapter, in addition to the same notifydatasetchanged () method as ListAdapter, has a bunch of ways to change the notification data that triggers the animation effect:

    • final void notifyitemchanged (int position); //Modify
    • final void notifyiteminserted (int position); //Insert
    • final void notifyitemmoved (int fromposition, int toposition); //Mobile
    • final void notifyitemrangechanged (int positionstart, int itemCount); //Batch Change
    • final void notifyitemrangeinserted (int positionstart, int itemCount); //BULK INSERT
    • final void notifyitemrangeremoved (int positionstart, int itemCount); //Bulk Delete
    • final void notifyitemremoved (int position); //Delete a

Invoking these methods will fire the corresponding method on the itemanimator to produce the animation.

Recyclerview.viewholder

In the time of the ListView, in fact already used Viewholder, only then the method looks more flattering, to hide in the View Tag () . Now Recyclerview enforces the use of Viewholder, and Viewholder in addition to references to child views, there are information such as itemviewtype and position.

Mholder //Get current holder type  mholder. getadapterposition (); //Gets the current position (updated whenever there is a delete/Add Item), so it can be used instead of the tag to get the data. 

Recyclerview.layoutmanager

LayoutManager is a new part of the ListView, and inherits the class to implement a custom layout, not just a fixed layout for the ListView. The support library provides two ready-made subclasses:Linearlayoutmanager and Staggeredgridlayoutmanager. The former can get the same layout as the ListView, and it can be horizontally oriented, while the latter provides a layout like the GridView. So the basic everyday of the application can be fulfilled.

If you need to implement a custom layoutmanager, it will be troublesome to understand recycle, scrap, dirty, the concept of the view state of the subproject.

Recyclerview.itemanimator

By inheriting the Itemanimator, and then creating the object settings on the Recyclerview, you get the animation effect based on the sub-item. However, how to properly and reasonably create a itemanimator subclass without a detailed description of the guide.

The only subclass available in the library is Defaultitemanimator, which shows that its animated effect is a simple alpha gradient. It will also find that the implementation is so complex, there are many actions for animation steps, but also note that the animation is interrupted in the middle of the processing, at the end of the view to reset the state for reuse. This, in turn, shows that Itemanimator basically does not provide any information about how to implement and manage animations. On the other hand, because the implementation of Defaultitemanimator is too specific, it is not appropriate as a parent class for custom Itemanimator .

It is believed that when Recyclerview more and more are applied to the program, more reasonable design of this aspect will be raised. At present, there are many references on GitHub to the implementation of Defaultitemanimator, such as Recyclerviewitemanimators, Recyclerview-animators.

There are several points to note about the use of Itemanimator: if itemanimatoris not provided, Recyclerview creates a defaultitemanimator for animation by default, So it is not necessary to set the Defaultitemanimator object to Recyclerview on the display, adding (add) and removing (remove) is the default, but the effect of modifying (change) needs to be called Setsupportschangeanimations (Boolean) to specify whether to enable it by default, which is an animation that is not modified.

Overall, Recyclerview is very powerful and its structural design is very open, which makes it relatively difficult to get started. As more and more people begin to try to use this part, there will be more and more profound understanding and design implementation. In addition, reading the source code of Recyclerview can help to understand its design ideas, and can be better referenced later when designing other reusable views.

Android Recyclerview Experience (i)-Introduction

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.