android how to deal with data when listview refresh

如何解决listview数据刷新,下拉刷新,上拉加载更多时,图片不闪烁。

在Activity的onResume()方法中将adaper和listView重新再绑定一次。

listView.setAdapter(adapter);
adapter.notifyDataSetChanged();

http://www.eoeandroid.com/forum.php?mod=viewthread&tid=541113&extra=&ordertype=1

public class HomeWaterfallXlListAdapter extends BaseAdapter {private Bitmap defaultBm;
    private static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

    public HomeWaterfallXlListAdapter() {
        defaultBm = BitmapFactory.decodeResource(mActivity.getResources(), R.drawable.loading);
    }

    /**刷新视图**/
    public void setList(List<HomeAnswersJSONResult.Answer> list) {
        this.answers = list;
        notifyDataSetInvalidated();
    }

    /**加载更多**/
    public void addList(List<HomeAnswersJSONResult.Answer> list) {
        if (this.answers != null) {
            this.answers.addAll(list);
            notifyDataSetChanged();
        } else {
            setList(list);
        }
    }

    @Override
    public int getCount() {
        return answers == null ? 0 : answers.size();
    }

    @Override
    public Object getItem(int arg0) {
        return answers.get(arg0);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Answer answer = answers.get(position);

        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mActivity).inflate(R.layout.item_home_infos_list, null);
            holder = new ViewHolder();
            holder.imageView = (ScaleImageView) convertView.findViewById(R.id.starImg);
            convertView.setTag(holder);
        } else {

            holder = (ViewHolder) convertView.getTag();
        }

        int w = answer.getImg().getWidth();
        int h = answer.getImg().getHeight();
        holder.imageView.setImageWidth(w);
        holder.imageView.setImageHeight(h);
        String imageUrl = answer.getImg().getUrl().toString() + "?w=" + AppActivity.screenWidth / 2;
        holder.imageView.setTag(imageUrl);

        imageLoader.loadImage(imageUrl, new ImageSize(w, h), options, new ImageLoadingListener() {

            @Override
            public void onLoadingStarted(String imageUri, View view) {
                holder.imageView.setImageBitmap(defaultBm);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                if (loadedImage != null && imageUri.equals(holder.imageView.getTag())) {
                    holder.imageView.setImageBitmap(loadedImage);
                    boolean firstDisplay = !displayedImages.contains(imageUri);
                    if (firstDisplay) {
                        // 图片淡入效果
                        AlphaAnimation fadeImage = new AlphaAnimation(0, 1);
                        fadeImage.setDuration(500);
                        fadeImage.setInterpolator(new DecelerateInterpolator());
                        holder.imageView.startAnimation(fadeImage);
                        displayedImages.add(imageUri);
                    }
                }
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
            }
        });
 convertView.setOnClickListener(new onItemClick(id));

        return convertView;
    }

    static final class ViewHolder {
        ScaleImageView imageView;
    }

    private class onItemClick implements View.OnClickListener {
        private int id;

        public onItemClick(int id) {
            this.id = id;
        }

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(mActivity, A.class);
            intent.putExtra("answerId", id);
            mActivity.startActivity(intent);
        }
    }

    /**
     * 图片加载第一次显示监听器
     * @author Administrator
     *
     */
    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);
                }
            }
        }
    }
}

ScaleImageView

/**
 *
 * This view will auto determine the width or height by determining if the
 * height or width is set and scale the other dimension depending on the images
 * dimension
 *
 * This view also contains an ImageChangeListener which calls changed(boolean
 * isEmpty) once a change has been made to the ImageView
 *
 * @author Maurycy Wojtowicz
 *
 */
public class ScaleImageView extends ImageView {
    private Bitmap currentBitmap;
    private ImageChangeListener imageChangeListener;
    private boolean scaleToWidth = false; // this flag determines if should
                                            // measure height manually dependent
                                            // of width

