Android ViewPager循环左右自动滑动加载Page

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
    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.ViewGroup;  
    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循环左右自动滑动加载Page。 
     *   
     * 核心关键:使用 ViewPager的setCurrentItem(int index)左右自动循环加载。 
     * 思路:使用Android Handler,在Handler的handleMessage(Message msg)中收消息,收到消息就从msg中解析出需要加载的索引。 
     * 相应的,开辟一个线程,时隔两秒就用Handler发一个Message,触发Handler的handleMessage,从而调用ViewPager的setCurrentItem操作. 
     */  
      
    public class MainActivity extends ActionBarActivity {  
      
        private ViewPager mViewPager;  
      
        private final static String ID = "id";  
        private ArrayList<Fragment> mArrayList;  
      
        // 用以显示的Fragment总数,假设有5个.  
        private final int SIZE = 5;  
      
        private Handler handler;  
        private final int WHAT = 0xf01;  
      
        @Override  
        protected 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(  
                    getSupportFragmentManager());  
            mViewPager.setAdapter(mPagerAdapter);  
      
            handler = new Handler() {  
                @Override  
                public void handleMessage(Message msg) {  
                    switch (msg.what) {  
                    case WHAT:  
                        int index = (Integer) msg.obj;  
                        mViewPager.setCurrentItem(index);  
                        break;  
                    }  
                }  
            };  
      
            new Thread(new Runnable() {  
      
                @Override  
                public void run() {  
      
                    int i = 0;  
                    while (true) {  
                        // 当超过SIZE后,回绕,从头开始循环。  
                        i = i % SIZE;  
      
                        Message message = new Message();  
                        message.what = WHAT;  
                        message.obj = i;  
                        handler.sendMessage(message);  
      
                        // 休息两秒。  
                        SystemClock.sleep(2000);  
      
                        i++;  
                    }  
                }  
            }).start();  
        }  
      
        private class MyFragmentPagerAdapter extends FragmentPagerAdapter {  
      
            public MyFragmentPagerAdapter(FragmentManager fm) {  
                super(fm);  
            }  
      
            @Override  
            public Fragment getItem(int pos) {  
                return mArrayList.get(pos);  
            }  
      
            @Override  
            public int getCount() {  
                return mArrayList.size();  
            }  
        }  
      
        // 测试的Fragmnt,只显示一个Id,用于区别不同的Fragment。  
        public static class TestFragment extends Fragment {  
      
            private int Id;  
      
            @Override  
            public void onCreate(Bundle savedInstanceState) {  
                super.onCreate(savedInstanceState);  
      
                Bundle b = this.getArguments();  
                Id = b.getInt(ID);  
            }  
      
            @Override  
            public 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>  

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:html5 web worker实现异步执行 计数器

下一篇:JavaScript实现星级评分