Android uses Recyclerview to implement custom lists, click events, and Drop-down refreshes _android

Source: Internet
Author: User
Tags gettext

Android uses Recyclerview

1. What is Recyclerview

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

Simply put: 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.

2. What you need to know to use Recyclerview

adapter-wrap the data collection and create a view for each entry
layoutmanager-Place the view of each entry in the appropriate place
onitemclicklistener-Set Click events for each entry
swiperefreshlayout-Add a Drop-down refresh event to Recyclerview
itemanimator-Draw some decorative views around or above the view of each entry
itemdecoration-Add animation effects When an entry is added, removed, or reordered

3. What needs to be prepared before using Recyclerview

Add a dependency pack (take Android studio for example)

4. Custom Adapter

Recyclerview.adapter contains a new type of adapter that is basically similar to the one we used previously, but slightly different, such as Viewholder, to encapsulate it so that we don't have to write viewholder as we used to do with ListView adapters.

The item view is as follows:

The instance code is as follows:

public class Recycleradapter extends Recyclerview.adapter {private list<model> models;
 Public Recycleradapter (list<model> models) {this.models = models;
  Class Viewholder extends recyclerview.viewholder{private imageview picture;

  Private TextView Title,text;

   Public Viewholder (View Itemview) {super (Itemview);
   Picture = (ImageView) Itemview.findviewbyid (r.id.picture);
   title = (TextView) Itemview.findviewbyid (r.id.title);
  Text = (TextView) Itemview.findviewbyid (R.id.text);
  Public ImageView Getpicture () {return picture;
  Public TextView GetTitle () {return title;
  Public TextView GetText () {return text; } @Override Public Recyclerview.viewholder oncreateviewholder (viewgroup parent, int viewtype) {return new Viewhol
 Der (Layoutinflater.from (Parent.getcontext ()). Inflate (r.layout.item_view,null)); @Override public void Onbindviewholder (Recyclerview.viewholder holder, final int position) {Final ViewholDer VH = (viewholder) holder;
  Vh.getpicture (). Setimageresource (Models.get (position). Getpicture ());
  Vh.gettitle (). SetText (Models.get (position). GetTitle ());
 Vh.gettext (). SetText (Models.get (position). GetText ());
 @Override public int GetItemCount () {return models.size ();

 }
}

5. What role does LayoutManager have?

Since Recyclerview is not just like the previous ListView or GridView, it is used as the successor to the ListView and GridView controls, So when you use it, you need to set its layoutmanager to specify which type it is, and you can set the LayoutManager to set whether it is a horizontal or vertical list, and if it is reversed.

Vertical list: (the third argument is whether the setting is reversed, that is, the slide direction)

Mlayoutmanager = new Linearlayoutmanager (this,linearlayoutmanager.vertical,false);
Recyclerview.setlayoutmanager (Mlayoutmanager);

Landscape list:

Mlayoutmanager = new Linearlayoutmanager (this,linearlayoutmanager.horizontal,false);
Recyclerview.setlayoutmanager (Mlayoutmanager);

Vertical Table layout:

Mlayoutmanager = new Gridlayoutmanager (this,2);
Recyclerview.setlayoutmanager (Mlayoutmanager);

Horizontal Table layout: (fourth parameter indicates whether to invert)

Mlayoutmanager = new Gridlayoutmanager (this,2,linearlayoutmanager.horizontal,false);
Recyclerview.setlayoutmanager (Mlayoutmanager);

6. Why do you want to add Onitemclicklistener

Recyclerview has a disadvantage, in Recyclerview, there is not a Onitemclicklistener method. So it is better to handle such an event in the adapter now. 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. The specific operation can be reflected in the adapter code.

The specific code is as follows:

public class Recycleradapter extends Recyclerview.adapter {private list<model> models;
  Public Recycleradapter (list<model> models) {this.models = models;
    Class Viewholder extends recyclerview.viewholder{private imageview picture;

    Private TextView Title,text;

      Public Viewholder (View Itemview) {super (Itemview);
      Picture = (ImageView) Itemview.findviewbyid (r.id.picture);
      title = (TextView) Itemview.findviewbyid (r.id.title);
    Text = (TextView) Itemview.findviewbyid (R.id.text);
    Public ImageView Getpicture () {return picture;
    Public TextView GetTitle () {return title;
    Public TextView GetText () {return text; } @Override Public Recyclerview.viewholder oncreateviewholder (viewgroup parent, int viewtype) {return new Vi
  Ewholder (Layoutinflater.from (Parent.getcontext ()). Inflate (r.layout.item_view,null)); @Override public void Onbindviewholder (RECYCLERVIEW.VIEwholder holder, Final int position) {Final Viewholder VH = (viewholder) holder;
    Vh.getpicture (). Setimageresource (Models.get (position). Getpicture ());
    Vh.gettitle (). SetText (Models.get (position). GetTitle ());

    Vh.gettext (). SetText (Models.get (position). GetText ()); 
        If a callback is set, set the Click event if (Monitemclicklistener!= null) {Vh.itemView.setOnClickListener (new View.onclicklistener () { 
        @Override public void OnClick (View v) {Monitemclicklistener.onitemclick (vh.itemview,position);
    }
      });
  @Override public int GetItemCount () {return models.size ();
  }/** * ItemClick Callback interface * * Public interface onitemclicklistener{void Onitemclick (View view,int position);

  Private Onitemclicklistener Monitemclicklistener; public void Setmonitemclicklistener (Onitemclicklistener monitemclicklistener) {This.monitemclicklistener =
  Monitemclicklistener;

 }
}

7. Realize Drop-down refresh and pull load more

In fact, as before, the original ListView or the GridView can not meet the needs of the actual app, many times you have to customize the view to achieve the Drop-down list of refresh and pull load, Recyclerview is the same, However, this will not introduce how to customize Recyclerview, instead, we will borrow swiperefreshlayout this component, faster to implement the list refresh function

The layout of the interface is as follows:

<view
    android:id= "@+id/swiperefreshlayout"
    class= "Android.support.v4.widget.SwipeRefreshLayout"
    android:layout_width= "match_parent"
    android:layout_height= "match_parent" >

    < Android.support.v7.widget.RecyclerView
      android:id= "@+id/recyclerview"
      android:layout_width= "Match_ Parent "
      android:layout_height=" match_parent >

    </android.support.v7.widget.RecyclerView>

</view>

To set a drop down Refresh listener event:

Swiperefreshlayout = (swiperefreshlayout) Findviewbyid (r.id.swiperefreshlayout);
Swiperefreshlayout.setonrefreshlistener (New Swiperefreshlayout.onrefreshlistener () {
      @Override
      public void Onrefresh () {
        //re-fetching data
        //Get completed swiperefreshlayout.setrefreshing (false);
      }
);

Setting up the pull load can be done by setting a sliding listener event:

Recyclerview.setonscrolllistener (New Recyclerview.onscrolllistener () {
      @Override public
      Void Onscrollstatechanged (Recyclerview recyclerview, int newstate) {
        super.onscrollstatechanged (RecyclerView, NewState);
        if (newstate = = Recyclerview.scroll_state_idle
            && lastvisibleitem + 1 = adapter.getitemcount ()) {
          Swiperefreshlayout.setrefreshing (true);

          Paging fetch data
          //Get complete swiperefreshlayout.setrefreshing (false);
        }

      @Override public
      void onscrolled (recyclerview recyclerview, int dx, int dy) {
        super.onscrolled (Recyclerview, DX, dy);
        Lastvisibleitem = Mlayoutmanager.findlastvisibleitemposition ();
      }
    );

8. Itemanimator and Itemdecorator

Because there is no recyclerview line, so there is itemdecorator, but there is another way, is directly in the item interface layout to add a split line, so you can save a lot of code;
And Itemanimator is simply to be based on the relevant notifications received on the adapter to animate the display components of the changes, add and delete. It automatically adds and removes the item's animation. The default effect is also good, already very good. Because these two items are not very commonly used, so there is no more introduction, interested students can surf the internet to find detailed information to learn.

Related Article

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.