    public ScaleImageView(Context context) {
        super(context);
        init();
    }

    public ScaleImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public ScaleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        this.setScaleType(ScaleType.CENTER_INSIDE);
    }

    public void recycle() {
        setImageBitmap(null);
        if ((this.currentBitmap == null) || (this.currentBitmap.isRecycled()))
            return;
        this.currentBitmap.recycle();
        this.currentBitmap = null;
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        currentBitmap = bm;
        super.setImageBitmap(currentBitmap);
        if (imageChangeListener != null)
            imageChangeListener.changed((currentBitmap == null));
    }

    @Override
    public void setImageDrawable(Drawable d) {
        super.setImageDrawable(d);
        if (imageChangeListener != null)
            imageChangeListener.changed((d == null));
    }

    @Override
    public void setImageResource(int id) {
        super.setImageResource(id);
    }

    public interface ImageChangeListener {
        // a callback for when a change has been made to this imageView
        void changed(boolean isEmpty);
    }

    public ImageChangeListener getImageChangeListener() {
        return imageChangeListener;
    }

    public void setImageChangeListener(ImageChangeListener imageChangeListener) {
        this.imageChangeListener = imageChangeListener;
    }

    private int imageWidth;
    private int imageHeight;

    public void setImageWidth(int w) {
        imageWidth = w;
    }

    public void setImageHeight(int h) {
        imageHeight = h;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        /**
         * if both width and height are set scale width first. modify in future
         * if necessary
         */

        if (widthMode == MeasureSpec.EXACTLY || widthMode == MeasureSpec.AT_MOST) {
            scaleToWidth = true;
        } else if (heightMode == MeasureSpec.EXACTLY || heightMode == MeasureSpec.AT_MOST) {
            scaleToWidth = false;
        } else
            throw new IllegalStateException("width or height needs to be set to match_parent or a specific dimension");

        if (imageWidth == 0) {
            // nothing to measure
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        } else {
            if (scaleToWidth) {
                int iw = imageWidth;
                int ih = imageHeight;
                int heightC = width * ih / iw;
                if (height > 0)
                    if (heightC > height) {
                        // dont let hegiht be greater then set max
                        heightC = height;
                        width = heightC * iw / ih;
                    }

                this.setScaleType(ScaleType.CENTER_CROP);
                setMeasuredDimension(width, heightC);

            } else {
                // need to scale to height instead
                int marg = 0;
                if (getParent() != null) {
                    if (getParent().getParent() != null) {
                        marg += ((RelativeLayout) getParent().getParent()).getPaddingTop();
                        marg += ((RelativeLayout) getParent().getParent()).getPaddingBottom();
                    }
                }

                int iw = imageWidth;
                int ih = imageHeight;

                width = height * iw / ih;
                height -= marg;
                setMeasuredDimension(width, height);
            }

        }
    }

}
时间: 2024-08-05 01:37:23

android how to deal with data when listview refresh的相关文章

Android图表库MPAndroidChart(十四)——在ListView种使用相同的图表

Android图表库MPAndroidChart(十四)--在ListView种使用相同的图表 各位好久不见,最近挺忙的,所有博客更新的比较少,这里今天说个比较简单的图表,那就是在ListView中使用相同的图标,因为我们在下篇会讲解使用不同的图表,相同的图表还是比较简单的,我们来看下效果图 具体怎么去实现呢,这里我们先写点铺垫,比如我们需要一个基类的Activity ViewPagerBaseActivity package com.liuguilin.mpandroidchartsample

Android高级控件(一)——ListView绑定CheckBox实现全选,添加和删除等功能

Android高级控件(一)--ListView绑定CheckBox实现全选,添加和删除等功能 这个控件还是挺复杂的.也是项目中应该算是比較经常使用的了,所以写了一个小Demo来讲讲,主要是自己定义adapter的使用方法.加了非常多的推断等等等等-.我们先来看看实现的效果吧! 好的,我们新建一个项目LvCheckBox 我们事先先把这两个布局写好吧,一个是主布局,另一个listview的item.xml.相信不用多说 activity_main.xml <LinearLayout xmlns:

