我正在尝试将viewpager与图像一起使用,但如果我将其滑动崩溃,它只显示一张图片, 说 java.lang.IllegalStateException:指定的子节点已经有父节点。您必须先在孩子的父母上调用removeView()。 我的适配器代码是:
public class NewDemoAdapter extends PagerAdapter {
public ArrayList<Integer> imagelist;
public ArrayList<String> lines;
public Context context;
private ImageView imageView;
private BitmapFactory.Options bounds;
private ViewGroup viewPager;
private Bitmap cropImg;
private LayoutInflater mInflater;
public NewDemoAdapter(Context context, ArrayList<Integer> imagelist, ArrayList<String> line) {
this.imagelist = imagelist;
this.lines = line;
}
@Override
public int getCount() {
return imagelist.size();
}
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
mInflater = LayoutInflater.from(AppController.getAppContext());
final View itemView = mInflater.inflate(R.layout.demolayout,container,false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.myimage);
TextView textView = (TextView) itemView.findViewById(R.id.line);
textView.setText(lines.get(position));
imageView.setImageResource(imagelist.get(position));
((ViewPager) container).addView(imageView, 0);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
((ViewPager) container).removeView((TextView) object);
}
}
答案 0 :(得分:0)
问题在于这一行
((ViewPager) container).addView(imageView, 0);
ImageView
(R.id.image)作为父itemView
,这意味着它不能将ViewPager
作为父级。变化
((ViewPager) container).addView(imageView, 0);
到
((ViewPager) container).addView(itemView, 0);
修改
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}