当前位置:  编程技术>.net/c#/asp.net

ListView Adapter优化 实例

    来源: 互联网  发布时间:2014-10-19

    本文导语:  代码如下://方案一 - adapter优化public class listviewAdapterDemo1 extends BaseAdapter {     private String[] mArrData;    private ListView listView;    private AsyncImageLoader asyncImageLoader;    private final LayoutInflater inflater;     public listviewAdapterDemo1(Stri...

代码如下:

//方案一 - adapter优化public class listviewAdapterDemo1 extends BaseAdapter {

    private String[] mArrData;
    private ListView listView;
    private AsyncImageLoader asyncImageLoader;
    private final LayoutInflater inflater;

    public listviewAdapterDemo1(String[] mArrData, Context mContext, ListView listView) {
        this.mArrData = mArrData;
        this.listView = listView;
        asyncImageLoader = new AsyncImageLoader();
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return this.mArrData.length;
    }

    @Override
    public Object getItem(int arg0) {
        return mArrData[arg0];
    }

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

    /***
     * 1.控件对象内存为一个
     * 2.view对象只会创建可视对象次数(有的是8次)根据屏幕可视listview的数量而定,在加载超过可视postion数据时不会重新创建
     *   但是需要每次重新加载数据
     *  
     *   从节省内存的角度出发,这种更合理,毕竟加载数据不需要占用内存
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewholder;
        Log.d("dd", "Position:" + position+"------");
        if (convertView == null) {
            Log.d("dd", "Position:" + position);
            convertView = inflater.inflate(R.layout.list_item_icon_text, null);
            viewholder = new ViewHolder();
            viewholder.icon = (ImageView) convertView.findViewById(R.id.icon);
            viewholder.text = (TextView) convertView.findViewById(R.id.text);

            convertView.setTag(viewholder);
        } else {
            viewholder = (ViewHolder) convertView.getTag();
        }

        viewholder.text.setText(mArrData[position]);
        // Load the image and set it on the ImageView
        String imageUrl = "";
        viewholder.icon.setTag(imageUrl);
        Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {

            public void imageLoaded(Drawable imageDrawable, String imageUrl) {
                ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
                if (imageViewByTag != null) {
                    imageViewByTag.setImageDrawable(imageDrawable);
                }
            }
        });
        if (cachedImage != null)//说明是从缓存池中得到
            viewholder.icon.setImageDrawable(cachedImage);

        return convertView;
    }

    static class ViewHolder {
        ImageView icon;
        TextView text;
    }
}


在listview中的getview方法中也可以这样用这个方法:

代码如下:

public Drawable getDrawable(AsyncImageLoader asyncImageLoader, String imageUrl, final ImageView imageView) {
Drawable drawable = asyncImageLoader.loadDrawable(imageUrl,new ImageCallback() {
@Override public void imageLoaded(Drawable imageDrawable,String imageUrl) {
if (imageDrawable != null)
imageView.setImageDrawable(imageDrawable);
else
imageView.setImageResource(R.drawable.u6_normal);
}
});
return drawable;
}

代码如下:

//方案二---adapter的优化public class listviewAdapterDemo2 extends BaseAdapter {

    private String[] mArrData;
    private ListView listView;
    private AsyncImageLoader asyncImageLoader;
    private final LayoutInflater inflater;
    private Map viewMap = new HashMap();

    public listviewAdapterDemo2(String[] mArrData, Context mContext, ListView listView) {
        this.mArrData = mArrData;
        this.listView = listView;
        asyncImageLoader = new AsyncImageLoader();
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return this.mArrData.length;
    }

    @Override
    public Object getItem(int arg0) {
        return mArrData[arg0];
    }

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

    /**
     * 1.控件对象内存为一个
     * 2.每一个postion对应一个view,且不管前进后退,每个postion数据只加载一次   * 但是从节省内容的角度出发,方案一更节省内存
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewholder;
        Log.d("dd", "Position:" + position+"------");
        convertView = this.viewMap.get(position);
        if (convertView == null) {
            Log.d("dd", "Position:" + position);
            convertView = inflater.inflate(R.layout.list_item_icon_text, null);
            viewholder = new ViewHolder();
            viewholder.icon = (ImageView) convertView.findViewById(R.id.icon);
            viewholder.text = (TextView) convertView.findViewById(R.id.text);

            viewholder.text.setText(mArrData[position]);
            // Load the image and set it on the ImageView
            String imageUrl = "";
            viewholder.icon.setTag(imageUrl);
            Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {

                public void imageLoaded(Drawable imageDrawable, String imageUrl) {
                    ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
                    if (imageViewByTag != null) {
                        imageViewByTag.setImageDrawable(imageDrawable);
                    }
                }
            });
            if (cachedImage != null)//说明是从缓存池中得到
                viewholder.icon.setImageDrawable(cachedImage);

           
            convertView.setTag(viewholder);
            viewMap.put(position, convertView);
        } else {
            viewholder = (ViewHolder) convertView.getTag();
        }
        return convertView;
    }

    static class ViewHolder {
        ImageView icon;
        TextView text;
    }
}

代码如下:

/**图片下载-adpater优化*/
public class AsyncImageLoader {