Android基础入门教程——2.4.6 ListView的数据更新问题

Android基础入门教程--2.4.6 ListView的数据更新问题 标签(空格分隔): Android基础入门教程 本节引言: 我们前面已经学习了ListView的一些基本用法咧,但是细心的你可能发现了,我们的数据 一开始定义好的,都是静态的,但是实际开发中,我们的数据往往都是动态变化的,比如 我增删该了某一列,那么列表显示的数据也应该进行同步的更新,那么本节我们就来探讨 下ListView数据更新的问题,包括全部更新,以及更新其中的一项,那么开始本节内容!~ 1.先写个正常的demo先

Android基础入门教程——2.4.5 ListView之checkbox错位问题解决

Android基础入门教程--2.4.5 ListView之checkbox错位问题解决 标签(空格分隔): Android基础入门教程 本节引言: 作为ListView经典问题之一,如果你尝试过自定义ListView的item,在上面带有一个checkbox的话,那么 当你的item数超过了一页的话,就会出现这个问题,下面我们来分析下出现这种问题的原因,以及如何来 解决这个问题! 1.问题发生的原因: 这是网上找来的一幅关于ListView getView方法调用机制的一个图 上图中有一个Re

Android学习笔记二十四之ListView列表视图二

Android学习笔记二十四之ListView列表视图二 前面一篇我们介绍了常用的几种适配器的简单实现和ListView的简单使用,这一篇中,我们介绍一下ListView的优化和一些其它的问题. ListView优化方法一 在ListView中,我们最常用的就是自定义Adapter,在我们自定义Adapter中,需要实现两个比较重要的方法getCount()和getView(),前者是负责计算ListView的总Item数,后者是生成Item,有多少个Item就会调用getView()方法多少次

Android MVP设计框架模板 之 漂亮ListView上拉刷新下拉加载更多

mvp的全称为Model-View-Presenter,Model提供数据,View负责显示,Controller/Presenter负责逻辑的处理.MVP与MVC有着一个重大的区别:在MVP中View并不直接使用Model,它们之间的通信是通过Presenter (MVC中的Controller)来进行的,所有的交互都发生在Presenter内部,而在MVC中View会直接从Model中读取数据而不是通过 Controller. 项目中大部分是面对接口编程,通过P层可以预先将所有需要的接口功能

【android】从源码上分析ListView/GridView调用setEmptyView不起作用的原因及解决办法

当我们使用ListView或GridView的时候,当列表为空的时候,我们往往需要一个Loading或者一段提示文字又或者一个特殊的View来提示用户操作,这个时候就用到了setEmptyView()方法. setEmptyView()其实是AdapterView的方法,而我们开发中常用到的ListView, GridView, ExpandableListView等都是继承于AdapterView的,所以可以直接调用这个方法. 但是问题来了,当你这个emptyview不在当前的View hie

Android 快速开发系列 打造万能的ListView GridView 适配器

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38902805 ,本文出自[张鸿洋的博客] 1.概述 相信做Android开发的写得最多的就是ListView,GridView的适配器吧,记得以前开发一同事开发项目,一个项目下来基本就一直在写ListView的Adapter都快吐了~~~对于Adapter一般都继承BaseAdapter复写几个方法,getView里面使用ViewHolder模式,其实大部分的代码基本都是类似的

Android Widget 小部件(四---完结) 使用ListView、GridView、StackView、ViewFlipper展示Widget

官方有话这样说: A RemoteViews object (and, consequently, an App Widget) can support the following layout classes: FrameLayout LinearLayout RelativeLayout And the following widget classes: AnalogClock Button Chronometer ImageButton ImageView ProgressBar Text