对于viewpager的使用,如果说是请求的数据对内存的消耗不到的话,这是很理想的,但是如果说请求的数据对内存消耗的比较大的话这样就容易出现oom(比如说图片),在使用viewPager的时候使用setOffscreenPageLimit(int size)默认的是1,所以说我们怎么设置这个值,在加载数据的话会将当前页面和下个页面的数据同时的进行请求,这在某种情况下时非常的影响性能的,还有就是在当前页面和下个页面在viewPager中算作了同一个页面,因为你是无法通过生命周期来进行控制数据的延迟加载,所以现在很多网友通过修改v4包进行修改setOffscreenPageLimit(int size)的默认值为0;但是这样虽然效果实现了,但是会引发另一个问题,那就是在切换页面时,中间过渡的是一个大大的黑屏,myGod的这种修改对体验算是致命的打击对于我而言,不过最后终于还是被我搞定了;
之前的一种方式在我之前的博客中有写,下面介绍的是另一种方式:结合Fragment来处理,废话不说直接上代码:
MainActivity public class MainActivity extends FragmentActivity { private ViewPager pager; private ArrayList<Fragment> fts = new ArrayList<Fragment>(); private String[] urls = new String[]{ "http://t2.qpic.cn/mblogpic/d5149f8549e394000aa6/2000", "http://t2.qpic.cn/mblogpic/fad173ab259d7efbfecc/2000", "http://t2.qpic.cn/mblogpic/463b55016d51d4b9c172/2000", "http://t2.qpic.cn/mblogpic/ab3f96d6095ec82e6d40/2000", "http://t2.qpic.cn/mblogpic/e61f017bcad2684e79f2/2000", "http://t2.qpic.cn/mblogpic/d271142c68e6e1d5b8de/2000", "http://t2.qpic.cn/mblogpic/ef50986bdf4dd8801060/2000", "http://t2.qpic.cn/mblogpic/71585d34aaa60b9dcc2a/2000", "http://t2.qpic.cn/mblogpic/e0011bad45ae23889216/2000", "http://t2.qpic.cn/mblogpic/6e84cb60bf0e6b4945f6/2000", "http://t2.qpic.cn/mblogpic/5a9217624c130c2fa9f8/2000", "http://t2.qpic.cn/mblogpic/7ddb0e9618754fc4bc8e/2000", "http://t2.qpic.cn/mblogpic/eed45f9d4b5001a04b30/2000", "http://t2.qpic.cn/mblogpic/62353a0dbdd40bc1be9a/2000", "http://t2.qpic.cn/mblogpic/0ccf7f16318331e9a892/2000", "http://t2.qpic.cn/mblogpic/4e55efffef93f6d4a86e/2000", "http://t2.qpic.cn/mblogpic/4ac76f1427e4278b7172/2000", "http://t2.qpic.cn/mblogpic/0db3a5a1047d7ee52bdc/2000", "http://t2.qpic.cn/mblogpic/c65d09035d7b6c644d6a/2000", "http://t2.qpic.cn/mblogpic/7ebf4b49bf7e2ef6a716/2000", "http://t2.qpic.cn/mblogpic/4ba5fd867149273f9b02/2000", "http://t2.qpic.cn/mblogpic/4f375f19fbd08151d0f8/2000", "http://t2.qpic.cn/mblogpic/1c22a0574274d0e3c866/2000", "http://t2.qpic.cn/mblogpic/50cdf4f63120ccf74e86/2000", "http://t2.qpic.cn/mblogpic/ab8588008e7865927d7c/2000", "http://t2.qpic.cn/mblogpic/1e9e65d61d5f2bb8ebc4/2000", "http://t2.qpic.cn/mblogpic/6913e7ad5c7267969d9a/2000", "http://t2.qpic.cn/mblogpic/54157580cc7ddf7b2870/2000", "http://t2.qpic.cn/mblogpic/0bd2f9be53d2c9aec8ca/2000", "http://t2.qpic.cn/mblogpic/2041a03d63cc6c5b9b92/2000", }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pager = (ViewPager) findViewById(R.id.pager); for (int i = 0; i < urls.length; i++) { fts.add(PhotoFragment.instance(urls[i], i)); } pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), fts)); pager.setOffscreenPageLimit(fts.size()); pager.setCurrentItem(0); } private class MyPagerAdapter extends FragmentPagerAdapter{ private ArrayList<Fragment> list; public MyPagerAdapter(FragmentManager fm,ArrayList<Fragment> list) { super(fm); this.list = list; } @Override public Fragment getItem(int arg0) { return list.get(arg0); } @Override public int getCount() { return list.size(); } } }
PhotoFragment:
package com.zhizhi.viewpagernocache; import com.nostra13.universalimageloader.core.ImageLoader; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; public class PhotoFragment extends Fragment { private final static String TAG = PhotoFragment.class.getName(); private String url; private int index; private ImageView image; public static PhotoFragment instance(String url, int index) { PhotoFragment fragment = new PhotoFragment(); Bundle bundle = new Bundle(); bundle.putString("url", url); bundle.putInt("index", index); fragment.setArguments(bundle); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { url = getArguments().getString("url"); index = getArguments().getInt("index"); } setRetainInstance(true); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("url", url); outState.putInt("index", index); } @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if (savedInstanceState != null) { url = savedInstanceState.getString("url"); index = savedInstanceState.getInt("index"); } View view = inflater.inflate(R.layout.view_photo, container, false); image = (ImageView) view.findViewById(R.id.image); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser) { Log.i(TAG, "get pic data for show"); ImageLoader.getInstance().displayImage(url, image); } } }
以上就是代码的核心部分了;
源码Demo :http://files.cnblogs.com/kingfly13/ViewPagerNoCache.zip
android ViewPager中页面延迟加载的实现
时间: 2024-11-08 22:17:34