Previously wrote an article Viewpager: Content content+ Indicator point of the demo;
This article continues to introduce Viewpager: content content+ title of the demo.
Realize:
Source:
Layout file: Activity_main:
<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 " tools:context= ". Mainactivity "> <android.support.v4.view.viewpager android:id=" @+id/viewpager " android:layout _width= "Match_parent" android:layout_height= "match_parent" android:layout_gravity= "center" > <android.support.v4.view.pagertitlestrip android:id= "@+id/pagertitle" android:layout_width= "Wrap_ Content " android:layout_height=" wrap_content " android:layout_gravity=" Top "> </ Android.support.v4.view.pagertitlestrip> </android.support.v4.view.viewpager></relativelayout >
F1.xml (Sports News layout file):
<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 " tools:context= ". Mainactivity "> <ratingbar android:id=" @+id/ratingbar1 " android:layout_width=" Wrap_content " android:layout_height= "wrap_content" android:layout_alignparentleft= "true" android:layout_ Alignparenttop= "true"/></relativelayout>
F2.xml (Entertainment news layout file):
<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 " tools:context= ". Mainactivity "> <progressbar android:id=" @+id/progressbar1 " style="? android:attr/ Progressbarstylelarge " android:layout_width=" wrap_content " android:layout_height=" Wrap_content " android:layout_marginleft= "105DP"/></relativelayout>
F3.xml (Military news layout file):
<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 "Tools:context =". Mainactivity "> <radiogroup android:id=" @+id/radiogroup1 "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_marginleft= "84DP" > <radiobutton Android Oid:id= "@+id/radio0" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:checked= "true" android:text= "RadioButton1"/> <radiobutton android:id= "@+i D/radio1 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Androi d:text= "RadioButton2"/> <radiobutton android:id= "@+id/radio2" android:layout_width= "WR Ap_content "Android:layout_height= "Wrap_content" android:text= "RadioButton3"/> </RadioGroup></RelativeLayout>
Code files:
Mainactivity:
Package Com.fragmentdemo11_viewpager;import Java.util.arraylist;import Java.util.list;import android.app.Activity; Import Android.os.bundle;import Android.support.v4.view.viewpager;import Android.view.layoutinflater;import Android.view.view;public class Mainactivity extends Activity {private Viewpager viewpager;private list<string> Title;private list<view> content;private layoutinflater inflater;private myviewpageradapter adapter;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Viewpager = (Viewpager) Findviewbyid (r.id.viewpager); content = new arraylist<view> (); Inflater = Layoutinflater.from (this);/** * Add Content */content.add (inflater.inflate (R.LAYOUT.F1, null)); Content.add (inflater.inflate (R.LAYOUT.F2, null)), Content.add (inflater.inflate (r.layout.f3, null));/** * Add each content corresponding to the title title */title = new Arraylist<string> (); Title.add ("Sports News"); Title.add ("Entertainment News"); Title.add ("Military News"); Adapter= new Myviewpageradapter (this,content,title); Viewpager.setadapter (adapter);}}
Viewpager Adapter:
<pre name= "code" class= "java" >package com.fragmentdemo11_viewpager;import Java.util.list;import Android.content.context;import Android.support.v4.view.pageradapter;import Android.support.v4.view.ViewPager; Import Android.view.view;import android.view.viewgroup;/** * Viewpager Adapter */public class Myviewpageradapter extends Pageradapter {private Context context;private list<view> content;private list<string> title;public Myviewpageradapter (context context, list<view> content,list<string> title) {This.context = context; this.content = Content;this.title = title;} /** * Initialize */@Overridepublic Object Instantiateitem (viewgroup container, int position) {((Viewpager) container). AddView ( Content.get (position)); return content.get (position);} /** * * Title */@Overridepublic charsequence getpagetitle (int position) {return title.get (position);} @Overridepublic int GetCount () {return content.size ();} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = ARG1;} /** * Destroy */@Overridepublic void Destroyitem (ViewGroup container, int position, object object) {((Viewpager) container). rem Oveview (Content.get (position));}}
Source code Download:
Click to download the source code