緩存ViewPager銷毀的視圖(防止重新加載)
問(wèn)題導(dǎo)致ViewPager翻滾到第三個(gè)fragment時(shí)fragment重新加載了
方案1
通過(guò)屬性設(shè)置 ViewPager.setOffscreenPageLimit( 緩存頁(yè)數(shù)值 ); 來(lái)實(shí)現(xiàn)
方案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{
//銷毀再走的話會(huì)走onCreateView而onCreate不會(huì)走
Button bt;
//如果涉及到Fragment使用FragmentPagerAdapter 需要在碎片中做緩存頁(yè)面的處理
View v;//new了一個(gè)
@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();
//如果獲取的父視圖不為空 說(shuō)明之前加載過(guò)數(shù)據(jù)
if(container != null){
//那么 再次加載的時(shí)候 要把當(dāng)前父容器中的所有視圖移除掉 添加緩存的視圖
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結(jié)合時(shí) 所涉及的Fragment相關(guān)的類都是v4包下的
public class MainActivity extends FragmentActivity {
ViewPager vp;
Fragment1 f1;
Fragment2 f2;
Fragment3 f3;
ArrayList<Fragment> arrayList = new ArrayList<Fragment>();
ArrayList<View> alist = new ArrayList<View>();
@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{
//獲取顯示數(shù)量
@Override
public int getCount() {
return alist.size();
}
@Override//判斷顯示的內(nèi)容是否是對(duì)象
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override//銷毀頁(yè)面
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(alist.get(position));
}
@Override//添加頁(yè)面
public Object instantiateItem(ViewGroup container, int position) {
container.addView(alist.get(position));
return alist.get(position);
}
}
//FragmentStatePagerAdapter適合較多碎片的加載
//默認(rèn)只保留相鄰碎片的操作痕跡 占用內(nèi)存較少
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保留所有頁(yè)面的操作痕跡
//適合較少碎片的加載 占內(nèi)存
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();
}
}
}
效果圖