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

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>



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。