Android Implementation Recyclerview Custom lists, click events, and drop-down refreshes

Source: Internet
Author: User

Android using 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 the ListView, more advanced and more flexible.

Simply put: Recyclerview is a new view group with the goal of providing a 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 version of SUPPORT-V7.

2. What you need to know to use Recyclerview
    • adapter-Wrap data collection and create a view for each entry
    • layoutmanager-Place the view of each entry in the appropriate location
    • onitemclicklistener-Set Click events for each entry
    • swiperefreshlayout-Adding a drop-down refresh event to Recyclerview
    • itemanimator-to draw some decorative views around or above the view of each item
    • itemdecoration-Animate when an entry is added, removed, or reordered
3. What to prepare before using Recyclerview

Add a dependency package (take Android studio as an example)

4. Custom Adapter

Recyclerview.adapter contains a new type of adapter, which is basically similar to the adapter we used before, just slightly different, such as viewholder it to help us encapsulate it, do not have to write the Viewholder as before with the ListView adapter.

The item view is as follows:

The instance code is as follows:

 Public  class recycleradapter extends recyclerview. Adapter {    PrivateList<model> models; Public Recycleradapter(List<model> models) { This. models = models; } class Viewholder extends recyclerview.viewholder{PrivateImageView picture;PrivateTextView 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); } PublicImageViewgetpicture(){returnPicture } PublicTextViewGetTitle(){returnTitle } PublicTextViewGetText(){returnText }    }@Override     PublicRecyclerview.viewholderOncreateviewholder(ViewGroup parent,intViewType) {return NewViewholder (Layoutinflater.from (Parent.getcontext ()). Inflate (R.layout.item_view,NULL)); }@Override     Public void Onbindviewholder(Recyclerview.viewholder holder,Final intPosition) {FinalViewholder 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() {returnModels.size (); }}
5. What is the role of LayoutManager?

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, or you can set the LayoutManager to set whether it is a horizontal or vertical list, and if it reverses.

Portrait list: (the third parameter is whether the setting is reversed, that is, the slide direction)

new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);recyclerview.setLayoutManager(mLayoutManager);

Landscape list:

new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);recyclerview.setLayoutManager(mLayoutManager);

Vertical Table layout:

new GridLayoutManager(this,2);recyclerview.setLayoutManager(mLayoutManager);

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

new GridLayoutManager(this,2,LinearLayoutManager.HORIZONTAL,false);recyclerview.setLayoutManager(mLayoutManager);

6. Why add Onitemclicklistener yourself

Recyclerview has one drawback, in Recyclerview, there is no Onitemclicklistener method. So it's 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 {    PrivateList<model> models; Public Recycleradapter(List<model> models) { This. models = models; } class Viewholder extends recyclerview.viewholder{PrivateImageView picture;PrivateTextView 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); } PublicImageViewgetpicture(){returnPicture } PublicTextViewGetTitle(){returnTitle } PublicTextViewGetText(){returnText }    }@Override     PublicRecyclerview.viewholderOncreateviewholder(ViewGroup parent,intViewType) {return NewViewholder (Layoutinflater.from (Parent.getcontext ()). Inflate (R.layout.item_view,NULL)); }@Override     Public void Onbindviewholder(Recyclerview.viewholder holder,Final intPosition) {FinalViewholder VH = (viewholder) holder;        Vh.getpicture (). Setimageresource (Models.get (position). Getpicture ());        Vh.gettitle (). SetText (Models.get (position). GetTitle ()); Vh.gettext (). SetText (Models.get (position). GetText ());//Set a click event if a callback is set        if(Monitemclicklistener! =NULL) {Vh.itemView.setOnClickListener (NewView.onclicklistener () {@Override                 Public void OnClick(View v)                {Monitemclicklistener.onitemclick (vh.itemview,position);        }            }); }    }@Override     Public int GetItemCount() {returnModels.size (); }/** * ItemClick Callback interface * /     Public  interface onitemclicklistener{        voidOnitemclick (View view,intposition); }PrivateOnitemclicklistener Monitemclicklistener; Public void Setmonitemclicklistener(Onitemclicklistener Monitemclicklistener) { This. Monitemclicklistener = Monitemclicklistener; }}
7. Implement pull-down refresh with pull-up load more

In fact, as before, the original ListView or GridView can not meet the needs of the actual application, many times to be through the custom view to implement the list of drop-down refresh and pull-up loading, Recyclerview is the same, However, this will not show you how to customize Recyclerview, instead, we will borrow swiperefreshlayout this component, faster to implement the list refresh function

The interface layout 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. Widgets. RecyclerviewAndroid:id="@+id/recyclerview"Android:layout_width="Match_parent"android:layout_height="Match_parent"> </android. Support. V7. Widgets. Recyclerview></view>

To set the drop-down Refresh listener event:

swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {            @Override            publicvoidonRefresh() {                //重新获取数据                //获取完成swipeRefreshLayout.setRefreshing(false);            }});

To set up pull-up loading more can be achieved by setting the swipe listener event:

Recyclerview.setonscrolllistener (NewRecyclerview.onscrolllistener () {@Override             Public void onscrollstatechanged(Recyclerview Recyclerview,intNewState) {Super. onscrollstatechanged (Recyclerview, newstate);if(NewState = = Recyclerview.scroll_state_idle && Lastvisibleitem +1= = Adapter.getitemcount ()) {swiperefreshlayout.setrefreshing (true);//paging to get data                    //Get completion swiperefreshlayout.setrefreshing (FALSE);}            }@Override             Public void onscrolled(Recyclerview Recyclerview,intDxintDY) {Super. onscrolled (Recyclerview, DX, dy);            Lastvisibleitem = Mlayoutmanager.findlastvisibleitemposition (); }        });
8. Itemanimator and Itemdecorator

Since the Recyclerview does not have a split line, so there is a itemdecorator, but in fact there is another way, is directly in the item interface layout to add a split line, which can save a lot of code;
Itemanimator, in short, will be based on the adapter received the relevant notification to animate the display components of the changes, additions and deletions. It will automatically add and remove the item's animation. The default effect is also good, already very good. Because these two are not very commonly used, so here is not more introduction, interested students can go online to find detailed information to learn.

Editor's it Little match

Android Implementation Recyclerview Custom lists, click events, and drop-down refreshes

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.