private ViewPager viewPager; private List<String> imgUrls=new ArrayList<>(); private List<ImageView> lists=new ArrayList<>(); private LinearLayout linearLayout; private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); int cim = viewPager.getCurrentItem(); cim++; viewPager.setCurrentItem(cim); //调用方法 setSelect(cim%imgUrls.size()); sendEmptyMessageDelayed(1,1000); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = findViewById(R.id.vp); linearLayout = findViewById(R.id.li); indeData(); MyviewAdapter myviewAdapter = new MyviewAdapter(imgUrls,MainActivity.this); viewPager.setAdapter(myviewAdapter); //默认显示页面 setCurrentItem默认显示页面 viewPager.setCurrentItem(imgUrls.size()*10); handler.sendEmptyMessageDelayed(1,1000); } private void indeData() { imgUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151721118&di=649c9a43aed72fbc4d99ec1a031510c6&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F015c7d574b9f8f6ac72525aee98351.jpg"); imgUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151956771&di=0eb6f306991d24b67a13ceb336f80102&imgtype=0&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farchive%2F00613def3f1beb7a35ae136341be2b589bc46a2d.jpg_320x200.jpg"); imgUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151847685&di=c7a4b5d08ec43fa629bcb690039a7629&imgtype=0&src=http%3A%2F%2Fattimg.dospy.com%2Fimg%2Fday_080625%2F20080625_2e91a10c444877e88827vri2ZKdGMvQo.jpg"); imgUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505151825129&di=70bf74b87d8a15cb91a2d79f15ed0eaf&imgtype=0&src=http%3A%2F%2Fattimg.dospy.com%2Fimg%2Fday_081016%2F20081016_fee215664d5740e56c13E2YB8giERFEX.jpg"); imgUrls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505746504&di=930c4d677a02328a142d6fa85ed14580&imgtype=jpg&er=1&src=http%3A%2F%2Fattimg.dospy.com%2Fimg%2Fday_090113%2F20090113_6ac58b42bea94f0b318e1B6BZb5lPZl5.jpg"); for (int i = 0; i <imgUrls.size() ; i++) { ImageView imageView = new ImageView(this); if(i==0){ imageView.setImageResource(R.drawable.point_selected); }else{ imageView.setImageResource(R.drawable.point_mormal); } linearLayout.addView(imageView); lists.add(imageView); } } private void setSelect(int popop) { for (int j = 0; j <lists.size() ; j++) { if(j==popop){ lists.get(j).setImageResource(R.drawable.point_selected); }else{ lists.get(j).setImageResource(R.drawable.point_mormal); } } }
Viewpager
最新推荐文章于 2024-01-31 22:00:00 发布