Android Recyclerview Basic Knowledge _android

Source: Internet
Author: User

This week's Google I/O conference brought a lot of exciting news about Android. It may take us a long time to digest the new things introduced by Android L.

I have been studying recyclerview these days and would like to share with you my achievements so far.

What is Recyclerview?

Recyclerview is a new view group with the goal of providing similar rendering for any adapter based view. It is used as a successor to the ListView and GridView controls and is supported in the latest SUPPORT-V7 version.

Extensibility is fully considered when developing recyclerview, so it can be used to create any kind of layout that is thought of. But it is also slightly inconvenient to use. That's android--it's not always easy to finish something.

If you use Recyclerview, you need to know the following three elements:

    1. Recyclerview.adapter
    2. LayoutManager
    3. Itemanimator

Recyclerview.adapter

Recyclerview includes a new type of adapter. It's similar to the adapter you're using, but it's slightly different, for example, it needs to use Viewholder. You need to rewrite two main methods when you use it: one to show the view and its holder, and the other to bind the data to the view. The advantage of doing so is that the first method is invoked only when we really need to create a new view, and we do not need to check if it has been reclaimed.

;

  Html-script:false] public class Myrecycleradapter extends Recyclerview.adapter<myrecycleradapter.viewholder> {
  Private list<viewmodel> items;

  private int itemlayout;
    Public Myrecycleradapter (list<viewmodel> items, int itemlayout) {this.items = items;
  This.itemlayout = ItemLayout; @Override public Viewholder Oncreateviewholder (viewgroup parent, int viewtype) {View v = layoutinflater.from (par
    Ent.getcontext ()). Inflate (ItemLayout, parent, false);
  return new Viewholder (v);
    @Override public void Onbindviewholder (viewholder holder, int position) {ViewModel item = items.get (position);
    Holder.text.setText (Item.gettext ());
    Holder.image.setImageBitmap (NULL);
    Picasso.with (Holder.image.getContext ()). Cancelrequest (Holder.image);
    Picasso.with (Holder.image.getContext ()). Load (Item.getimage ()). into (Holder.image);
  Holder.itemView.setTag (item);
  @Override public int GetItemCount () {return items.size ();

}  public static class Viewholder extends Recyclerview.viewholder {public ImageView image;

    public TextView text;
      Public Viewholder (View Itemview) {super (Itemview);
      Image = (ImageView) Itemview.findviewbyid (r.id.image);
    Text = (TextView) Itemview.findviewbyid (R.id.text);

 }
  }
}

This is a simple adapter, but things are getting a little complicated. In Recyclerview, there is not a Onitemclicklistener method (at least I didn't find it). So the adapter is a good candidate for handling events.

If you want to add or remove entries from the adapter, you need to explicitly notify the adapter. This is slightly different from the previous notifydatasetchanged () method.

; Html-script:false] public
void Add (ViewModel item, int position) {
  Items.Add (position, item);
  notifyiteminserted (position);
}

public void Remove (ViewModel item) {
  int position = Items.indexof (item);
  Items.remove (position);
  notifyitemremoved (position);
}

LayoutManager

This class determines where the view is placed in the screen, but this is just one of its many responsibilities. It can be managed by rolling and recycling.

LayoutManager has only one implementation class called Linearlayoutmanager, which has more than 1500 lines of code. But from this we can see how complicated it is. The manager can simulate a list view (both landscape and portrait), but without a header and footer.

Writing subclasses for LayoutManager is not a good fit for beginners, we need to rely on the community to discover the full potential of recyclerview. Together with this example, in a short time I will upload a GridView control implementation.

I think the key behind this is to create a baselayoutmanager modeled after the Linearlayoutmanager code and extend it based on that. Perhaps the final version of SUPPORT-V7 will provide more and better implementations.

Itemanimator

Itemanimator displays changes to the view group based on notifications received on the adapter. Basically, it automatically displays add and remove entry animations. This is not a simple class, but we find that defaultitemanimator can already run well.

Recyclerview settings

So finally, if you want to initialize a running Recyclerview, you need to do something like this:

; Html-script:false]
Recyclerview recyclerview = (recyclerview) Findviewbyid (r.id.list);
Recyclerview.sethasfixedsize (true);
Recyclerview.setadapter (New Myrecycleradapter (Createmocklist (), R.layout.item));
Recyclerview.setlayoutmanager (This) (new Linearlayoutmanager);
Recyclerview.setitemanimator (New Defaultitemanimator ());

The Sethasfixedsize () method is used to keep the recyclerview to a fixed size, which is used for its own optimizations.

Summarize

Recyclerview is indeed a powerful view that provides developers with unlimited scalability. The learning curve can be very steep. But I'm sure that soon the Android community will be releasing LayoutManager awesome implementations.

I'm creating a github warehouse where I can find this example. It is the foundation of an extension library that I plan to create. You can test the implementation of the GridView. All forms of feedback are welcome.

Above is the basic information on the Android Recyclerview, follow-up to continue to supplement the relevant information, thank you for your support for this site!

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.