Android Development Step-by-step 53:viewpager+fragment construction lets you swipe the tab effect left and right

Source: Internet
Author: User

Sliding tab is a lot of applications used to do outside the framework, such as Weibo, and so on, I realize the effect is the following is the Main tab, and then the middle of a tab inside and can continue to slide left and right, is the label page inside and then nested tabs, used to the main components are: for the sliding effect of the viewpager, and the fragment to implement each tab function, look first:

First step: Design page Activity_learn_fragment.xml

<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 "> &lt ; Android.support.v4.view.ViewPager android:id= "@+id/viewpager" android:layout_width= "Fill_parent" and roid:layout_height= "Fill_parent"/> <linearlayout android:layout_width= "Fill_parent" android:layou t_height= "Wrap_content" android:id= "@+id/layout_tab" android:layout_alignparentbottom= "true" Android: orientation= "Horizontal" > <textview android:id= "@+id/tv_earn" Android:layout_width= "Ma Tch_parent "android:layout_height=" 50DP "android:layout_weight=" 1 "android:background=" #9        96699 "android:gravity=" center "android:text=" Earn "android:textcolor=" #222222 "/> <textview android:id= "@+id/Tv_withdraw "android:layout_width=" match_parent "android:layout_height=" 50DP "Android:lay out_weight= "1" android:background= "#999999" android:gravity= "center" android:text= "Withdr Aw "android:textcolor=" #222222 "/> <textview android:id=" @+id/tv_me "Android : layout_width= "match_parent" android:layout_height= "50DP" android:layout_weight= "1" Andro Id:background= "#999999" android:gravity= "center" android:text= "Me" android:textcolor= "#22 2222 "/> </LinearLayout></RelativeLayout>



Step Two: Write Activity,fragmenttestactivity.java

