Universal Image Loader - call too many getView()

十年热恋 提交于 2019-12-10 12:19:41

问题


I'm trying to create a image ListView using universal-image-loader. Currently, no images appear or load. I found that getView() from ImageAdapter was called to many times.

For example, if list size is 3, then getView() should call 3 times but in my code, call 24 times!!!

This is my Source Code.

public class MainActivity extends Activity{
    AsyncTask<Void, Void, Void> asyncTask, registerTask;
    DisplayImageOptions options;
    ArrayList<HashMap<String, Object>> feedList = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        int cacheSize = ((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass() * 1024 * 1024 / 8;

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
            .memoryCacheExtraOptions(metrics.widthPixels, metrics.heightPixels)
            .diskCacheExtraOptions(metrics.widthPixels, metrics.heightPixels, null)
            .memoryCache(new LruMemoryCache(cacheSize))
            .memoryCacheSize(cacheSize)
            .memoryCacheSizePercentage(13) 
            .diskCache(new UnlimitedDiscCache(StorageUtils.getCacheDirectory(this)))
            .diskCacheSize(100 * 1024 * 1024)
            .diskCacheFileCount(200)
            .diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
            .imageDownloader(new BaseImageDownloader(this))
            .imageDecoder(new BaseImageDecoder(false))
            .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
            .build();
        ImageLoader.getInstance().init(config);

        options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.ic_stub)
            .showImageForEmptyUri(R.drawable.ic_empty)
            .showImageOnFail(R.drawable.ic_error)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .considerExifParams(true)
            .displayer(new RoundedBitmapDisplayer(20))
            .build();

        asyncTask = new AsyncTask<Void, Void, Void>() {
            protected Void doInBackground(Void... params) {


                StyleFeedServerUtils serverUtils = new StyleFeedServerUtils();
                feedList = serverUtils.getStyleFeedList(MainActivity.this);
                return null;
            }
            @Override
            protected void onPostExecute(Void result) {
                if(feedList != null && feedList.size() > 0){
                    ListView listView = (ListView)findViewById(R.id.list);
                    listView.setAdapter(new ImageAdapter());
                    listView.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Log.e("fashion", "onItemClick, position : "+position);
                        } 
                    });
                }
                asyncTask.cancel(!isCancelled());
                asyncTask = null;
            }
        };  
        asyncTask.execute(null, null, null);
    }

    @Override
    public void onDestroy() {
        if (registerTask != null) {
            registerTask.cancel(true);
        }
        GCMRegistrar.onDestroy(this);
        AnimateFirstDisplayListener.displayedImages.clear();
        super.onDestroy();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    class ImageAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

        ImageAdapter() {
            inflater = LayoutInflater.from(MainActivity.this);
        }

        @Override
        public int getCount() {
            return feedList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            Log.e("fashion", "getView, position : "+position);
            View view = convertView;
            if (convertView == null) {
                view = inflater.inflate(R.layout.style_feed_list_view, parent, false);
            }
            ImageView imageView = (ImageView)view.findViewById(R.id.style_feed_image);
            ImageLoader.getInstance().displayImage(feedList.get(position).get("URL").toString(), imageView, options, animateFirstListener);

            return view;
        }
    }

    private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view; 
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }
}

ArrayList<HashMap<String, Object >> feedList has 3 items. So, getView() should be called 3 times but called 24 times. I think the log should be like this,

02-11 12:55:32.059: E/fashion(6829): getView, position : 0
02-11 12:55:32.084: E/fashion(6829): getView, position : 1
02-11 12:55:32.184: E/fashion(6829): getView, position : 2

But, I got the log like this:

02-11 12:55:32.059: E/fashion(6829): getView, position : 0
02-11 12:55:32.084: E/fashion(6829): getView, position : 1
02-11 12:55:32.184: E/fashion(6829): getView, position : 2
02-11 12:55:32.194: E/fashion(6829): getView, position : 0
02-11 12:55:32.194: E/fashion(6829): getView, position : 1
02-11 12:55:32.284: E/fashion(6829): getView, position : 2
02-11 12:55:32.564: E/fashion(6829): getView, position : 0
02-11 12:55:32.584: E/fashion(6829): getView, position : 1
02-11 12:55:32.584: E/fashion(6829): getView, position : 2
02-11 12:55:32.584: E/fashion(6829): getView, position : 0
02-11 12:55:32.584: E/fashion(6829): getView, position : 1
02-11 12:55:32.584: E/fashion(6829): getView, position : 2
02-11 12:55:32.659: E/fashion(6829): getView, position : 0
02-11 12:55:32.659: E/fashion(6829): getView, position : 1
02-11 12:55:32.659: E/fashion(6829): getView, position : 2
02-11 12:55:32.659: E/fashion(6829): getView, position : 0
02-11 12:55:32.659: E/fashion(6829): getView, position : 1
02-11 12:55:32.664: E/fashion(6829): getView, position : 2
02-11 12:55:32.684: E/fashion(6829): getView, position : 0
02-11 12:55:32.684: E/fashion(6829): getView, position : 1
02-11 12:55:32.684: E/fashion(6829): getView, position : 2
02-11 12:55:32.684: E/fashion(6829): getView, position : 0
02-11 12:55:32.684: E/fashion(6829): getView, position : 1
02-11 12:55:32.684: E/fashion(6829): getView, position : 2

回答1:


getview () call is not associated with using AIUL.

I recommend that you like this to you.

before

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ListView
        android:id="@+id/list_listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>
</LinearLayout>

after

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:id="@+id/list_listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>
</LinearLayout>


来源:https://stackoverflow.com/questions/28446476/universal-image-loader-call-too-many-getview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!