Android self-learning course-Use of RecyclerView and androidrecycler

Source: Internet
Author: User
Tags getcolor

Android self-learning course-Use of RecyclerView and androidrecycler

I saw an introduction to RecyclerView on the internet, saying that it is an advanced version of ListView. I recommend it officially, and I will find the information and play tricks.

First, the official tutorials are posted. The official tutorials are the most authoritative and convincing first-hand materials.

Https://developer.android.com/training/material/lists-cards.html

  • To create complex lists and cards with material design styles in your apps, you can useRecyclerViewAndCardViewWidgets.
      • We can see from this that the RecyclerView and CardView widgets are provided to create the material design styles style.
  • TheRecyclerViewWidget is a more advanced and flexible versionListView. This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views. UseRecyclerView Widget when you have data collections whose elements change at runtime based on user action or network events.
      • There is not much sentiment and it is difficult to write it in disorder. In short, it is more advanced and flexible than ListView.
  • TheRecyclerViewClass simplifies the display and handling of large data sets by providing:
    • Layout managers for positioning items
    • Default animations for common item operations, such as removal or addition of items
      • The RecyclerView class simplifies the display and processing of a large number of datasets in the following two ways. 1: Layout managers is used to manage the display of items. 2: The default animation is used to run common items, such as deleting and adding items.
  • You also have the flexibility to define custom layout managers and animationsRecyclerViewWidgets.
      • To tell the truth, do you mean to define your own Recyclerview? I have never done this before.

Nothing can be said.

    

 

If you have any source code, you will not need to write it. You will need to fill it out later.

After that, write the code. The official source code can be roughly understood.

The Demo I wrote is as follows.

      

The first Gif image cannot be captured. The first image can be pulled down.

 

The activity _ main. xml Layout

Just like ListView, we need to create a layout first. The RecyclerView can be considered as a container.

We need to import the Jar package first,

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     tools:context=".MainActivity"> 6  7     <android.support.v7.widget.RecyclerView 8         android:id="@+id/recyclerView" 9         android:scrollbars="vertical"10         android:layout_width="match_parent"11         android:layout_height="match_parent"12         android:background="#CCCC"/>13 14 </RelativeLayout>

Creating our data class

ColorDateItem.java
 1 package com.ryan.recyclerviewdemo01; 2  3 import android.graphics.Color; 4  5 /** 6  * Created by air on 15-8-17. 7  */ 8 public class ColorDateItem { 9     private String name;10     private int color;11 12     public ColorDateItem(String name, String color) {13         this.color = Color.parseColor((color));14         this.name = name;15     }16 17     public int getColor() {18         return color;19     }20 21     public String getName() {22         return name;23     }24 }

 

 The data layout

This layout serves ColorDataItem objects. We want to display our data on the screen.

 

 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent"> 5     <View 6         android:layout_width="match_parent" 7         android:layout_height="150dp" 8         android:id="@+id/colorBlock" 9         android:background="#CCCCCC"/>10 11     <TextView12         android:layout_width="match_parent"13         android:layout_height="wrap_content"14         android:id="@+id/colorName"15         android:text="name"16         android:textColor="@android:color/white"17         android:textSize="22dp"18         android:layout_margin="10dp"/>19 20 </RelativeLayout>

 

 

Creating the Adapter

Our adaper must inherit the RecyclerView and must rewrite three methods.

OnCreateViewHolder ()

