使用视图作为页面而不是片段保存 viewpager 的状态
Saving the state of a viewpager using views as pages and not fragments
我有一个使用自定义视图而不是片段作为页面的 viewpager。
这个自定义视图基本上是一个带有图像视图的线性布局,因为我正在使用我的 viewpager 来显示图像。
然而,当屏幕方向改变时,一切都消失了。包括我的 imageviews。我做了研究
- 大多数教程都展示了如何使用片段来做到这一点。(在我的例子中,我没有使用片段)Android: Saving Fragment state in ViewPager?
我的 viewpager 适配器正在使用包含图像文件路径的数组列表。
下面是我的 viewpager 的适配器。(注意:我正在使用一个数组列表,在其中添加项目然后调用notifyDataSetChanged
)
下面是我的viewpager的适配器class
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 的状态?
通过在
中保存我的数组列表和当前位置,当屏幕方向发生变化时,我能够恢复我的 viewpager 的状态,包括它的当前页面
onSaveInstanceState
方法。稍后我从方法 onCreate(Bundle savedInstanceState)
中的包中获取数组列表和位置
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);
}
}
我有一个使用自定义视图而不是片段作为页面的 viewpager。 这个自定义视图基本上是一个带有图像视图的线性布局,因为我正在使用我的 viewpager 来显示图像。
然而,当屏幕方向改变时,一切都消失了。包括我的 imageviews。我做了研究
- 大多数教程都展示了如何使用片段来做到这一点。(在我的例子中,我没有使用片段)Android: Saving Fragment state in ViewPager?
我的 viewpager 适配器正在使用包含图像文件路径的数组列表。
下面是我的 viewpager 的适配器。(注意:我正在使用一个数组列表,在其中添加项目然后调用notifyDataSetChanged
)
下面是我的viewpager的适配器class
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 的状态?
通过在
中保存我的数组列表和当前位置,当屏幕方向发生变化时,我能够恢复我的 viewpager 的状态,包括它的当前页面onSaveInstanceState
方法。稍后我从方法 onCreate(Bundle savedInstanceState)
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);
}
}