本文介绍了通用图像装载机 - 呼叫太多getView()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个使用通用的图像装载一个图像的ListView 。目前,还没有显示图像或负载。我发现, getView() ImageAdapter 被称为很多次。

例如,如果列表大小为3,则 getView()应该叫3次,但在我的code,拨打24次!

这是我的源$ C ​​$ C。

 公共类MainActivity延伸活动{
    AsyncTask的<太虚,太虚,太虚> AsyncTask的,registerTask;
    DisplayImageOptions选择;
    ArrayList的<&HashMap的LT;弦乐,对象>> feedList = NULL;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main_activity);        DisplayMetrics指标=新DisplayMetrics();
        。getWindowManager()getDefaultDisplay()getMetrics(指标)。        INT的cachesize =((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE))getMemoryClass()* 1024 * 1024/8;        ImageLoaderConfiguration配置=新ImageLoaderConfiguration.Builder(本)
            .memoryCacheExtraOptions(metrics.widthPixels,metrics.heightPixels)
            .diskCacheExtraOptions(metrics.widthPixels,metrics.heightPixels,NULL)
            .memoryCache(新LruMemoryCache(CACHESIZE))
            .memoryCacheSize(CACHESIZE)
            .memoryCacheSizePercentage(13)
            .diskCache(新UnlimitedDiscCache(StorageUtils.getCacheDirectory(本)))
            .diskCacheSize(100 * 1024 * 1024)
            .diskCacheFileCount(200)
            .diskCacheFileNameGenerator(新的Hash codeFileNameGenerator())
            .imageDownloader(新BaseImageDownloader(本))
            .imageDe codeR(新BaseImageDe codeR(假))
            .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
            。建立();
        。ImageLoader.getInstance()的init(配置);        选项​​=新DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.ic_stub)
            .showImageForEmptyUri(R.drawable.ic_empty)
            .showImageOnFail(R.drawable.ic_error)
            .cacheInMemory(真)
            .cacheOnDisk(真)
            .considerExifParams(真)
            .displayer(新RoundedBitmapDisplayer(20))
            。建立();        AsyncTask的=新的AsyncTask<太虚,太虚,太虚>(){
            保护无效doInBackground(虚空...... PARAMS){
                StyleFeedServerUtils serverUtils =新StyleFeedServerUtils();
                feedList = serverUtils.getStyleFeedList(MainActivity.this);
                返回null;
            }
            @覆盖
            保护无效onPostExecute(虚空结果){
                如果(feedList =空&放大器;!&放大器; feedList.size()大于0){
                    ListView控件的ListView =(ListView控件)findViewById(R.id.list);
                    listView.setAdapter(新ImageAdapter());
                    listView.setOnItemClickListener(新OnItemClickListener(){
                        @覆盖
                        公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
                            Log.e(时尚,onItemClick,位置:+位置);
                        }
                    });
                }
                asyncTask.cancel(isCancelled()!);
                AsyncTask的= NULL;
            }
        };
        asyncTask.execute(NULL,NULL,NULL);
    }    @覆盖
    公共无效的onDestroy(){
        如果(registerTask!= NULL){
            registerTask.cancel(真);
        }
        GCMRegistrar.onDestroy(本);
        AnimateFirstDisplayListener.displayedImages.clear();
        super.onDestroy();
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        INT ID = item.getItemId();
        如果(ID == R.id.action_settings){
            返回true;
        }
        返回super.onOptionsItemSelected(项目);
    }    类ImageAdapter扩展了BaseAdapter {
        私人LayoutInflater吹气;
        私人ImageLoadingListener animateFirstListener =新AnimateFirstDisplayListener();        ImageAdapter(){
            吹气= LayoutInflater.from(MainActivity.this);
        }        @覆盖
        公众诠释的getCount(){
            返回feedList.size();
        }        @覆盖
        公共对象的getItem(INT位置){
            返回的位置;
        }        @覆盖
        众长getItemId(INT位置){
            返回的位置;
        }        @覆盖
        公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
            Log.e(时尚,getView,位置:+位置);
            查看查看= convertView;
            如果(convertView == NULL){
                鉴于= inflater.inflate(R.layout.style_feed_list_view,父母,假);
            }
            ImageView的ImageView的=(ImageView的)view.findViewById(R.id.style_feed_image);
            。ImageLoader.getInstance()displayImage(feedList.get(位置)获得(URL)的toString(),ImageView的,期权,animateFirstListener);            返回视图。
        }
    }    私有静态类AnimateFirstDisplayListener扩展SimpleImageLoadingListener {        静态最终名单<串GT; displayedImages = Collections.synchronizedList(新的LinkedList<串GT;());        @覆盖
        公共无效onLoadingComplete(字符串imageUri,观景,位图loadedImage){
            如果(loadedImage!= NULL){
                ImageView的ImageView的=(ImageView的)视图。
                !布尔firstDisplay = displayedImages.contains(imageUri);
                如果(firstDisplay){
                    FadeInBitmapDisplayer.animate(ImageView的,500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }
}

的ArrayList<&HashMap的LT;弦乐,对象>> feedList有3项。因此, getView()应该叫3次,但所谓的24倍。
我认为,日志应该是这样的,

  12月2日至11日:55:32.059:E /时装(6829):getView,位置:0
12月2日至十一日:55:32.084:E /时装(6829):getView,位置:1
12月2日至十一日:55:32.184:E /时装(6829):getView,位置:2

不过,我得到的日志是这样的:

  12月2日至11日:55:32.059:E /时装(6829):getView,位置:0
12月2日至十一日:55:32.084:E /时装(6829):getView,位置:1
12月2日至十一日:55:32.184:E /时装(6829):getView,位置:2
12月2日至十一日:55:32.194:E /时装(6829):getView,位置:0
12月2日至十一日:55:32.194:E /时装(6829):getView,位置:1
12月2日至十一日:55:32.284:E /时装(6829):getView,位置:2
12月2日至十一日:55:32.564:E /时装(6829):getView,位置:0
12月2日至十一日:55:32.584:E /时装(6829):getView,位置:1
12月2日至十一日:55:32.584:E /时装(6829):getView,位置:2
12月2日至十一日:55:32.584:E /时装(6829):getView,位置:0
12月2日至十一日:55:32.584:E /时装(6829):getView,位置:1
12月2日至十一日:55:32.584:E /时装(6829):getView,位置:2
12月2日至十一日:55:32.659:E /时装(6829):getView,位置:0
12月2日至十一日:55:32.659:E /时装(6829):getView,位置:1
12月2日至十一日:55:32.659:E /时装(6829):getView,位置:2
12月2日至十一日:55:32.659:E /时装(6829):getView,位置:0
12月2日至十一日:55:32.659:E /时装(6829):getView,位置:1
12月2日至十一日:55:32.664:E /时装(6829):getView,位置:2
12月2日至十一日:55:32.684:E /时装(6829):getView,位置:0
12月2日至十一日:55:32.684:E /时装(6829):getView,位置:1
12月2日至十一日:55:32.684:E /时装(6829):getView,位置:2
12月2日至十一日:55:32.684:E /时装(6829):getView,位置:0
12月2日至十一日:55:32.684:E /时装(6829):getView,位置:1
12月2日至十一日:55:32.684:E /时装(6829):getView,位置:2


解决方案

getview()调用未使用32,768有关。

我建议你喜欢这个给你。

 <的LinearLayout
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT>
    < ListView控件
        机器人:ID =@ + ID / list_listview
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT>
    < /&的ListView GT;
< / LinearLayout中>

 <的LinearLayout
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
    < ListView控件
        机器人:ID =@ + ID / list_listview
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT>
    < /&的ListView GT;
< / LinearLayout中>

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
解决方案

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>

这篇关于通用图像装载机 - 呼叫太多getView()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:59