Android ViewPager automatically Slide left and right to load Page, androidviewpager
Package zhangphil. auto_viewpager; import java. util. arrayList; import android. support. v4.app. fragment; import android. support. v4.app. fragmentManager; import android. support. v4.app. fragmentPagerAdapter; import android. support. v4.view. viewPager; import android. support. v7.app. actionBarActivity; import android. view. gravity; import android. view. layoutInflater; import android. view. view; import android. view. viewGrou P; import android. widget. textView; import android. graphics. color; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. OS. systemClock;/*** Android ViewPager automatically slides left and right to load the Page. ** Key: automatically cyclically load the left and right sides of the setCurrentItem (int index) of ViewPager. * Idea: Use Android Handler to receive a Message in handleMessage (Message msg) of Handler. After receiving the Message, parse the index to be loaded from msg. * Correspondingly, a thread is opened up, and a Message is sent using Handler in two seconds to trigger handleMessage of Handler, thus calling the setCurrentItem operation of ViewPager. */public class MainActivity extends ActionBarActivity {private ViewPager mViewPager; private final static String ID = "id"; private ArrayList <Fragment> mArrayList; // The total number of Fragment items to be displayed, suppose there are five. private final int SIZE = 5; private Handler handler; private final int WHAT = 0xf01; @ Overrideprotected void onCreate (Bundle SavedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mArrayList = new ArrayList <Fragment> (); for (int I = 0; I <SIZE; I ++) {Fragment f = new TestFragment (); bundle B = new Bundle (); B. putInt (ID, I); f. setArguments (B); mArrayList. add (f);} mViewPager = (ViewPager) findViewById (R. id. viewpager); FragmentPagerAdapter mPagerAdapter = new MyFragmentPagerAdapter (getSup PortFragmentManager (); mViewPager. setAdapter (mPagerAdapter); handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {switch (msg. what) {case WHAT: int index = (Integer) msg. obj; mViewPager. setCurrentItem (index); break ;}}; new Thread (new Runnable () {@ Overridepublic void run () {int I = 0; while (true) {// when the SIZE is exceeded, the loop starts from the beginning. I = I % SIZE; Message message = new Message (); message. what = WHAT; message. obj = I; handler. sendMessage (message); // two seconds off. SystemClock. sleep (2000); I ++ ;}}}). start ();} private class MyFragmentPagerAdapter extends FragmentPagerAdapter {public MyFragmentPagerAdapter (FragmentManager fm) {super (fm) ;}@ Overridepublic Fragment getItem (int pos) {return mArrayList. get (pos) ;}@ Overridepublic int getCount () {return mArrayList. size () ;}/// Fragmnt of the test. Only one Id is displayed, which is used to differentiate different Fragment. Public static class TestFragment extends Fragment {private int Id; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); Bundle B = this. getArguments (); Id = B. getInt (ID) ;}@ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {TextView TV = new TextView (getActivity (); String str = "id: "+ Id; TV. setTextColor (Color. LTGRAY); TV. setText (str); TV. setTextSize (50); TV. setGravity (Gravity. CENTER); return TV ;}}}
Activity_main.xml
<?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:orientation="vertical" > <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout></LinearLayout>
Zookeeper