1     @Override2     public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {3         //create a new view4         View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout,null);5         //set the view's size , margins , padding and layout parameters6         //……7         MyViewHolder vh = new MyViewHolder(v);8         return vh;9     }

        Here Layoutflater and inflate are not very useful

  

OnBindVIewHolder ()

1 @ Override 2 public void onBindViewHolder (RecyclerView. viewHolder holder, int position) {3 //-get element from your dataset (dataset) at this position 4 //-replace the contents of the view with that element 5 ColorDateItem dateItem = dateItems. get (position); 6/** casting the viewHolder to MyViewHolder so I cocould interface with the views */7 MyViewHolder myViewHolded = (MyViewHolder) holder; 8 myViewHolded. colorBlock. setBackgroundColor (dateItem. getColor (); 9 myViewHolded. colorName. setText (dateItem. getName (); 10}

  

  GetItemCount ()

1     @Override2     public int getItemCount() {3         return dateItems.size();4     }

  

  MyViweHolder class

 

 1     /** this is our ViewHolder class */ 2     public static class MyViewHolder extends RecyclerView.ViewHolder{ 3         public TextView colorName; 4         public View colorBlock; 5  6         public MyViewHolder(View itemView) { 7             super(itemView); /** Must call super()first */ 8             colorName = (TextView) itemView.findViewById(R.id.colorName); 9             colorBlock = itemView.findViewById(R.id.colorBlock);10         }11     }

 

Overall Effect

MyAdapter. class

 

1 package com. ryan. recyclerviewdemo01; 2 3 import android. support. v7.widget. recyclerView; 4 import android. view. layoutInflater; 5 import android. view. view; 6 import android. view. viewGroup; 7 import android. widget. textView; 8 9 import java. util. list; 10 11/** 12 * Created by air on 15-8-17.13 */14 public class Myadapter extends RecyclerView. adapter {15 16 private List <ColorDateItem> dateItems; 17 18 p Ublic Myadapter (List <ColorDateItem> dateItems) {19 this. dateItems = dateItems; 20} 21 22 23 24/** this is our ViewHolder class */25 public static class MyViewHolder extends RecyclerView. viewHolder {26 public TextView colorName; 27 public View colorBlock; 28 29 public MyViewHolder (View itemView) {30 super (itemView);/** Must call super () first */31 colorName = (TextView) itemView. findViewById (R. id. colo RName); 32 colorBlock = itemView. findViewById (R. id. colorBlock); 33} 34} 35 36 37 38 // 39 @ Override40 public RecyclerView. viewHolder onCreateViewHolder (ViewGroup parent, int viewType) {41 // create a new view42 View v = LayoutInflater. from (parent. getContext ()). inflate (R. layout. item_layout, null); 43 // set the view's size, margins, padding and layout parameters44 //...... 45 MyViewHolder vh = new MyViewHolder (v); 46 return VL; 47} 48 49 50 // replace the contents of the view (invoked by the layout manager) 51 @ Override52 public void onBindViewHolder (RecyclerView. viewHolder holder, int position) {53 //-get element from your dataset (dataset) at this position54 //-replace the contents of the view with that element55 ColorDateItem dateItem = dateItems. get (position); 56/** casting the viewHolder to MyViewHolder so I cocould interface with the views */57 MyViewHolder myViewHolded = (MyViewHolder) holder; 58 myViewHolded. colorBlock. setBackgroundColor (dateItem. getColor (); 59 myViewHolded. colorName. setText (dateItem. getName (); 60} 61 62 63 // number of return Items 64 @ Override65 public int getItemCount () {66 return dateItems. size (); 67} 68}

 

The layoutManager

As mentioned earlier, LayoutManager is mainly responsible for displaying data on the screen. We also have three options: LinerlayoutManager, GridLayoutManager and StaggeredGirdLayoutManager. We can also create our own LayoutManager if needed. In the first test, we used LingerLayout, And the implementation method was always simple.

 

mLayoutManage = new LinearLayoutManager(this);        mRecyclerView.setLayoutManager(mLayoutManage);

                      

LayoutManager

        mLayoutManage = new GridLayoutManager(this,2);        mRecyclerView.setLayoutManager(mLayoutManage);

                     

 

HandLing item click evens

RecycleView does not have onItemClickListener. On the contrary, we need to implement onClickListener in our ViewHolder.

There are many ways to implement this step. I will illustrate a simple method.

Click the event to print the color name of the item.

 

    public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{        public TextView colorName;        public View colorBlock;        public MyViewHolder(View itemView) {            super(itemView); /** Must call super()first */            colorName = (TextView) itemView.findViewById(R.id.colorName);            colorBlock = itemView.findViewById(R.id.colorBlock);            itemView.setOnClickListener(this);        }        @Override        public void onClick(View v) {            Log.d("MyViewHolder", "Item click:"+colorName.getText().toString());        }    }

 

It is worth noting that

itemView.setOnClickListener(this);

View itemView is actually our item main layout.

 

This is just one corner of the ice. What makes RecyclerView really powerful is to customize different components ,.......

It is easy to describe RecycleView as a more personalized and flexible ListView, but this may be a bit misleading. Although RecyclerView allows ListView to share the same features, their core or even philosophy is different.

 

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.