Last post we introduced the use of Viewpager and fragment to achieve the top slider left and right sliding effect, specific reference (http://blog.csdn.net/a123demi/article/details/39480385).
This blog post will illustrate the use of viewpager to achieve the bottom of the dot navigation left and right sliding effect, as well as the sliding interface to implement monitoring events, and through the fragment to achieve page switching.
For the implementation of this effect, the following issues need to be implemented:
1. How to load and implement bottom dots?
2. How to achieve the left and right sliding effect?
3. How to implement the listener event by sliding the page?
4. How do I switch between pages and other pages?
First, the following results are achieved.
Two. The specific code is as follows
1.XML Layout file
1> Main layout Activity_main.xml
<span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" ><button android:id= "@+id/fragment_switch_btn" android:layout_width= "Match_ Parent " android:layout_height=" wrap_content " android:text=" fragment Toggle " /><linearlayout Android:id= "@+id/fragment_switch_ll" android:layout_width= "match_parent" android:layout_height= "match _parent " android:orientation=" vertical " android:layout_margintop=" 20DP " ></linearlayout > </LinearLayout></span>
Note: LinearLayout with ID fragment_switch_ll is reserved for fragment switchover.
2>fragment page Toggle Layout fragment_other.xml
<span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" ><textview android:layout_width= "match_parent" android:layout_height= "match _parent " android:text=" Another Fragment interface " android:textsize=" 24sp " android:textcolor=" #FF0000 " android:gravity= "center" /> </LinearLayout></span>
3> Slide layout fragment_switch.xml (fragment another layout in the page switch)
<span style= "font-size:18px;" ><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:orien tation= "vertical" ><linearlayout android:id= "@+id/viewgroup" android:layout_width= "Match_parent" Android:la yout_height= "Wrap_content" android:layout_alignparentbottom= "true" android:layout_marginbottom= "40DP" Android:gra Vity= "Center_horizontal" android:orientation= "Horizontal" ></LinearLayout> < Android.support.v4.view.ViewPager android:id= "@+id/viewpager" android:layout_width= "Match_parent" Android:layout_ height= "Match_parent" android:layout_alignparenttop= "true" android:layout_above= "@id/viewgroup" Android:layout_ma Rginbottom= "20DP" ></ANDROID.SUPPORT.V4.VIEW.VIEWPAGER></RELATIVELAYOUT></SPAN>
Note: LinearLayout with ID viewgroup is the parent container that implements the dot ImageView.
4> left and right sliding interface layout View_one.xml (this example with a listener event), View_one.xml,view_one.xml (only one of them, other similar)
<span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:o rientation= "vertical" > <button android:id= "@+id/btn" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:text= "btn" > </Button> <imageview Android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:background= "@ Drawable/image_one "> </ImageView></LinearLayout></span>
2.java Code Implementation
1> Main function Mainactivity.java
<span style= "FONT-SIZE:18PX;" >package Com.example.pagerdemo;import Android.app.activity;import Android.app.fragment;import Android.app.fragmenttransaction;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class Mainactivity extends Activity {private Button fgswitchbtn;private Boolean isswitch = true;//true: Enter Switchfragment,false: Enter otherfragment@overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Main); fgswitchbtn = (Button) This.findviewbyid (R.ID.FRAGMENT_SWITCH_BTN); Fgswitchbtn.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubswitchfragment ();}});} @Overridepublic void Onresume () {super.onresume (); Switchfragment ();} /** * Fragment interface Toggle */private void Switchfragment () {Fragment Fragment = null;if (isswitch) {Fragment = new switchfragment (); }else{fragment = new OtherfragmenT ();} Isswitch =!isswitch; Fragmenttransaction ft = This.getfragmentmanager (). BeginTransaction (); Ft.replace (R.id.fragment_switch_ll, fragment ); Ft.commitallowingstateloss ();}} </span>
Note: The Switchfragment () implements the fragment switch, which solves the problem 4.
2>fragment Switch Page Otherfragment.java
<span style= "FONT-SIZE:18PX;" >package Com.example.pagerdemo;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class OtherFragment Extends Fragment {@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup container,bundle Savedinstancestate) {Super.oncreateview (Inflater, container, savedinstancestate); View Otherview = inflater.inflate (R.layout.fragment_other, Container,false); return otherview;}} </span>
3>fragment Switch interface and sliding interface Switchfragment.java
<span style= "FONT-SIZE:18PX;" >package com.example.pagerdemo;import java.util.arraylist;import Java.util.list;import android.app.Activity; Import Android.app.fragment;import Android.content.context;import Android.os.bundle;import Android.support.v4.view.viewpager;import Android.support.v4.view.viewpager.onpagechangelistener;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.view.view.onclicklistener;import Android.view.viewgroup.layoutparams;import Android.widget.Button;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.toast;public Class Switchfragment extends Fragment {private linearlayout groupviewll;private Viewpager viewpager;private imageview[] Imageviews;private ImageView imageview;private Button btn;private list<view> viewlist = new arraylist<view> ();p rivate layoutinflater minflater;public View oncreateview (layoutinflater inflater, ViewGroup container,Bundle SavedinstanceState) {Super.oncreateview (Inflater, container, savedinstancestate); View SwitchView = inflater.inflate (R.layout.fragment_switch, container,false); minflater = Inflater;groupviewll = ( LinearLayout) Switchview.findviewbyid (r.id.viewgroup); Viewpager = (Viewpager) Switchview.findviewbyid ( R.id.viewpager); return switchView;} @Overridepublic void onactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (savedinstancestate);/** * Add View that needs sliding to viewlist */view OneView = minflater.inflate (R.layout.view_one, null); Viewlist.add (OneView); Viewlist.add (minflater.inflate (r.layout.view_two, null)); Viewlist.add (Minflater.inflate, NULL));/** * defines a dot slide navigation ImageView, depending on the number of view */imageviews = new Imageview[viewlist.size ()];<span style= "White-space :p re "></span>//sliding interface Event monitoring, problem solving 3btn = (Button) Oneview.findviewbyid (R.ID.BTN); Btn.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {toast.maketext (SwitchFragment.this.getActivity (), "This is View_one", Toast.length_short). Show ();}}); <span style= "White-space:pre" ></span>//dynamically generate ImageView, to achieve the bottom of the dot (the number of dots based on the left and right sliding view), solve the problem 1</span> <span style= "font-family:arial, Helvetica, Sans-serif;" ></span><span style= "FONT-SIZE:18PX;" >for (int i = 0; i < viewlist.size (); i++) {ImageView = new ImageView (this.getactivity ()); Imageview.setlayoutparams ( New Layoutparams); imageview.setpadding (0, 0); Imageviews[i] = imageview;if (i = = 0) {//The first picture is selected by default Imageviews [I].setbackgroundresource (r.drawable.page_indicator_focused);} else {imageviews[i].setbackgroundresource (r.drawable.page_indicator);} Groupviewll.addview (Imageviews[i]);} <span style= "White-space:pre" ></span>//slide adapter for sliding effect, solve problem 2viewpager.setadapter (new Mypageradapter ( viewlist)); Viewpager.setonpagechangelistener (new Switchpagechangelistener ());} @Overridepublic void Onresume () {Super.onresume ();} Directs the page to change the event listener, setting the background change when the dots slide. Class Switchpagechangelistener implements Onpagechangelistener {@Overridepublic void onpagescrollstatechanged (int arg0) {} @Overridepublic void onpagescrolled (int arg0, float arg1, int arg2) { } @Overridepublic void onpageselected (int arg0) {for (int i = 0; i < imageviews.length; i++) {imageviews[arg0].setbackgr Oundresource (r.drawable.page_indicator_focused), if (arg0! = i) {Imageviews[i].setbackgroundresource ( R.drawable.page_indicator);}}}} </span>
Note: The Switchpagechangelistener achieves the effect of sliding discoloration of the bottom navigation dots.
4> Slide Adapter Mypageradapter.java
<span style= "FONT-SIZE:18PX;" >package Com.example.pagerdemo;import Java.util.list;import Android.os.parcelable;import Android.support.v4.view.pageradapter;import Android.support.v4.view.viewpager;import Android.view.View;public Class Mypageradapter extends Pageradapter {private list<view> viewlist;public mypageradapter (list<view> viewlist) {this.viewlist = viewlist;} @Overridepublic int GetCount () {return viewlist.size ();} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = arg1;} @Overridepublic int GetItemPosition (Object object) {return Super.getitemposition (object);} @Overridepublic void Destroyitem (View arg0, int arg1, Object arg2) {(Viewpager) arg0). Removeview (Viewlist.get (Arg1));} @Overridepublic Object Instantiateitem (View arg0, int arg1) {((Viewpager) arg0). AddView (Viewlist.get (arg1)); return Viewlist.get (arg1);} @Overridepublic void Restorestate (parcelable arg0, ClassLoader arg1) {} @Overridepublic parcelable saveState () {return null;} @Overridepublic void Startupdate (view arg0) {} @Overridepublic void Finishupdate (view arg0) {}}</span> Note: The effect of the page changes when the adapter implements the page swipe by adding or removing view
This is all the content of this blog post.
Source: http://download.csdn.net/detail/a123demi/7960273
Android uses Viewpager to achieve the bottom of the dot navigation left and right sliding effects and fragment page switching