    private HashMap imageCache; 

    public AsyncImageLoader() { 
        imageCache = new HashMap(); 
    } 

    public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) { 
        //从缓存中检测是否存在
        if (imageCache.containsKey(imageUrl)) { 
            SoftReference softReference = imageCache.get(imageUrl); 
            Drawable drawable = softReference.get(); 
            if (drawable != null) { 
                return drawable; 
            } 
        } 
        final Handler handler = new Handler() { 
            @Override 
            public void handleMessage(Message message) { 
                imageCallback.imageLoaded((Drawable) message.obj, imageUrl); 
            } 
        }; 
        //缓存中没有则从服务器下载
        new Thread() { 
            @Override 
            public void run() { 
                Drawable drawable = loadImageFromUrl(/tech-dotnet/imageUrl/index.html); 
                imageCache.put(imageUrl, new SoftReference(drawable)); 
                Message message = handler.obtainMessage(0, drawable); 
                handler.sendMessage(message); 
            } 
        }.start(); 

        return null; 
    } 

    public static Drawable loadImageFromUrl(/tech-dotnet/String url/index.html) { 
        Drawable drawable = null;
        //根据url从服务器下载图片
        return drawable;
    } 

    public interface ImageCallback { 
        public void imageLoaded(Drawable imageDrawable, String imageUrl); 
    } 

代码如下:

//activity---adapterpublic class ListviewDemoActivity extends Activity {

    private listviewAdapterDemo1 mAdapter;
    private ListView listview;
    private String[] mArrData;

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

        mArrData = new String[1000];
        for (int i = 0; i < 1000; i++) {
            mArrData[i] = "Google IO Adapter" + i;
        }

        listview = (ListView)findViewById(R.id.lv);
        mAdapter = new listviewAdapterDemo1(mArrData,this,listview);
        listview.setAdapter(mAdapter);
    }
}

在main.xml中插入

代码如下:



    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • java命名空间javax.swing.text.html类listview的类成员方法: listview定义及介绍
  • ScrollView与ListView合用(正确计算Listview的高度)的问题解决
  • java命名空间javax.swing.text.html类listview的类成员方法: setpropertiesfromattributes定义及介绍
  • android ListView的右边滚动滑块启用方法 分享
  • java命名空间javax.swing.text.html类listview的类成员方法: paint定义及介绍
  • android中ListView多次刷新重复执行getView的解决方法
  • java命名空间javax.swing.text.html类listview的类成员方法: paintchild定义及介绍
  • c# ListView实现双击Item事件的变通方法
  • andriod中ListView, listItem布局和convertView的缓存与重用详解
  • Android Horizontal ListView
  • C# ListView双击Item事件
  • 谁能给我一个minigui中listview的例子?急!!!!
  • android中如果为listview的item添加listener,也能为item中的button添加listener
  • Android笔记之:在ScrollView中嵌套ListView的方法
  • android ListView自动滚动方法
  • 滑动刷新的ListView Android PullToRefresh
  • 谁能给我一个minigui中listview的例子?急!!!! iis7站长之家
  • android listview 水平滚动和垂直滚动的小例子
  • C# listview添加combobox到单元格的实现代码
  • 解决EditText、ListView以及GridView同时使用,输入法自动跳出来的方法
  • Android用ListView显示SDCard文件列表的小例子


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3