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個位置必定會有一個頁面是空的。這樣屢次循環狀況中空頁面就回愈來愈多java