This article shares the specific code of Android carousel diagram infinite loop for your reference. The specific content is as follows
Implement infinite loops
In the getCount() method, a large value is returned, Integer.MAX_VALUE
In the instantiateItem() method, when obtaining the index of the current View, it takes the operation. The passed in int position is a very large number, and it is calculated for the remainder.
In destroyItem() method, the same
In the onPageSelected() listening method, the passed in index is taken from
Infinite loop in reverse
Call the setCurrentItem() method of the ViewPager object to set the current page when you first come in, parameter: int number, we position it in the middle of Integer.MAX_VALUE, and ensure that the first item is the current position, Integer.MAX_VALUE/2-((Integer.MAX_VALUE/2)%())
There is currently a problem with a blank page with left not being solved
package ; import ; import ; import ; import ; import .; import .; import .; import ; import ; import ; import ; import ; public class MainActivity extends Activity { private ViewPager vp_banner; private LinearLayout ll_points; private TextView tv_title; private List<View> banners; private String[] titles=new String[]{ "News Title 1", "News Title 2", "News Title 3" }; private List<ImageView> points=new ArrayList<ImageView>(); @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); // Initialization vp_banner = (ViewPager) findViewById(.vp_banner); ll_points = (LinearLayout) findViewById(.ll_points); tv_title = (TextView) findViewById(.tv_title); // banner part banners = new ArrayList<>(); ImageView img1 = new ImageView(this); (); (img1); ImageView img2 = new ImageView(this); (); (img2); ImageView img3 = new ImageView(this); (); (img3); //Small icon ll_points=(LinearLayout) findViewById(.ll_points); for(int i=0;i<();i++){ ImageView image=new ImageView(this); (.point_selector); params=new (5, 5); =4; (params); if(i==0){ (true); }else{ (false); } (image); ll_points.addView(image); } // Set up the adapter vp_banner.setAdapter(new MyPagerAdapter()); vp_banner.setCurrentItem(Integer.MAX_VALUE/2-((Integer.MAX_VALUE/2)%())); vp_banner.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { tv_title.setText(titles[arg0%()]); //Small icon processing for(ImageView point:points){ (false); } (arg0%()).setEnabled(true); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } }); } private class MyPagerAdapter extends PagerAdapter { @Override public void destroyItem(ViewGroup container, int position, Object object) { ("destroy:"+position); ((View)object); object=null; } @Override public Object instantiateItem(ViewGroup container, int position) { ("initialization:"+position); position=position%(); //Solve the error of right stroke if((position).getParent()!=null){ //((position)); }else{ ((position)); } return (position); } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.