效果图
首先,让我们了解下什么是viewpager:
这个大致意思是:这个布局管理器允许用户左右翻转并传递页面的数据,你想要的视图可以通过pagerAdapter来实现。
布局文件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_centerInParent="true"
android:background="#aadcff"
android:clipChildren="false">
<android.support.v4.view.ViewPager
android:id="@+id/view_Pager"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_gravity="center"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:clipChildren="false" />
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="10dp"
android:orientation="horizontal" />
</FrameLayout>
布局文件很简单,只有个用于展示图片的viewpager和用于同步展示图片的radioGroup
pagerAdapter
pagerAdapter是android.support.v4包中的类,它的子类有fragmentPagerAdapter、 fragmentStatePagerAdapter,这两个adapter都是fragment的适配器,用于实现fragment的滑动效果。
PagerAdapter pagerAdapter = new PagerAdapter() {
@Override
public int getCount() {
return Integer.MAX_VALUE;
// return images.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
//判断当前要显示的画面
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
position = position % images.length;
ImageView imageView = new ImageView(ViewPagerActivity.this);
imageView.setImageResource(images[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
};
pagerAdapter内有四个方法:
getCount():要滑动的图片数量,这里用Integer.MAX_VALUE,目的是为了实现无限轮播。
isViewFromObject&#x