今天新学知识总计,个人信息,仅供参考:
item设置:
viewpager页面设置:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager1220"
android:layout_width="match_parent"
android:layout_height="200dp">
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignBottom="@id/viewpager1220"
android:padding="10dp"
>
<LinearLayout
android:id="@+id/point1220"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
</RelativeLayout>
adapter设置
package com.example.administrator.test_1216.adapter;
public class PagerAdapter1220 extends PagerAdapter {
private List<ImageModel1220> list;
private Context context;
public PagerAdapter1220(List<ImageModel1220> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
/**
* 判断是否由对象生成页面
* @param view
* @param object
* @return
*/
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
/**
* 添加页面
* @param container 将要显示的页面
* @param position 将要显示页面的位置
* @return
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
//添加子布局
View view=View.inflate(context, R.layout.img1220_item,null);
//声明控件并绑定id
ImageView img1220= (ImageView) view.findViewById(R.id.img1220);
//获取图片
img1220.setBackgroundResource(list.get(position%list.size()).getImages());
//将子布局添加到视图
container.addView(view);
return view;
}
/**
* 销毁页面
* @param container 将要销毁的页面
* @param position 将要销毁的页面的位置
* @param object
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
activity设置:
package com.example.administrator.test_1216;
/**
* Created by Administrator on 2016/12/20.
*/
public class PagerActivity1220 extends Activity implements ViewPager.OnPageChangeListener {
private ViewPager viewPager;
private LinearLayout point;
private PagerAdapter adapter;
private List<ImageModel1220> list;
//图片数组
private int[] imgs = {R.mipmap.main_page1, R.mipmap.main_page2, R.mipmap.main_page3, R.mipmap.main_page4, R.mipmap.main_page5};
//handdle机制处理定时任务
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
handler.sendEmptyMessageDelayed(0,3000); //设置时间间隔
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager1220_activity);
viewPager = (ViewPager) findViewById(R.id.viewpager1220);
point = (LinearLayout) findViewById(R.id.point1220);
//设置图片滑动监听
viewPager.setOnPageChangeListener(this);
//初始化集合
list = new ArrayList<ImageModel1220>();
//遍历图片数组
for (int i = 0; i < imgs.length; i++) {
ImageModel1220 model = new ImageModel1220();
model.setImages(imgs[i]);
list.add(model);
}
//初始化小圆点
for (int i = 0; i < list.size(); i++) {
//新建视图
View v = new View(this);
//新建一个布局参数,设置小圆点宽,高
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(10, 10);
//设置小圆点的间距
if (i != 0) {
params.leftMargin = 10;
}
//设置小圆点视图
v.setLayoutParams(params);
//给小圆点设置背景颜色
v.setBackgroundResource(R.drawable.points);
//将设置好的小圆点添加到布局文件里
point.addView(v);
}
//初始化适配器
adapter = new PagerAdapter1220(list, this);
//设置适配器
viewPager.setAdapter(adapter);
//设置当前的图片位置
//将有用的View放到所有view中间,并将有用的View的第一个视图放到第一个位置
viewPager.setCurrentItem(Integer.MAX_VALUE/2-(Integer.MAX_VALUE/2)%list.size());
changePoint();//动态改变小圆点
//设置自动时间为3秒
handler.sendEmptyMessageDelayed(0,3000);
}
//动态改变小圆点
private void changePoint() {
//获取当前页面的位置
int currentPager = viewPager.getCurrentItem() % list.size();
//遍历小圆点
for (int i = 0; i < point.getChildCount(); i++) {
//设置Enabled,获取小圆点的焦点,当i==currentPager,小圆点是白色
point.getChildAt(i).setEnabled(i == currentPager);
}
}
/**
* 页面滑动结束调用
*
* @param position 选中页面的位置
*/
@Override
public void onPageSelected(int position) {
changePoint();
}
/**
* 此方法在页面滑动的时候调用
*
* @param position:当前页面的位置
* @param positionOffset:页面偏移的百分比
* @param positionOffsetPixels:页面偏移的像素
*/
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
/**
* 此方法在页面状态改变的时候调用
*
* @param state:state==0:什么也没做 state==1:正在滑动
* state==2:滑动结束
*/
@Override
public void onPageScrollStateChanged(int state) {
}
}
效果图: