The general framework of the viewpager in the previous section has been set up. We found that there is a rotation picture, that is, it can't rotate automatically, and can only slide left and right manually, and it can't slide indefinitely. If you slide to the beginning and end in order, you can't move. Next, we'll realize how to make the picture slide indefinitely in front and back, The first thing we need to change is to change the method of setting the content length of getCount() in the adapter to a large enough number
//Set content length @Override public int getCount() { return Integer.MAX_VALUE;//MAX_VALUE = 2147483647 }
Then we need to change the place where the data is filled in the initialization method, because our length is changed to the maximum value, but the set of filled contents is still the original length. If it slides beyond the set length, the program will collapse due to the cross-border of the subscript. Therefore, we also need to change the place where the set content is filled in the pager to the infinite cycle set length. Here, because we have four pictures, we set a number, which is the length of the modular array with the length just set, for example
When we slide to 4, we take the modular set size length 4, that is 0. At this time, the picture with the set position of 0 is displayed
At 5, take mold 4 as 1 and display the picture with position 1
Slide to bit 2 of 6 mold taking 4 to display position 2
And so on, 7% 4 = 3
8% 4 = 0, back again, a pseudo infinite loop becomes, and it won't crash until the user slides to 2147483647 times
public Object instantiateItem(@NonNull ViewGroup container, int position) { //Binding layout View item = LayoutInflater.from(container.getContext()).inflate(R.layout.item,container,false); //Set data ImageView imageView = item.findViewById(R.id.imageView); //Bind imageview first int realPosition = position % Data.size(); imageView.setImageResource(Data.get(realPosition)); //Then fill in the image data in the collection in imageview if (imageView.getParent() instanceof ViewGroup){ ((ViewGroup) imageView.getParent()).removeView(imageView); } //If this line of code is not added, an error will be reported. The modified subclass already has a parent class. Here we judge that if the parent class is group, it will be deleted from the parent class, // If not, add it directly to pager container.addView(imageView); return imageView; }
Here, we just realize the infinity of sliding backward, and the method of sliding forward is also very simple. We only need to write a method to set the position where we fill the content in the set, and the value in it will be filled in the middle of the length just set
/** * How to add pictures to a collection */ private void initData(){ Data.add(R.mipmap.img1); Data.add(R.mipmap.mig2); Data.add(R.mipmap.mig3); Data.add(R.mipmap.mig4); looperpagerAdapter.notifyDataSetChanged(); //Update data viewPager.setCurrentItem(Integer.MAX_VALUE/2); //Set position }
Here is the complete code
package com.example.viewpager; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.viewpager.widget.PagerAdapter; import androidx.viewpager.widget.ViewPager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ViewPager viewPager; //Declare Pager private LooperpagerAdapter looperpagerAdapter = new LooperpagerAdapter(); //Declare Pager adapter private List<Integer> Data = new ArrayList<>(); //Collection for putting pictures @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = findViewById(R.id.viewPager); //Bind Pager viewPager.setAdapter(looperpagerAdapter); //Add adapter initData(); } /** * How to add pictures to a collection */ private void initData(){ Data.add(R.mipmap.img1); Data.add(R.mipmap.mig2); Data.add(R.mipmap.mig3); Data.add(R.mipmap.mig4); looperpagerAdapter.notifyDataSetChanged(); //Update data viewPager.setCurrentItem(Integer.MAX_VALUE/2); //Set position } private class LooperpagerAdapter extends PagerAdapter { //Set content length @Override public int getCount() { return Integer.MAX_VALUE;//MAX_VALUE = 2147483647 } /** * initialization * @param container * @param position * @return */ @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { //Binding layout View item = LayoutInflater.from(container.getContext()).inflate(R.layout.item,container,false); //Set data ImageView imageView = item.findViewById(R.id.imageView); //Bind imageview first int realPosition = position % Data.size(); imageView.setImageResource(Data.get(realPosition)); //Then fill in the image data in the collection in imageview if (imageView.getParent() instanceof ViewGroup){ ((ViewGroup) imageView.getParent()).removeView(imageView); } //If this line of code is not added, an error will be reported. The modified subclass already has a parent class. Here we judge that if the parent class is group, it will be deleted from the parent class, // If not, add it directly to pager container.addView(imageView); return imageView; } /** * Destroy * The function is to achieve recycling without overflow * @param container * @param position * @param object */ @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { //Delete data container.removeView((View) object); } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view == object ; } } }
In this way, no matter sliding forward or backward, you can slide infinitely