PagerAdapter 崩溃
PagerAdapter crashing
我有一个 pageradapter,我用它来显示图像的幻灯片,但是当我设置一个较低的屏幕限制时它崩溃了。
这是我的代码
public class ProjectPicturesAdapter extends PagerAdapter {
private List<String> pictures;
private LayoutInflater inflater;
private final RequestManager glide;
public ProjectPicturesAdapter(RequestManager glide, LayoutInflater inflater, List<String> pictures) {
this.pictures = pictures;
this.glide = glide;
this.inflater = inflater;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View image = inflater.inflate(R.layout.slider_image, container, false);
ImageView myImage = (ImageView) image.findViewById(R.id.image);
glide.load(pictures.get(position))
.centerCrop()
.crossFade()
.placeholder(R.drawable.project_placeholder)
.into(myImage);
container.addView(myImage);
return myImage;
}
@Override
public void destroyItem(ViewGroup container, int position, Object view) {
container.removeView((View) view);
}
@Override
public int getCount() {
return pictures.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
}
我初始化适配器的方式是这样的
ProjectPicturesAdapter picturesAdapter = new
ProjectPicturesAdapter(Glide.with(this), LayoutInflater.from(this),
images);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(picturesAdapter);
pager.setOffscreenPageLimit(1);
如果超过 2 张图片就会崩溃,我总是可以增加离屏限制但是如果有 20 张图片会发生什么,我不能将离屏限制保持为 20。
我的崩溃日志给出了一个错误,指出指定的 child 已经有一个 parent。您必须先在 child 的 parent 上调用 removeView()。
如果你的目的是展示轮播?
只要使用这个库,你的问题就会得到解决https://github.com/sayyam/carouselview
问题是这样引起的:
@Override
public void destroyItem(ViewGroup container, int position, Object view)
{
container.removeView((View) view);
}
我建议使用 FragmentStatePagerAdapter 而不是 PagerAdapter。
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return NUM_ITEMS;
}
@Override
public Fragment getItem(int position) {
return ArrayListFragment.newInstance(position);
}
}
mAdapter = new MyAdapter(getFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
更新 ProjectPicturesAdapter
如下:
public class ProjectPicturesAdapter extends PagerAdapter {
private List<String> pictures;
private LayoutInflater inflater;
private final RequestManager glide;
public ProjectPicturesAdapter(RequestManager glide, LayoutInflater inflater, List<String> pictures) {
this.pictures = pictures;
this.glide = glide;
this.inflater = inflater;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ViewGroup view = (ViewGroup) inflater.inflate(R.layout.slider_image, container, false);
ImageView myImage = (ImageView) view.findViewById(R.id.image);
glide.load(pictures.get(position))
.centerCrop()
.crossFade()
.placeholder(R.drawable.project_placeholder)
.into(myImage);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object view) {
container.removeView((View) view);
}
@Override
public int getCount() {
return pictures.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
我有一个 pageradapter,我用它来显示图像的幻灯片,但是当我设置一个较低的屏幕限制时它崩溃了。
这是我的代码
public class ProjectPicturesAdapter extends PagerAdapter {
private List<String> pictures;
private LayoutInflater inflater;
private final RequestManager glide;
public ProjectPicturesAdapter(RequestManager glide, LayoutInflater inflater, List<String> pictures) {
this.pictures = pictures;
this.glide = glide;
this.inflater = inflater;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View image = inflater.inflate(R.layout.slider_image, container, false);
ImageView myImage = (ImageView) image.findViewById(R.id.image);
glide.load(pictures.get(position))
.centerCrop()
.crossFade()
.placeholder(R.drawable.project_placeholder)
.into(myImage);
container.addView(myImage);
return myImage;
}
@Override
public void destroyItem(ViewGroup container, int position, Object view) {
container.removeView((View) view);
}
@Override
public int getCount() {
return pictures.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
}
我初始化适配器的方式是这样的
ProjectPicturesAdapter picturesAdapter = new
ProjectPicturesAdapter(Glide.with(this), LayoutInflater.from(this),
images);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(picturesAdapter);
pager.setOffscreenPageLimit(1);
如果超过 2 张图片就会崩溃,我总是可以增加离屏限制但是如果有 20 张图片会发生什么,我不能将离屏限制保持为 20。
我的崩溃日志给出了一个错误,指出指定的 child 已经有一个 parent。您必须先在 child 的 parent 上调用 removeView()。
如果你的目的是展示轮播? 只要使用这个库,你的问题就会得到解决https://github.com/sayyam/carouselview
问题是这样引起的:
@Override
public void destroyItem(ViewGroup container, int position, Object view)
{
container.removeView((View) view);
}
我建议使用 FragmentStatePagerAdapter 而不是 PagerAdapter。
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return NUM_ITEMS;
}
@Override
public Fragment getItem(int position) {
return ArrayListFragment.newInstance(position);
}
}
mAdapter = new MyAdapter(getFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
更新 ProjectPicturesAdapter
如下:
public class ProjectPicturesAdapter extends PagerAdapter {
private List<String> pictures;
private LayoutInflater inflater;
private final RequestManager glide;
public ProjectPicturesAdapter(RequestManager glide, LayoutInflater inflater, List<String> pictures) {
this.pictures = pictures;
this.glide = glide;
this.inflater = inflater;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ViewGroup view = (ViewGroup) inflater.inflate(R.layout.slider_image, container, false);
ImageView myImage = (ImageView) view.findViewById(R.id.image);
glide.load(pictures.get(position))
.centerCrop()
.crossFade()
.placeholder(R.drawable.project_placeholder)
.into(myImage);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object view) {
container.removeView((View) view);
}
@Override
public int getCount() {
return pictures.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}