fragment+viewpager的懒加载
公司的app中是使用viewpager加载fragment:每个viewpager页面都是一个fragment,fragment的内容从网上获取,需要实现的功能是当切换viewpager的不同页面时,能流畅的显示已经加载过的页面。
实现的方法很简答。因为viewpager有缓存的功能。
mViewPager.setOffscreenPageLimit(6);
只需添加这一句话就能实现:对于已经加载过的fragment,当被切换后,不会触发该fragment的任何事件。当切换回来时可以正常显示。
需要注意的是:这句话的功能 是设置预缓存的fragment(或其他view)的个数。如果设置数字过大,当你加载当前fragment时还会预加载未显示的fragment导致加载当前页面过慢。所以这个数字应该选择合适的值。
详细内容博客链接:http://blog.csdn.net/zhouzme/article/details/19707677
- package com.cosmosource.mbi.fragment;
- import java.util.ArrayList;
- import java.util.List;
- import cn.jiabeis.andr.log.CommonLog;
- import cn.jiabeis.andr.log.LogFactory;
- import com.cosmosource.mbi.MainActivity;
- import com.cosmosource.mbi.R;
- import com.cosmosource.mbi.adapter.TabPagerAdapter;
- import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
- import android.os.Bundle;
- import android.os.Handler;
- import android.support.v4.app.Fragment;
- import android.support.v4.view.PagerAdapter;
- import android.support.v4.view.ViewPager;
- import android.support.v4.view.ViewPager.OnPageChangeListener;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.ViewParent;
- /**
- * @description viewerPager的fragment
- * @author herb
- * @version 0.1
- * @since 2013-11-18 上午10:29:37
- */
- public class ViewPagerFragment extends CommonFragment{
- private static final CommonLog log = LogFactory.createLog();
- /**
- * ViewPager对象的引用
- */
- public ViewPager mviewPager;
- /**
- * 装载Fragment的容器,我们的每一个界面都是一个Fragment
- */
- private List<Fragment> mfragmentList = new ArrayList<Fragment>();
- /**
- * 用于记录已经浏览过的fragment,不再重新从网上加载,因为当切换fragemnt时viewpager的缓存功能
- * 不会触发fragment的任何事件。可以完整保存。
- * jkx
- */
- private List<Fragment> browsedfragmentList = new ArrayList<Fragment>();
- private TabPagerAdapter tpa = null;
- public List<Fragment> getMfragmentList() {
- return mfragmentList;
- }
- public void setMfragmentList(List<Fragment> mfragmentList) {
- this.mfragmentList = mfragmentList;
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- if(mView==null){
- mView = inflater.inflate(R.layout.frame_viewpager_layout, null);
- mviewPager = (ViewPager)mView.findViewById(R.id.viewPager);
- //设置监听
- mviewPager.setOnPageChangeListener(new MySetOnPageChangeListener());
- //缓存多个子页面,预缓存16个菜单项
- mviewPager.setOffscreenPageLimit(15);
- /*解决:java.lang.IllegalStateException: Recursive entry to executePendingTransaction*/
- new Handler().postDelayed(new Runnable() {
- @Override
- public void run() {
- tpa = new TabPagerAdapter(getFragmentManager(), mfragmentList);
- mviewPager.setAdapter(tpa);
- mviewPager.setCurrentItem(0);
- /*这里加载url*/
- if (null != mfragmentList && mfragmentList.size() > 0) {
- //二级导航有多个,,默认显示第0个导航对应的fragment
- Fragment ff = mfragmentList.get(0);
- if (null != ff && ff instanceof WebviewFragment) {
- ((WebviewFragment)ff).cusLoadURL();
- }
- }
- }
- }, 10);
- }else{
- System.out.println("ViewPagerFragment:mView不为空,复用");
- }
- return mView;
- }
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- setupViews();
- }
- private void setupViews() {
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- }
- @Override
- public void onDestroyView() {
- // TODO Auto-generated method stub
- super.onDestroyView();
- if(null!=mView){
- ((ViewGroup) mView.getParent()).removeView(mView);
- }
- }
- /**
- * ViewPager页面选项卡切换监听器
- */
- class MySetOnPageChangeListener implements OnPageChangeListener
- {
- public void onPageScrollStateChanged(int arg0) {
- }
- public void onPageScrolled(int arg0, float arg1, int arg2) {
- }
- public void onPageSelected(int position) {
- Fragment ff = mfragmentList.get(position);
- /**
- * 判断是否浏览过,如果没有则重新从网上加载。
- * jkx
- */
- if(!isBrowsered(ff)){
- if (null != ff && ff instanceof WebviewFragment) {
- ((WebviewFragment)ff).cusLoadURL();
- }
- browsedfragmentList.add(ff);
- System.out.println("已加载页面个数:"+browsedfragmentList.size());
- }
- MainActivity ma = (MainActivity)getActivity();
- if (null != ma) {
- ma.getSupportActionBar().setSelectedNavigationItem(position);
- int last = mviewPager.getChildCount()-1;
- if (position == last || position == 0){
- ma.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
- } else {
- ma.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
- }
- }
- }
- }
- /**
- * 判断该fragment是否已经浏览过。
- * @param fragment
- * @return
- * jkx
- */
- public boolean isBrowsered(Fragment fragment){
- if(browsedfragmentList.contains(fragment)){
- return true ;
- }else{
- return false ;
- }
- }
- }