👉 About the author

As we all know, life is a long process of overcoming difficulties, reflecting and moving forward. In this process, there will be a lot of questions and thinking about life, so I decided to share my own thinking, experience and story, in order to find resonance!! Focus on Android/Unity and various game development skills, as well as various resource sharing (website, tools, materials, source code, games, etc.)

👉 Soon to learn

Fix bugs, ha ha – bugs are heartbreaking.

👉 background

The day before yesterday, a friend sent a picture saying that the viewpager slide problem

🙎 Xiaozhi: IT was me who brought it up.

🙈 Xiao Kong: Um……

👉 Practice Process

Bug:

  1. A blank page appears when you swipe
  2. A View uses both Parent

Realization of original technology: data source only 2 pictures, with the realization of pseudo infinite rotation point

Reasons for the first question:

  1. When the number of pages =1, the page is still;
  2. If the number of pages =>2, page sliding does not generate blank space
  3. The infinite loop ViewPager will always report an error if it skates less than 3 pages

Therefore, we can simulate some basic ideas of pictures: when the number of pictures is 2, add them in the order of the original pictures, and the number of pictures doubles to 4 to prevent blank pages, but the number of rotation points is still written as 2

So the first problem has been solved (not finished), don’t worry, continue to see. After searching for a long time, I failed to find enough perfect, or simple, ViewPager adapter code, so I also downloaded a few DEMO tests, infinite loop ViewPager in the roller-skating page less than 3 will always report an error.

The Parent class can’t use the same View at the same time. The Parent class can’t use the same View at the same time. The Parent class can’t use the same View at the same time.

// // get the parent component of the view and remove the View ViewGroup parent = (ViewGroup) v.getParent(); if (parent ! = null) { parent.removeAllViews(); }Copy the code

When loading and removing, add the following code, although no error, but when sliding the other page will be full of empty pages, in order to load the view we want, which is obviously not what we want.

@Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, int position) { View v = list.get(position % list.size()); ViewGroup parent = (ViewGroup) v.getParent(); if (parent ! = null) { parent.removeView(v); } container.addView(v); return v; } @Override public void destroyItem(ViewGroup container, int position, Object object) { } @Override public int getCount() { if (list == null) { return 0; } return Integer.MAX_VALUE; }Copy the code

Problem solved!

Such an infinite loop ViewPager adapter has been made, there are many things, listening, navigation introduction, scroll dot page number and so on.

📢 by xiao Kong and xiao Kong in Xiao Zhi 📢. 📢 Welcome to like 👍 save 🌟 leave 📝