/**
* 无限滑动+自动无限轮播
* handler无限滑动
* 出现一个触摸的冲突—:在触摸界面的时候 handler取消发送消息,不触摸之后 继续发送
*
* 1.使用viewPager显示界面
* 2.手动的实现无限轮播
* 3.使用handler实现自动轮播
* 在进入界面的时候发送空的延时消息
* 处理消息:设置要显示的界面viewPager.setCurrentItem(current+1)
* 再次发送消息 循环播放—->此时会出现自动轮播与手动触摸的冲突
*/
public class MainActivity extends Activity {
protected static final String TAG = "MainActivity";
private ViewPager viewPager;
private RadioGroup group;
private List<Integer> list;
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
//发送延时消息 接收到消息之后向下选择一页
if (msg.what == 0) {
//返回当前页的位置
int current = viewPager.getCurrentItem();
viewPager.setCurrentItem(current+1);
//再次发送
handler.sendEmptyMessageDelayed(0, 1000);
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//1.这个方法是去掉titleBar 必须在setContentView()之前调用 否则报错
//2.使用主题的方式 去掉actionbar或者全屏显示
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//显示不同页面的控件 可以左右滑动加载界面的显示
viewPager = (ViewPager) findViewById(R.id.view_pager);
group = (RadioGroup) findViewById(R.id.group);
list = new ArrayList<Integer>();
list.add(R.drawable.a);
list.add(R.drawable.b);
list.add(R.drawable.c);
//创建适配器 设置
ViewPagerAdapter adapter = new ViewPagerAdapter(MainActivity.this,list,handler);
viewPager.setAdapter(adapter);
handler.sendEmptyMessageDelayed(0, 1000);
viewPager.setCurrentItem(list.size()*10000);
//页面改变的时候 的监听事件
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
//选中某个界面的方法
@Override
public void onPageSelected(int position) {
//根据position拿到radioButton 再 拿到radioButton的id
//group.check(group.getChildAt(position%list.size()).getId());
//Log.d(TAG, "position---"+position);
}
//int position, float positionOffset 偏移量 从零到1 包含0不包含1,int positionOffsetPixels偏移的像素值 从0到屏幕的宽度
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
//Log.d(TAG, "position---"+position+"--positionOffset---"+positionOffset+"--positionOffsetPixels--"+positionOffsetPixels);
}
@Override
public void onPageScrollStateChanged(int state) {
//Log.d(TAG, "state---"+state);
/**
* public static final int SCROLL_STATE_IDLE = 0; 静止状态
public static final int SCROLL_STATE_DRAGGING = 1; 拖动状态
public static final int SCROLL_STATE_SETTLING = 2; 拖动松开回弹状态
*/
}
});
}
}
/**
*
* 1.@author 2.@date 2017-2-17 3.@description
*
* ViewPager如果显示三张图片的话 会出现bug– You must call removeView() on the child’s parent
* first. 因为我们集合的泛型写的是View,,,这个时候集合中只有三个固定的对象,,,ViewPager前后加载一页,他的容器里面
* 会有三个对象,,但是滑动的时候会调用instantiateItem()去添加View,因为只有三个对象destroyItem()没有去销毁对象
* 所以添加不进去相同的view对象
*
* 更改的方法:集合的泛型不适用View,而是集合只传递数据
*
*/
public class ViewPagerAdapter extends PagerAdapter {
List<Integer> list;
Handler handler;
Context context;
public ViewPagerAdapter(Context context, List<Integer> list, Handler handler) {
// TODO Auto-generated constructor stub
this.list = list;
this.handler = handler;
this.context = context;
}
// 返回要显示的页面个数
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
// 如果当前位置的view跟instantiateItem(ViewGroup, int)返回的object的key相同 就返回true
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(context);
imageView.setImageResource(list.get(position % list.size()));
imageView.setScaleType(ScaleType.FIT_XY);
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
handler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_MOVE:
handler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_CANCEL:
handler.sendEmptyMessageDelayed(0, 1000);
break;
case MotionEvent.ACTION_UP:
handler.sendEmptyMessageDelayed(0, 1000);
break;
default:
break;
}
return true;
}
});
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView((View) object);
}
}