使用视图作为页面保存viewpager的状态而不是片段

时间:2016-02-02 10:16:09

标签: android android-fragments android-viewpager android-view android-pageradapter

我有一个使用自定义视图而不是片段作为页面的viewpager。 这个自定义视图基本上是一个带有imageview的线性布局,因为我正在使用我的viewpager来显示图像。

然而,当屏幕方向改变时,一切都会消失。包括我的imageviews。我做过研究和

我的viewpager适配器正在使用包含图像文件路径的arraylist。

以下是我的viewpager适配器。(注意:我正在使用arraylist向其中添加项目,然后调用 notifyDataSetChanged

以下是我的viewpager的适配器类

 public class ViewPagerAdapter extends PagerAdapter {

private Context context;
private LayoutInflater inflater;
private ArrayList<ImagePaths> imagesArray;

public  ViewPagerAdapter(Context context,ArrayList<ImagePaths> array, ){
    this.context = context;
    inflater =LayoutInflater.from(context);
    imagesArray = array;
}

public int getCount(){
    return imagesArray.size();
}

public boolean isViewFromObject(View view,Object object){
    return view == object;
}

public Object instantiateItem(ViewGroup viewGroup, int position){
    View view = inflater.inflate(R.layout.photo_layout, viewGroup, false);
    ImageView imageView =(ImageView) view.findViewById(R.id.photoView);

    final String imagePath = imagesArray.get(position);

    ImageLoader.getInstance().displayImage(imagePath, imageView);

    viewGroup.addView(view);
    return view;
}

public void destroyItem(ViewGroup container, int position, Object object){
    container.removeView((LinearLayout) object);
}
}

我需要做些什么来保存我的viewpager的状态?

1 个答案:

答案 0 :(得分:2)

通过保存我的arraylist和当前位置,我能够恢复我的viewpager状态,包括当前页面的屏幕方向改变

onSaveInstanceState方法。稍后我会在方法onCreate(Bundle savedInstanceState)

中从包中获取arraylist和position
public class myActivity extends AppCompactActivity{

  private ArrayList<ImagePaths> listviewArray;
  private int savedImagePosition;

  @Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_layout);

    if(savedInstanceState != null){
        listviewArray = savedInstanceState.getParcelableArrayList("image_array");
        savedImagePosition = savedInstanceState.getInt("savedImagePosition");

    }else{
        listviewArray = new ArrayList<>();
        savedImagePosition =0;
    }

    ViewPager viewpager = (ViewPager) findViewById(R.id.viewpager);

    ViewPagerAdapter adapter = new ViewPagerAdapter(this,listviewArray);

    viewpager.setAdapter(adapter);
    viewpager.setCurrentItem(savedImagePosition,true);
  }

 @Override
protected void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    outState.putParcelableArrayList("image_array", listviewArray);
    outState.putInt("savedImagePosition",savedImagePosition);

}

}

我的viewpager适配器

public class ViewPagerAdapter extends PagerAdapter {

private Context context;
private LayoutInflater inflater;
private ArrayList<ImagePaths> imagesArray;

public  ViewPagerAdapter(Context context,ArrayList<ImagePaths> array, ){
    this.context = context;
    inflater =LayoutInflater.from(context);
    imagesArray = array;
}

public int getCount(){
    return imagesArray.size();
}

public boolean isViewFromObject(View view,Object object){
    return view == object;
}

public Object instantiateItem(ViewGroup viewGroup, int position){
    View view = inflater.inflate(R.layout.photo_layout, viewGroup, false);
    ImageView imageView =(ImageView) view.findViewById(R.id.photoView);

    final String imagePath = imagesArray.get(position);

    ImageLoader.getInstance().displayImage(imagePath, imageView);

    viewGroup.addView(view);
    return view;
}

public void destroyItem(ViewGroup container, int position, Object object){
    container.removeView((LinearLayout) object);
}
}