美文网首页
ViewPager的IllegalStateException运

ViewPager的IllegalStateException运

作者: Mashell熙 | 来源:发表于2019-05-20 17:18 被阅读0次

    开发场景:
    1.在activity执行oncreate()生命周期时,初始化viewpager控件、viewpager的适配器adapter,adapter的填充数组list;

    1. 当用户刷新数据的时候,先清空listview,并将新数据填充到list中,调用adapter的notifyDataSetChanged()方法,此时产生运行错误。
    错误类型: Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

    代码如下:

    package com.example.androidtest;
     
    import java.util.ArrayList;
    import java.util.List;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.view.ViewPager;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.ImageView;
     
     
    public class MainActivity extends Activity {
        private ViewPager vp ;
        private VPAdapter vpAdapter;
        private List<View> views;
     
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            vp = (ViewPager)findViewById(R.id.vp);
            views = new ArrayList<View>();
            vpAdapter = new VPAdapter(views);
            vp.setAdapter(vpAdapter);
        }
     
        public void doClick(View v){
            switch (v.getId()) {
            case R.id.add:
                views.clear();
                for (int i = 0; i < 3; i++) {
                    ImageView iv = (ImageView)((View)LayoutInflater.from(this).inflate(R.layout.imageview, null)).findViewById(R.id.iv);
                    iv.setImageResource(R.drawable.ic_launcher);
                    views.add(iv);
                }
                vpAdapter.notifyDataSetChanged();
                break;
            case R.id.del:
                break;
            }
        }
    } 
    
    package com.example.androidtest;
     
    import java.util.ArrayList;
    import java.util.List;
     
    import android.support.v4.view.PagerAdapter;
    import android.view.View;
    import android.view.ViewGroup;
     
    public class VPAdapter extends PagerAdapter {
        private List<View> viewList;
     
        public VPAdapter(List<View> viewList){
            if(viewList!=null){
                this.viewList = viewList;
            }else{
                this.viewList = new ArrayList<View>();
            }
        }
        
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return viewList.size();
        }
     
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0==arg1;
        }
     
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewList.get(position));
        }
     
        @Override
        public Object instantiateItem(ViewGroup container, int position){
            //这里的判断是关键代码,首先我们要判断这个view的父容器是否为null;
            //如果为null,证明是第一次加入,直接添加即可,
            //如果不为null。此时需要先remove掉父容器,再将这个view添加到container中,否则就会产生IllegalStateException异常
            if(viewList.get(position).getParent()==null){
                container.addView(viewList.get(position), 0);
            }else{
                ((ViewGroup)viewList.get(position).getParent()).removeView(viewList.get(position));
                container.addView(viewList.get(position));
            }
            return viewList.get(position);
        }
        
        
    }
    

    相关文章

      网友评论

          本文标题:ViewPager的IllegalStateException运

          本文链接:https://www.haomeiwen.com/subject/wwflzqtx.html