/** * */package com.figo.study;import java.util.arraylist;import android.graphics.color;import android.os.Bundle; Import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentactivity;import Android.support.v4.app.fragmentmanager;import Android.support.v4.app.fragmenttransaction;import Android.support.v4.view.viewpager;import Android.support.v4.view.viewpager.onpagechangelistener;import Android.util.log;import Android.view.view;import Android.view.window;import Android.widget.textview;import Com.figo.study.adapter.tabfragmentpageradapter;import Com.figo.study.fragment.earnfragment;import Com.figo.study.fragment.examplefragment;import Com.figo.study.fragment.mefragment;import com.figo.study.fragment.withdrawfragment;/** * @author Figo * */public class Fragmenttestactivity extends fragmentactivity {private TextView Tv_earn, Tv_withdraw, tv_me;private arraylist<fragment> fragmentslist; Private Viewpager vp;private int currindex; @Overridepublic void OnCreate (Bundle saVedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (r.layout.activity_learn_fragment); Initviews (); Initviewpager ();} private void Initviews () {Tv_earn = (TextView) Findviewbyid (r.id.tv_earn); Tv_withdraw = (TextView) Findviewbyid (r.id.tv _withdraw); Tv_me = (TextView) Findviewbyid (R.id.tv_me); Tv_earn.setonclicklistener (new Tabonclicklistener (0)); tv_ Withdraw.setonclicklistener (New Tabonclicklistener (1)), Tv_me.setonclicklistener (New Tabonclicklistener (2)); private void Initviewpager () {try {VP = (Viewpager) Findviewbyid (r.id.viewpager); fragmentslist = new arraylist< Fragment> (); Bundle bundle = new bundle (); Fragment mefragment = mefragment.newinstance (fragmenttestactivity.this, bundle); Fragment earnfragment = earnfragment.newinstance (fragmenttestactivity.this, bundle); Fragment withdrawfragment = withdrawfragment.newinstance (fragmenttestactivity.this, bundle); Fragmentslist.add ( earnfragment); Fragmentslist.add (withdrawfragment); fRagmentslist.add (mefragment); Vp.setadapter (New Tabfragmentpageradapter (Getsupportfragmentmanager (), fragmentslist)); Vp.setcurrentitem (0); Vp.setonpagechangelistener (new Myonpagechangelistener ()); catch (Exception e) {log.e ("Initviewpager", "Initviewpager", E);}} public class Tabonclicklistener implements View.onclicklistener {private int index = 0;public tabonclicklistener (int i) {i Ndex = i;} @Overridepublic void OnClick (View v) {vp.setcurrentitem (index);}}; public class Myonpagechangelistener implements Onpagechangelistener {@Overridepublic void onpageselected (int arg0) { Switch (arg0) {case 0:tv_earn.settextcolor (color.blue); Tv_withdraw.settextcolor (Color.Black); Tv_me.settextcolor ( Color.Black); Tv_earn.setbackgroundcolor (0xff996699); Tv_withdraw.setbackgroundcolor (0xFF999999); tv_ Me.setbackgroundcolor (0xff999999); Break;case 1:tv_earn.settextcolor (Color.Black); Tv_withdraw.settextcolor ( Color.Blue); Tv_me.settextcolor (Color.Black); Tv_earn.setbackgroundcolor (0xff999999); tv_withdraw.setBackgroundColor (0xff996699); Tv_me.setbackgroundcolor (0xff999999); Break;case 2:tv_earn.settextcolor (Color.BLACK); tv_ Withdraw.settextcolor (Color.Black); Tv_me.settextcolor (Color.Blue); Tv_earn.setbackgroundcolor (0xFF999999); tv_ Withdraw.setbackgroundcolor (0xff999999); Tv_me.setbackgroundcolor (0xff996699); break;} Currindex = arg0;} @Overridepublic void onpagescrolled (int arg0, float arg1, int arg2) {} @Overridepublic void onpagescrollstatechanged (int a rg0) {}}}


Step three: Write the fragment of each tab page

1,Mefragment.java

/** * */package com.figo.study.fragment;import com.figo.study.r;import Com.figo.study.r.id;import Com.figo.study.r.layout;import Android.content.context;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.button;import Android.widget.textview;import android.widget.Toast;/* * * @author Figo * */public class Mefragment extends Android.support.v4.app.Fragment {static Context mcontext; Bundle Mebundle;public Static Mefragment newinstance (Context context,bundle bundle) {mcontext=context;        Mefragment newfragment = new Mefragment ();        Newfragment.setarguments (bundle);    return newfragment; } @Overridepublic void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate);} @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {//TODO auto-generated Method Stub VieW view = Inflater.inflate (R.layout.fragment_me, container, false);        TextView Tv_me = (TextView) View.findviewbyid (R.id.tv_me); Tv_me.settext ("Born of my material will be useful!")        ");        Button Btn_me = (button) View.findviewbyid (R.id.btn_me); Btn_me.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method        Stub//getarguments () Toast.maketext (Mcontext, "earn more money!", Toast.length_long). Show ();}); return view;}}


2,Earnfragment.java

/** * */package com.figo.study.fragment;import com.figo.study.r;import Com.figo.study.r.id;import Com.figo.study.r.layout;import Android.content.context;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.button;import Android.widget.textview;import android.widget.Toast;/* * * @author Figo * */public class Earnfragment extends Android.support.v4.app.Fragment {static Context mcontext; Bundle Mebundle;public Static Earnfragment newinstance (Context context,bundle bundle) {mcontext=context;        Earnfragment newfragment = new Earnfragment ();        Newfragment.setarguments (bundle);    return newfragment; } @Overridepublic void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate);} @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {//TODO Auto-generated methodStub view view = Inflater.inflate (R.layout.fragment_earn, container, false);        TextView Tv_me = (TextView) View.findviewbyid (R.id.tv_earn); Tv_me.settext ("Making money is the foundation of the superstructure!        ");        Button Btn_earn = (button) View.findviewbyid (R.id.btn_earn); Btn_earn.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method        Stub//getarguments () Toast.maketext (Mcontext, "earn more money!", Toast.length_long). Show ();}); return view;}}


3,withdrawfragment. Java

/** * */package com.figo.study.fragment;import java.util.arraylist;import java.util.list;import Com.figo.study.R; Import Com.figo.study.r.id;import Com.figo.study.r.layout;import Com.figo.study.adapter.viewpageradapter;import Android.content.context;import Android.graphics.color;import Android.os.bundle;import Android.support.v4.view.viewpager;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.animation.translateanimation;import Android.view.ViewGroup ; Import Android.widget.button;import android.widget.textview;import android.widget.toast;/** * @author Figo * */public Class Withdrawfragment extends Android.support.v4.app.Fragment {static Context mcontext; Bundle mebundle;private list<view> lists = new arraylist<view> ();p rivate TextView tv_goods;private TextView Tv_card;private TextView tv_lottery;int white = color.white;int blue = Color.Blue; Viewpager viewpager;public static withdrawfragment newinstance (Context context, bundle bundle) {Mcontext = context; Withdrawfragment newfragment = new Withdrawfragment (); newfragment.setarguments (bundle); return newfragment;} @Overridepublic void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate);} @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {//TODO Auto-generated method Stubview view = Inflater.inflate (R.layout.fragment_withdraw, Container,false); InitViewPager ( view);//TextView Tv_me = (TextView) View.findviewbyid (R.id.tv_withdraw);//Tv_me.settext ("Life must learn to enjoy!") ")//Button Btn_withdraw = (button) View.findviewbyid (R.id.btn_withdraw);//Btn_withdraw.setonclicklistener (new Onclicklistener () {////@Override//public void OnClick (View v) {////TODO auto-generated method stub////Getarguments () Toast.maketext (Mcontext, "earn more money!",//Toast.length_long). Show ();//}//}); return view;} private void Initviewpager (view view) {Lists.add (GetaCtivity (). Getlayoutinflater (). Inflate (R.layout.layout1,null)); Lists.add (Getactivity (). Getlayoutinflater (). Inflate (R.layout.layout2,null)); Lists.add (Getactivity (). Getlayoutinflater (). Inflate (R.layout.layout3,null)); TV _goods = (TextView) View.findviewbyid (r.id.tv_goods); Tv_card = (TextView) View.findviewbyid (r.id.tv_card); tv_lottery = (TextView) View.findviewbyid (r.id.tv_lottery); Viewpageradapter myadapter = new Viewpageradapter (lists); Viewpager = (Viewpager) View.findviewbyid (R.id.vpType); Viewpager.setadapter (Myadapter); Viewpager.setonpagechangelistener (new Viewpager.onpagechangelistener () {@ overridepublic void onpageselected (int arg0) {//when sliding, the top imageview is slowly sliding through animation//TODO auto-generated method Stubswitch (arg0) {case 0://textview1.setbackgroundcolor (0x3a6a00);//Textview2.setbackgroundcolor (0x999999);// Textview3.setbackgroundcolor (0x999999); Tv_goods.setbackgroundcolor (0xff996699); Tv_card.setbackgroundcolor ( 0xff999999); Tv_lottery.setbackgroundcolor (0xff999999); Tv_goods.settexTColor (blue), Tv_card.settextcolor (white), tv_lottery.settextcolor (white); Break;case 1:tv_ Goods.setbackgroundcolor (0xff999999); Tv_card.setbackgroundcolor (0xff996699); Tv_lottery.setbackgroundcolor ( 0xff999999); Tv_goods.settextcolor (white), Tv_card.settextcolor (blue); Tv_lottery.settextcolor (white); Break;case 2 : Tv_goods.setbackgroundcolor (0xff999999); Tv_card.setbackgroundcolor (0xff999999); Tv_lottery.setbackgroundcolor ( 0xff996699); Tv_goods.settextcolor (white), tv_card.settextcolor (white); Tv_lottery.settextcolor (blue);} @Overridepublic void onpagescrolled (int arg0, float arg1, int arg2) {//TODO auto-generated method stub} @Overridepublic vo ID onpagescrollstatechanged (int arg0) {//TODO auto-generated Method stub}}); Tv_goods.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated method Stubviewpager.setcurrentitem (0);}); Tv_card.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated MetHod Stubviewpager.setcurrentitem (1);}); Tv_lottery.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO Auto-generated method Stubviewpager.setcurrentitem (2);}});}}


Android Development Step-by-step 53:viewpager+fragment construction lets you swipe the tab effect left and right

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.