Android Learning Note: Super Recyclerview component Usage (top)

Source: Internet
Author: User

These days the development encountered some interface sliding problems, mainly in the interface of the sliding control used too much, resulting in a sliding conflict. Just learning Recyclerview this control, so according to the screen learning situation to do a study notes, of course, a lot of content is to learn the screen, thanks to the teacher's pay.

Recyclerview is a new component in the ANDROID.SUPPORT.V7 package and is a powerful sliding component, where is it powerful?

1, achieve the ListView effect

2, to achieve the GridView effect

3, to achieve the horizontal sliding of the ListView and transverse sliding of the GridView effect

4, achieve waterfall flow effect

5, custom item add and delete animation effects

The most important thing is to implement the above features are easy, as long as a few lines of code can be implemented.

So why name this powerful component Recyclerview?

1, do not care whether the item is displayed in the correct position and how to display, we can use LayoutManager to control

2, do not care about the separation between the item problem, there is itemdecoration to achieve

3, do not pay attention to the item's additions and deletions animation effect, because there are itemanimator

4, the most concerned is how to recycle and use our view, forcing the use of viewholer.

Here is the code directly:

main interface layout Main_activity.lxml

<relativelayout xmlns:android=" http://schemas.android.com/apk/res/android "
    xmlns:tools= "Http://schemas.android.com/tools"
    android:layout_width= "match_parent"
    android:layout_height= "match_parent";
    <android.support.v7.widget.recyclerview<!- -This is the protagonist of our day-->
        android:id= "@+id/test_rcv"
        Android: Layout_width= "Match_parent"
        android:layout_height= "wrap_content"
       
    </android.support.v7.widget.recyclerview>
</RelativeLayout>

Main interface activity:

public class Mainactivity extends Actionbaractivity {
Private Recyclerview Mrecyclerview;
Private list<testdata> Mdatas;
Private Testadapter Madapter;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initdatas ();
Initviews ();
}
private void Initdatas () {
Mdatas=new arraylist<testdata> ();
TestData data=new TestData ();
for (int i = ' A '; i < ' z '; i++) {
Data.settitle ("Title" + (char) i);
Data.setcontent ("Content" + (char) i);
Mdatas.add (data);
}
}

private void Initviews () {
mrecyclerview= (Recyclerview) Findviewbyid (R.ID.TEST_RCV);
Madapter=new Testadapter (this, mdatas);
Mrecyclerview.setadapter (Madapter);
Linearlayoutmanager ll=new Linearlayoutmanager (This, linearlayoutmanager.horizontal, true);//This setting isHorizontal horizontal effect, if set to vertical is the vertical effect, and our usual ListView effect.
Mrecyclerview.setlayoutmanager (LL);
}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}

@Override
public boolean onoptionsitemselected (MenuItem item) {
int id = item.getitemid ();
if (id = = r.id.action_settings) {return true;}
return super.onoptionsitemselected (item);
}
}

Item Layout Lv_item.xml

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:padding= "10DP" >
<imageview
Android:id= "@+id/item_iv"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:src= "@drawable/ic_launcher"/>
<textview
Android:id= "@+id/item_title"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_below= "@id/item_iv"/>
<textview
Android:id= "@+id/item_content"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_below= "@id/item_title"/>
</RelativeLayout>

The last is adapter:

/**
* @description:
* We usually use Baseadapter to create Viewholer in the GetView method and set the data for the corresponding view.
* And in recyclerview.adapter<>, this process is divided into two parts, by the Oncreateviewholder and Onbindviewholder two methods to deal with the completion,
* In fact, when using the Recyclerview control, you must force the use of Viewholder mode to optimize the program
*/
public class Testadapter extends Adapter<testviewholder> {
Private Context Mcontext;
Private list<testdata> Mdatas;
Public Testadapter (Context mcontext, list<testdata> mdatas) {
This.mcontext = Mcontext;
This.mdatas = Mdatas;
}

@Override
Public Testviewholder Oncreateviewholder (viewgroup paramviewgroup, int paramint) {
View v = layoutinflater.from (mcontext). Inflate (R.layout.lv_item, NULL);
Testviewholder holder = new Testviewholder (v);
return holder;
}

@Override
public void Onbindviewholder (Testviewholder paramvh, int paramint) {
ParamVH.titleTv.setText (Mdatas.get (paramint). GetTitle ());
ParamVH.contentTv.setText (Mdatas.get (paramint). GetContent ());
}
@Override
public int GetItemCount () {
TODO auto-generated Method Stub
return Mdatas.size ();
}
}
Class Testviewholder extends Viewholder {
ImageView Iconiv;
TextView Titletv;
TextView Contenttv;
Public Testviewholder (View holder) {
Super (Holder);
Iconiv = (ImageView) Holder.findviewbyid (R.ID.ITEM_IV);
Titletv = (TextView) Holder.findviewbyid (r.id.item_title);
Contenttv = (TextView) Holder.findviewbyid (r.id.item_content);
}
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Learning Note: Super Recyclerview component Usage (top)

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.