Outofmemeoryerror (viewpager + imageviews)

后端 未结 6 873
悲哀的现实
悲哀的现实 2021-01-30 12:18

i am showing 150+ images in viewpager, when page size crossed 70 app is crashing , all images are loading from network , and i have fallowed [link]: Strange out of memory issue

6条回答
  •  礼貌的吻别
    2021-01-30 12:25

    The complete solution can be found below, the important lines are those in the destroyItem method:

    private class ContentPagerAdapter extends PagerAdapter {
        @Override
        public void destroyItem(View collection, int position, Object o) {
            View view = (View)o;
            ((ViewPager) collection).removeView(view);
            view = null;
        }
    
        @Override
        public void finishUpdate(View arg0) {
            // TODO Auto-generated method stub
    
        }
        @Override
        public int getCount() {
            return ids.length;
        }
    
        @Override
        public Object instantiateItem(View context, int position) {
            ImageView imageView = new ImageView(getApplicationContext());
            imageView.findViewById(R.id.item_image);
            imageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), ids[position]));
    
            ((ViewPager) context).addView(imageView);
    
            return imageView;
        }
    
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==((ImageView)object);
        }
    
        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
            // TODO Auto-generated method stub
        }
        @Override
        public Parcelable saveState() {
            // TODO Auto-generated method stub
            return null;
        }
        @Override
        public void startUpdate(View arg0) {
            // TODO Auto-generated method stub
    
        }
    

提交回复
热议问题