Android Recyclerview to implement the horizontal sliding code _android

Source: Internet
Author: User

Recyclerview is a newly added SDK in the Android L version to replace ListView, which is more flexible and fungible than ListView. This article introduces the Android Recyclerview to achieve the horizontal sliding code, together to see.

Android.support.v7.widget.RecyclerView

function: recyclerview Transverse slide

Control: <android.support.v7.widget.recyclerview/>

Java classes: Recyclerview, Galleryadapter, list<integer>

1, the main interface layout

<?xml version= "1.0" encoding= "Utf-8"?> <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:paddingbottom= "@dimen/activity_vertical_margin" 
android: paddingleft= "@dimen/activity_horizontal_margin" 
android:paddingright= "@dimen/activity_horizontal_margin" 
android:paddingtop= "@dimen/activity_vertical_margin" 
tools:context= " Com.info.activity.RecyclerActivity "> 
<android.support.v7.widget.recyclerview 
android:id=" @+id/ Id_recyclerview_horizontal " 
android:layout_width=" match_parent " 
android:layout_height=" 120DP 
" Android:layout_centervertical= "true" 
android:background= "#FF0000" 
android:scrollbars= "None" 
></android.support.v7.widget.RecyclerView> 

2. Sub-interface layout

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= 
"http://schemas.android.com/apk" /res/android " 
android:layout_width=" 120DP " 
android:layout_height=" 120DP "> 
<imageview 
Android:id= "@+id/id_index_gallery_item_image" 
android:layout_width= "80DP" 
android:layout_height= "80DP" 
android:layout_alignparenttop= "true" 
android:layout_centerhorizontal= "true" 
android:layout_ Margin= "5DP" 
android:scaletype= "Centercrop"/> 
<textview android:id= 
"@+id/id_index_gallery _item_text " 
android:layout_width=" wrap_content " 
android:layout_height=" Wrap_content " 
android: layout_below= "@id/id_index_gallery_item_image" 
android:layout_centerhorizontal= "true" 
android:layout _marginbottom= "5DP" 
android:layout_margintop= "5DP" 
android:textcolor= "#000000" 
android:text= " Some info " 
android:textsize=" 12DP "/> 

3, Java code

Package com.info.activity; 
Import android.app.Activity; 
Import Android.content.Context; 
Import android.support.v7.app.AppCompatActivity; 
Import Android.os.Bundle; 
Import Android.support.v7.widget.LinearLayoutManager; 
Import Android.support.v7.widget.RecyclerView; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import Android.widget.ImageView; 
Import Android.widget.TextView; 
Import java.util.ArrayList; 
Import Java.util.Arrays; 
Import java.util.List; 
public class Recycleractivity extends activity {private Recyclerview Mrecyclerview; 
Private Galleryadapter Madapter; 
Private list<integer> Mdatas; 
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
Setcontentview (R.layout.activity_recycler); 
Initdatas (); 
Get Control Mrecyclerview = (Recyclerview) Findviewbyid (r.id.id_recyclerview_horizontal); Set Layout manager Linearlayoutmanager Linearlayoutmanager = new Linearlayoutmanager (this); 
Linearlayoutmanager.setorientation (linearlayoutmanager.horizontal); 
Mrecyclerview.setlayoutmanager (Linearlayoutmanager); 
Set adapter Madapter = new Galleryadapter (this, mdatas); 
Mrecyclerview.setadapter (Madapter); private void Initdatas () {Mdatas = new arraylist<> (Arrays.aslist (R.drawable.ic_launcher, R.drawable.ic_launch Er, r.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_ 
Launcher, R.drawable.ic_launcher, R.drawable.ic_launcher)); } public class Galleryadapter extends recyclerview.adapter<galleryadapter.viewholder> {private Layoutinflater M 
Inflater; 
Private list<integer> Mdatas; 
Public Galleryadapter (context context, list<integer> datats) {minflater = Layoutinflater.from (context); 
Mdatas = Datats; 
public class Viewholder extends Recyclerview.viewholder {public viewholder (View arg0) {super (arg0); 
} ImageView mimg; 
TextView Mtxt; @Override public int GetitemcoUnt () {return mdatas.size (); /** * Create Viewholder/@Override public viewholder oncreateviewholder (viewgroup viewgroup, int i) {View view = MI 
Nflater.inflate (R.layout.activity_recycler_item, ViewGroup, false); 
Viewholder Viewholder = new Viewholder (view); 
Viewholder.mimg = (imageview) view. Findviewbyid (R.id.id_index_gallery_item_image); 
return viewholder; /** * Set Value * * @Override public void Onbindviewholder (final viewholder viewholder, final int i) {VIEWHOLDER.MIMG.S 
Etimageresource (Mdatas.get (i));  } 
} 
}

The above is a small set to introduce the Android Recyclerview to achieve horizontal sliding code, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.