Wemall Doraemon is the Android client program, the server uses Wemall Mall, do not make any changes to the original mall, only need to upload the interface file in the original mall directory to complete the server configuration, the client can be arbitrarily customized modification. This article shares information about adding layouts for Viewpager (Fragment), binding and handling logical relationships between fragments and Viewpager for technician reference learning.
Package Com.inuoer.util;import Java.util.list;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentmanager;import Android.support.v4.app.fragmenttransaction;import Android.support.v4.view.pageradapter;import Android.support.v4.view.viewpager;import Android.view.View;import android.view.viewgroup;/** * Add layout (Fragment) for Viewpager, bind and handle the logical relationship between fragments and Viewpager */public class Fragmentviewpageradapter extends Pageradapter implementsviewpager.onpagechangelistener {private List<Fragment > Fragments; Each fragment corresponds to a pageprivate fragmentmanager fragmentmanager;private viewpager Viewpager; Viewpager object private int currentpageindex = 0; Current page index (before switching) private Onextrapagechangelistener Onextrapagechangelistener; Viewpager additional features when switching pages add interface public fragmentviewpageradapter (Fragmentmanager Fragmentmanager,viewpager Viewpager, List <Fragment> fragments) {this.fragments = Fragments;this.fragmentmanager = Fragmentmanager;this.viewpager = Viewpager;this.viewpager.setadApter (this); This.viewPager.setOnPageChangeListener (this);} @Overridepublic int GetCount () {return fragments.size ();} @Overridepublic Boolean isviewfromobject (view view, Object O) {return view = = O;} @Overridepublic void Destroyitem (ViewGroup container, int position, object object) {Container.removeview (fragments.get (position). GetView ()); Remove page layout outside Viewpager} @Overridepublic Object Instantiateitem (viewgroup container, int position) {Fragment Fragment = Fragments.get (position), if (!fragment.isadded ()) {//If fragment has not addedfragmenttransaction ft = Fragmentmanager.begintransaction (); Ft.add (fragment, Fragment.getclass (). Getsimplename ()); Ft.commit ();/** * After the Fragmenttransaction object is submitted with the Fragmenttransaction.commit () method, it is executed asynchronously in the main thread of the process. If you want to perform this wait operation immediately, call this method (which can only be called in the main thread). * Note that all callbacks and related behaviors are performed in this call, so be sure to carefully confirm the call location of the method. */fragmentmanager.executependingtransactions ();} if (Fragment.getview (). getParent () = = null) {Container.addview (Fragment.getview ());//Add layout Viewpager for}return Fragment.getView ();} /** * Current page index (before switching) * * @return */public int getcurrentpageindex () {return currentpageindex;} Public Onextrapagechangelistener Getonextrapagechangelistener () {return onextrapagechangelistener;} /** * Set page toggle extra Function Listener * * @param onextrapagechangelistener */public void Setonextrapagechangelistener ( Onextrapagechangelistener onextrapagechangelistener) {this.onextrapagechangelistener = OnExtraPageChangeListener;} @Overridepublic void onpagescrolled (int i, float v, int i2) {if (null! = Onextrapagechangelistener) {//If an additional function interface is set up ONEXTR Apagechangelistener.onextrapagescrolled (i, V, I2);}} @Overridepublic void onpageselected (int i) {fragments.get (CurrentPageIndex). OnPause ();//Call Fargment before switching onPause ()// Fragments.get (CurrentPageIndex). OnStop (); Call the OnStop () if (Fragments.get (i). isadded ()) {//Fragments.get (i)) before switching fargment. OnStart ();//Call fargment after switching () Fragments.get (i). Onresume (); Call switch after Fargment onresume ()}currentpageindex = I;if (null! = Onextrapagechangelistener) {//If an additional feature interface one is setXtrapagechangelistener.onextrapageselected (i);}} @Overridepublic void onpagescrollstatechanged (int i) {if (null! = Onextrapagechangelistener) {// If an additional function interface is set onextrapagechangelistener.onextrapagescrollstatechanged (i);}} /** * Page Toggle Additional function Interface */static class Onextrapagechangelistener {public void onextrapagescrolled (int i, float v, int i2) {}pub LIC void onextrapageselected (int i) {}public void onextrapagescrollstatechanged (int i) {}}}
Original Details Address: http://git.oschina.net/zzunet/wemall-doraemon/commit/e8f303df5663dc69fe47bb9623222149d40e3956
Wemall doraemonandroid App Store details address: http://www.koahub.com/home/product/55
Wemall website Address: http://www.wemallshop.com
Wemall Open Source Micro-mall, mall, mall source code, level three distribution, micro-fresh, micro-fruit, micro-takeaway, micro-ordering---professional system
Wemall Java-based T binding and processing of logic relationship codes between fragments and Viewpager in the App Store source code