List<ImageView> IVlist=new ArrayList<ImageView>();
/** * 加点 */private void initDot() { for (int i = 0; i < image.length; i++) { ImageView lv=new ImageView(getActivity()); if(i==0) { lv.setImageResource(R.drawable.item_red); }else { lv.setImageResource(R.drawable.item_green); } //给线性布局设置狂傲 LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(20,20); params.setMargins(35,0,35,0); tv_linear.addView(lv,params); IVlist.add(lv); }}vp2.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { for (int i = 0; i < IVlist.size(); i++) { if (i==position%image.length) { IVlist.get(i).setImageResource(R.drawable.item_red); }else { IVlist.get(i).setImageResource(R.drawable.item_green); } } } @Override public void onPageScrollStateChanged(int state) { } });//图片跳转时间 handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what==0) { int currentItem = vp2.getCurrentItem(); currentItem++; vp2.setCurrentItem(currentItem); handler.sendEmptyMessageDelayed(0, 2000); } } }; handler.sendEmptyMessageDelayed(0, 2000); vp2.setAdapter(new OtherAdapter());/** * Viewpager适配器 */ class OtherAdapter extends PagerAdapter { private View view; @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { view = View.inflate(getActivity(), R.layout.item_vp2, null); ImageView image_vp2 = view.findViewById(R.id.image_vp2); image_vp2.setImageResource(image[position%image.length]); container.addView(view); return view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }
加点,无限轮播
最新推荐文章于 2022-04-22 09:18:58 发布