viewpager.setOffscreenPageLimit(limit);这个方法,是表示viewpage除了当前显示的页面外,左右个预加载的页面个数,也就是 为limit=2时表示当前一共加载了5个页面
如果getCount()方法返回的Count小于limit 不会出问题,会默认取Count和limit的最小值
package com.musichome.main.course; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; import android.widget.RelativeLayout; import com.musichome.R; import com.musichome.Widget.MyImageView; import com.musichome.base.BaseToolBarActivity; import com.musichome.utils.ImageViewUtils; import com.musichome.utils.LogUtils; import java.lang.reflect.Field; import java.util.ArrayList; import butterknife.Bind; import butterknife.ButterKnife; /** * Created by lidaqiang on 17/4/19. */ public class SwitchInstrumenActivity extends BaseToolBarActivity { @Bind(R.id.vp_switch_activiy_viewpager) ViewPager viewpager; private ArrayList<RelativeLayout> viewList; private ArrayList<String> urlList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_switch_instrument); ButterKnife.bind(this); initToolBar(); addData(); initItemView(); initViewPager(); } private void initViewPager() { viewpager.setAdapter(pagerAdapter); viewpager.setOffscreenPageLimit(2); viewpager.setCurrentItem((Integer.MAX_VALUE / 2)); viewpager.setPageTransformer(true, new ZoomOutPageTransformer()); viewpager.setPageMargin((int) -(getResources().getDimension(R.dimen.pagemargin2))); setViewPagerScrollSpeed(); } private void initItemView() { viewList = new ArrayList<>(); int size = urlList.size(); for (int i = 0; i < size * 4; i++) { RelativeLayout relativeLayout = (RelativeLayout) LayoutInflater.from(getActivity()).inflate(R.layout.item_switch_instrument_activity, null); MyImageView myImageView= (MyImageView)relativeLayout.findViewById(R.id.miv_myimageview); ImageViewUtils.setImageViewForUrl(myImageView,urlList.get(i%urlList.size())); viewList.add(relativeLayout); } } private void addData() { urlList = new ArrayList<>(); urlList.add("http://image.mymusichome.com/ugc_938273b30cbc4c9b8474f6c5b25b5fec_1489797562454"); urlList.add("http://image.mymusichome.com/ugc_a9da4d5f237d44f381ffca0040ef0588_1491812501197"); urlList.add("http://image.mymusichome.com/ugc_938273b30cbc4c9b8474f6c5b25b5fec_1489797562454"); } private void initToolBar() { initToolBarLeft(); setToolBarCenter("切换乐器"); } PagerAdapter pagerAdapter= new PagerAdapter() { @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(viewList.get(position % viewList.size())); } @Override public Object instantiateItem(ViewGroup container, final int position) { View view = viewList.get(position %viewList.size()); ViewParent viewParent = view.getParent(); if (viewParent != null) { viewpager.removeView(view); } container.addView(view); return view; } }; /** * 设置ViewPager的滑动速度 */ private void setViewPagerScrollSpeed() { try { Field mScroller = null; mScroller = ViewPager.class.getDeclaredField("mScroller"); mScroller.setAccessible(true); FixedSpeedScroller scroller = new FixedSpeedScroller(viewpager.getContext()); mScroller.set(viewpager, scroller); } catch (NoSuchFieldException e) { } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } } }
但是在假无线循环中如果设置Count=Integer.MAX_VALUE 但真实viewList只有10个页面,这个时候设置 limit=10就回出现空页面,因为一边加载10个,总共需要现实21个页面,而并没有这么多页面就会出现一个页面同时,需要加载多次,会出现error错误
需要将view去除从父布局移除
@Override public Object instantiateItem(ViewGroup container, final int position) { View view = viewList.get(position %viewList.size()); ViewParent viewParent = view.getParent(); if (viewParent != null) { viewpager.removeView(view); } container.addView(view); return view; }
这时就回出现问题,一共就有10个布局,需要加载21个布局,这样有些页面需要重复加载,同时为了避免布局加载时出现view已有父布局的错误,添加如上代码,这样一个view被第一次加载在viewpage中的左边第一个位置,第二次加载时这个view被我们从父布局viewpage中移除,加载到viewpage中左边的第10个位置,这时候在左边的第一个位置,或者左边的第10个位置一定会有一个页面是空的。这样多次循环情况中空页面就回越来越多