Android用ViewPager做翻动页面,保存页面缓存

缓存ViewPager销毁的视图(防止重新加载)

问题导致ViewPager翻滚到第三个fragment时fragment重新加载了

方案1

通过属性设置    ViewPager.setOffscreenPageLimit( 缓存页数值 ); 来实现

方案2

package com.example.androidfragmentandviewpager1611;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class Fragment1 extends Fragment{
    //销毁再走的话会走onCreateView而onCreate不会走
    Button bt;
    //如果涉及到Fragment使用FragmentPagerAdapter  需要在碎片中做缓存页面的处理
    View v;//new了一个
    @Override
    public void onCreate( Bundle savedInstanceState) {//初始化控件
        super.onCreate(savedInstanceState);
         v = View.inflate(getActivity(), R.layout.fragment1, null);
            bt = (Button) v.findViewById(R.id.button1);
            bt.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    bt.setText("我是碎片1");
                }
            });
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater,
             ViewGroup container,  Bundle savedInstanceState) {
       //获取视图所在的父容器
        container = (ViewGroup) v.getParent();
        //如果获取的父视图不为空  说明之前加载过数据
        if(container != null){
            //那么  再次加载的时候   要把当前父容器中的所有视图移除掉 添加缓存的视图
            container.removeAllViewsInLayout();
        }
        return v;
    }

}

碎片和试图做ViewPager

package com.example.androidfragmentandviewpager1611;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

//使用Fragment与ViewPager结合时 所涉及的Fragment相关的类都是v4包下的
public class MainActivity extends FragmentActivity {
ViewPager vp;
Fragment1 f1;
Fragment2 f2;
Fragment3 f3;
ArrayList arrayList = new ArrayList();
ArrayList alist = new ArrayList();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
void init(){
    vp = (ViewPager) findViewById(R.id.vp);
    f1 = new Fragment1();
    f2 = new Fragment2();
    f3 = new Fragment3();
    
    arrayList.add(f1);
    arrayList.add(f2);
    arrayList.add(f3);
//  vp.setAdapter(new MyAdapter1(getSupportFragmentManager()));
    
    View v1 = View.inflate(MainActivity.this, R.layout.fragment1, null);
    final Button bt1 = (Button) v1.findViewById(R.id.button1);
    bt1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            bt1.setText("我是视图1");
        }
    });
    View v2 = View.inflate(MainActivity.this, R.layout.fragment2, null);
    final Button bt2 = (Button)v2. findViewById(R.id.button2);
    bt2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            bt2.setText("我是视图2");
        }
    });
    View v3 = View.inflate(MainActivity.this, R.layout.fragment3, null);
    final Button bt3 = (Button)v3. findViewById(R.id.button3);
    bt3.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            bt3.setText("我是视图3");
        }
    });
    alist.add(v1);
    alist.add(v2);
    alist.add(v3);
    vp.setAdapter(new MyAdapter());
    
}

class MyAdapter extends PagerAdapter{
//获取显示数量
    @Override
    public int getCount() {
        return alist.size();
    }

    @Override//判断显示的内容是否是对象
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override//销毁页面
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(alist.get(position));
    }

    @Override//添加页面
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(alist.get(position));
        return alist.get(position);
    }
    
    
    
}


//FragmentStatePagerAdapter适合较多碎片的加载
//默认只保留相邻碎片的操作痕迹  占用内存较少
class MyAdapter1 extends FragmentStatePagerAdapter{

    public MyAdapter1(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int arg0) {
        return arrayList.get(arg0);
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }
    
}


//FragmentPagerAdapter保留所有页面的操作痕迹
//适合较少碎片的加载     占内存
class MyAdapter2 extends FragmentPagerAdapter{

    public MyAdapter2(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int arg0) {
        return arrayList.get(arg0);
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }
    
}
}

效果图


Android用ViewPager做翻动页面,保存页面缓存_第1张图片
1EB86C23B5D39CDE3C05A1FC3E6CDF00.jpg

你可能感兴趣的:(Android用ViewPager做翻动页面,保存页面缓存)