android-BASIC Programming-recyclerview

Source: Internet
Author: User

Later android-BASIC programming * is the control demo inside, no longer cumbersome rewrite. Describes the control's use directly.

Recyclerview is a more advanced and flexible version of the ListView. This widget was a Container for large sets of views of the can be recycled and scrolled very efficiently. Recyclerview was released from 2014. Located in Android.support.v7.widget.RecyclerView.

1.android Studio Configuration Recyclerview Library code

Join in the Gradle

Compile ' com.android.support:recyclerview-v7:23.+ '

2. Defining layouts in use

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

Also want to recycleview the layout of the item:

<Framelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:background= "#44ff0000"Android:layout_height= "Wrap_content" >    <TextViewAndroid:id= "@+id/id_num"Android:layout_width= "Match_parent"Android:layout_height= "50DP"android:gravity= "Center"Android:text= "1" /></Framelayout>

3. Use in Rcyclerviewacti

A. Load the view layout:

Recyclerview = (Recyclerview) Findviewbyid (R.id.recycle_view);

B. Setting up layout management LayoutManager

Recyclerview.setlayoutmanager (new Linearlayoutmanager (this));

There are currently three LayoutManager available in the SDK, namely thread, grid, waterfall layout Manager:

    • Linearlayoutmanager

    • Gridlayoutmanager

    • Staggeredgridlayoutmanager

C. Configuring a data source

New Recycleradapter (this); Adapter.setdata (Mdatas); // Adaoter Recyclerview.setadapter (Adapter) must be specified;

4. Click events, recycler do not support Click events, you need to implement

A. In the adapter definition the interface is implemented as follows:

 public  static  interface   Onrecyclerviewitemclicklistener { void  Onitemclick (view view, int   POS);  //  2 add interface and set interface method  private  Onrecyclerviewitemclicklistener monitemclicklistener = null  Span style= "color: #000000;"    >;  public  void   Setonitemclicklistener (Onrecyclerviewitemclicklistener listener) { this.monitemclicklistener = listener; }

B.adater Implement View Click events

@Override  Public void OnClick (View v) {        //toast.maketext (GET, "default Toast Style"). Show ();        LOG.D ("Zcx", "Test");         if NULL {            //Call Onitemclick            Monitemclicklistener.onitemclick (v,1);}

C. Calling Onitemclick

Adapter.setonitemclicklistener (new  Recycleradapter.onrecyclerviewitemclicklistener ()        {            @ Override            publicvoidint  pos) {                //doyour fucking Bussiness here!                Adapter.adddata (1);                Toast.maketext (Getapplicationcontext (),"zcx" +Pos,toast.length_long). Show ();            });

5.item Animation effect:

Recyclerview.setitemanimator (new defaultitemanimator ());

The results of the final experiment are as follows:

android-BASIC Programming-recyclerview

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.