public class MainActivity extends Activity implements OnPageChangeListener {
private TextView tv_desc;
private ViewPager viewpager;
private LinearLayout ll_points;
private List<ImageView> list;
private String[] imageDescs;
private int previousCount=0;
private boolean isStop=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
//动态切换
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while(!isStop){
System.out.println("切换页面");
SystemClock.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
viewpager.setCurrentItem(viewpager.getCurrentItem()+1);
}
});
}
}
}).start();
}
//设置页面销毁时页面停止切换
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
isStop=true;
}
public void init(){
//获取控件
viewpager = (ViewPager) findViewById(R.id.vp_test);
tv_desc = (TextView) findViewById(R.id.tv_desc);
ll_points = (LinearLayout) findViewById(R.id.ll_points);
//准备数据
prepare();
//设置适配器
viewpager.setAdapter(new MyPagerAdapter());
viewpager.setOnPageChangeListener(this); //设置页面变更监听器
tv_desc.setText(imageDescs[0]);
ll_points.getChildAt(0).setEnabled(true);
//设置当前显示Integer.MAX_VALUE/2-3
int item=Integer.MAX_VALUE/2-((Integer.MAX_VALUE/2)%list.size());
viewpager.setCurrentItem(item);
}
private void prepare() {
// TODO Auto-generated method stub
int[] imageID={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
imageDescs = new String[] {
"巩俐不低俗,我就不能低俗",
"朴树有回来啦,在唱经典老歌引万人大合唱",
"揭秘北京电影如何升级",
"乐视网TV版大派送",
"热血屌丝的反杀"
};
list=new ArrayList<ImageView>();
for(int i=0;i<imageID.length;i++){
ImageView iv=new ImageView(this);
iv.setBackgroundResource(imageID[i]);
list.add(iv);
//循环添加点
View view=new View(this);
view.setBackgroundResource(R.drawable.point_background);
LayoutParams lp=new LayoutParams(5,5);
lp.leftMargin=5;
view.setLayoutParams(lp);
view.setEnabled(false);
ll_points.addView(view);
}
}
//适配器
public class MyPagerAdapter extends PagerAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}
//销毁
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
int newPosition=position%list.size();
viewpager.removeView(list.get(newPosition));
}
//加载页面
@Override
public Object instantiateItem(View container, int position) {
// TODO Auto-generated method stub
int newPosition=position%list.size();
viewpager.addView(list.get(newPosition));
return list.get(newPosition);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
//页面改变时其他控件随着变化
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
int newPosition=arg0%list.size();
tv_desc.setText(imageDescs[newPosition]);
ll_points.getChildAt(previousCount).setEnabled(false);
ll_points.getChildAt(newPosition).setEnabled(true);
previousCount=newPosition;
}
}
ViewPager
最新推荐文章于 2024-06-25 23:42:12 发布