Learn to use Recyclerview and arrays.aslist functions

Source: Internet
Author: User
Tags recyclerview android

To implement the loop scrolling effect of the ListView list, you can actually implement it with Recyclerview.

Runnable run = new Runnable () {@Overridepublic void run () {//string str = "abc";//datas.add (str);//mrecycleradapter.notif yiteminserted (1);d atas.remove (CurPos); mrecycleradapter.notifyitemremoved (CurPos); curpos--; Handler.sendemptymessage (0);};


Layout files are used in Recyclerview, Landscape and portrait Two:

<linearlayout 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:orien tation= "Vertical" tools:context= ".        MyActivity "> <textview android:layout_width=" match_parent "android:layout_height=" Wrap_content " android:padding= "10DP" android:text= "horizontal" android:textsize= "18sp"/> <android.support.v7 . Widgets. Recyclerview android:id= "@+id/recyclerview_horizontal" android:layout_width= "Match_parent" Android:la         yout_height= "50DP" android:scrollbars= "horizontal"/> <textview android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:padding= "10DP" android:text= "Vertical" android:t Extsize= "18sp"/> <android.support.v7.widget.recyclerview android:id= "@+id/recyclerview_vertical" a ndRoid:layout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "1" android:scroll bars= "Vertical"/></linearlayout>

in Activity

Package Com.baoyz.recyclerviewdemo;import Android.app.activity;import Android.os.bundle;import Android.support.v7.widget.linearlayoutmanager;import Android.support.v7.widget.recyclerview;import Android.view.menu;import Android.view.menuitem;public class MyActivity extends Activity {@Override protected void O        Ncreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_my);        Inithorizaontal ();    Initvertical (); } private void Inithorizaontal () {Recyclerview Recyclerview = (recyclerview) Findviewbyid (r.id.recyclerview_hor        Izontal);        Create a linear layout manager Linearlayoutmanager LayoutManager = new Linearlayoutmanager (this);        Layoutmanager.setorientation (linearlayoutmanager.horizontal);        Set Layout manager Recyclerview.setlayoutmanager (LayoutManager);        Create DataSet string[] DataSet = new string[100]; for (int i = 0; i < dataset.length; i++) {Dataset[i] = "iteM "+ i;        }//Create adapter and specify the DataSet Myadapter adapter = new Myadapter (dataset);    Set adapter Recyclerview.setadapter (adapter); } public void Initvertical () {Recyclerview Recyclerview = (recyclerview) Findviewbyid (r.id.recyclerview_vertical        );        Create a linear layout manager Linearlayoutmanager LayoutManager = new Linearlayoutmanager (this);        Default is VERTICAL, can not write Layoutmanager.setorientation (linearlayoutmanager.vertical);        Set Layout manager Recyclerview.setlayoutmanager (LayoutManager);        Create DataSet string[] DataSet = new string[100];        for (int i = 0; i < dataset.length; i++) {Dataset[i] = "item" + I;        }//Create adapter and specify the DataSet Myadapter adapter = new Myadapter (dataset);    Set adapter Recyclerview.setadapter (adapter);                                                                                                                                                            }                                                                                                                          }

Recyclerview A feature is that the layout is abstracted into a layoutmanager,recylerview is not responsible for sub-View layouts, we can customize the LayoutManager to achieve different layout effects, currently only provides Linearlayoutmanager. Linearlayoutmanager can specify the direction, the default is vertical, you can specify the level, so it is easy to achieve a horizontal ListView.
Adapter

Package Com.baoyz.recyclerviewdemo;import Android.support.v7.widget.recyclerview;import Android.view.View;import Android.view.viewgroup;import android.widget.textview;/** * Created by Baoyz on 2014/6/29.     */public class Myadapter extends recyclerview.adapter<myadapter.viewholder>{//DataSet private string[] Mdataset;        Public Myadapter (string[] dataset) {super ();    Mdataset = DataSet; } @Override Public Viewholder oncreateviewholder (viewgroup viewgroup, int i) {//Create a view, simply use the system-provided layout directly, which is A textview view view = View.inflate (Viewgroup.getcontext (), Android.        R.layout.simple_list_item_1, NULL);        Create a viewholder viewholder holder = new Viewholder (view);    return holder; } @Override public void Onbindviewholder (Viewholder viewholder, int i) {//Bind data to Viewholder Viewholde    R.mtextview.settext (Mdataset[i]);    } @Override public int getitemcount () {return mdataset.length; } public StatIC class Viewholder extends recyclerview.viewholder{public TextView Mtextview;            Public Viewholder (View Itemview) {super (Itemview);        Mtextview = (TextView) Itemview; }    }}

Another feature of Recyclerview is the standardization of Viewholder, which is written Adapter oriented Viewhoder and not the view, the logic of reuse is encapsulated and easier to write.

Ashamed, using arrays.as (string[]) method improper, debugging half a day do not know which is wrong:

Try running the following code, and guess what will happen.

   Public Static void Main (string[] args) {        string[] strs={"1", "2", "3", "4"};        System.out.println (Arrays.aslist (STRs). Remove (0));    

If not right, you will generally say yes: 1. But actually the return is:

Exception in thread "main" java.lang.UnsupportedOperationException at    java.util.AbstractList.remove ( Abstractlist.java:144) at    Com.lmax.disruptor.YieldingWaitStrategy.main (Yieldingwaitstrategy.java: 126)

Why does this happen? Because we think the JDK will return a ArrayList or LinkedList, call the Remove (int index) method directly. What does Aslist actually return? Aslist returns a subclass of the arrays class's internal custom abstractlist, which does not implement the Remove method.

How do you avoid this happening?

 Public Static void Main (string[] args) {        string[] strs={"1", "2", "3", "4"};        System.out.println (new linkedlist<string> (Arrays.aslist (STRs)). Remove (0));    

Learn to use Recyclerview and arrays.aslist functions

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.