1.准备填充ViewPager的fragment
public class ImageDetailFragment extends Fragment {
private static final String IMAGE_DATA_EXTRA = "resId";
private int mImageNum;
private ImageView mImageView;
static ImageDetailFragment newInstance(int imageNum) {
final ImageDetailFragment f = new ImageDetailFragment();
final Bundle args = new Bundle();
args.putInt(IMAGE_DATA_EXTRA, imageNum);
f.setArguments(args);
return f;
}
// Empty constructor, required as per Fragment docs
public ImageDetailFragment() {}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mImageNum = getArguments() != null ? getArguments().getInt(IMAGE_DATA_EXTRA) : -1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// image_detail_fragment.xml contains just an ImageView
final View v = inflater.inflate(R.layout.image_detail_fragment, container, false);
mImageView = (ImageView) v.findViewById(R.id.imageView);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final int resId = ImageDetailActivity.imageResIds[mImageNum];
//对图片进行压缩,参见[bitmap加载](http://www.jianshu.com/p/fa67521c1e57)
mImageView.setImageBitmap(ImageResizer.decodeSampledBitmapFromResource(getResources(),resId,100,100));
}
}
2.ViewPager的Activity
public class ImageDetailActivity extends AppCompatActivity {
private static final String TAG = "ImageDetailActivity";
private ImagePagerAdapter mAdapter;
private ViewPager mPager;
private Handler mHandler;
private static int mPosition;
public static final int WHAT = 1001;
public static final int COUNT = 300;
public final static Integer[] imageResIds=new Integer[]{R.drawable.ho1,R.drawable.ho2,R.drawable.ho3,R.drawable.ho4,R.drawable.ho5,R.drawable.ho6 };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_detail_pager); // Contains just a ViewPager
mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), imageResIds.length*COUNT);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(imageResIds.length*COUNT/2);
mPosition=imageResIds.length*COUNT/2;
mHandler=new MyHandler(mPager);
mHandler.sendEmptyMessageDelayed(WHAT,3000);
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mPosition=position;
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private static class MyHandler extends Handler
{
//使用静态内部类避免内存泄漏
private WeakReference<ViewPager> mViewPagerWeakReference;
public MyHandler(ViewPager viewPager)
{
mViewPagerWeakReference=new WeakReference<ViewPager>(viewPager);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what)
{
case WHAT:
if (mViewPagerWeakReference.get()!=null)
{
mViewPagerWeakReference.get().setCurrentItem(mPosition+1);
sendEmptyMessageDelayed(WHAT,3000);
Log.d(TAG, "handleMessage: ");
}
break;
}
}
}
private static class ImagePagerAdapter extends FragmentStatePagerAdapter {
private final int mSize;
public ImagePagerAdapter(FragmentManager fm, int size) {
super(fm);
mSize = size;
}
@Override
public int getCount() {
return mSize;
}
@Override
public Fragment getItem(int position) {
return ImageDetailFragment.newInstance(position%imageResIds.length);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//移除消息避免一直运行
mHandler.removeMessages(WHAT);
}
}