首页 Android listview的封装,复用,及自定义适配器

listview的封装,复用,及自定义适配器

listview的优化。   1.复用历史缓存的view对象 converview 2.减少子孩子id查询的次…

listview的优化。

 

1.复用历史缓存的view对象 converview

2.减少子孩子id查询的次数

3.分批的加载数据

4.分页的加载数据

 

 

优化的原则: 拆东墙补西墙。

 

1.时间换时间(listview的分批加载数据)

 

2.时间换空间  文件拷贝

 

3.空间换时间  文件快速查找的索引。

 

4.空间换空间  虚拟内存  RAMdisk

 

 

 

listview  有一个刷新控件,和开启子线程 更新到界面的代码

private void fillData() {

        ll_loading.setVisibility(View.VISIBLE);

        new Thread() {

            public void run() {

                appInfos = AppInfoProvider.getAppInfo(AppManagerActivity.this);

                userAppInfos = new ArrayList<AppInfo>();

                systemAppInfos = new ArrayList<AppInfo>();

                for (AppInfo info : appInfos) {

                    if (info.isUserApp()) {

                        userAppInfos.add(info);

                    } else {

                        systemAppInfos.add(info);

                    }

                }

                // 加载listview的数据适配器

                runOnUiThread(new Runnable() {

                    @Override

                    public void run() {

                        if (adapter == null) {

                            adapter = new AppManagerAdapter();

                            lv_app_manager.setAdapter(adapter);

                        } else {

                            adapter.notifyDataSetChanged();

                        }

                        ll_loading.setVisibility(View.INVISIBLE);

                    }

                });

            };

        }.start();

    }

自定义适配器

 

private class AppManagerAdapter extends BaseAdapter {

        // 控制listview有多少个条目

        @Override

        public int getCount() {

            // return appInfos.size();

            return userAppInfos.size() + 1 + systemAppInfos.size() + 1;

        }

        @Override

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

            AppInfo appInfo;

            if (position == 0) {// 显示的是用程序有多少个的小标签

                TextView tv = new TextView(getApplicationContext());

                tv.setTextColor(Color.WHITE);

                tv.setBackgroundColor(Color.GRAY);

                tv.setText("用户程序:" + userAppInfos.size() + "个");

                return tv;

            } else if (position == (userAppInfos.size() + 1)) {

                TextView tv = new TextView(getApplicationContext());

                tv.setTextColor(Color.WHITE);

                tv.setBackgroundColor(Color.GRAY);

                tv.setText("系统程序:" + systemAppInfos.size() + "个");

                return tv;

            } else if (position <= userAppInfos.size()) {// 用户程序

                int newposition = position - 1;// 因为多了一个textview的文本占用了位置

                appInfo = userAppInfos.get(newposition);

            } else {// 系统程序

                int newposition = position - 1 - userAppInfos.size() - 1;

                appInfo = systemAppInfos.get(newposition);

            }

            View view;

            ViewHolder holder;

            // if(position<userAppInfos.size()){//这些位置是留个用户程序显示的。

            // appInfo = userAppInfos.get(position);

            // }else{//这些位置是留个系统程序的。

            // int newposition = position - userAppInfos.size();

            // appInfo = systemAppInfos.get(newposition);

            // }

            if (convertView != null && convertView instanceof RelativeLayout) {

                // 不仅需要检查是否为空,还要判断是否是合适的类型去复用

                view = convertView;

                holder = (ViewHolder) view.getTag();

            } else {

                view = View.inflate(getApplicationContext(),

                        R.layout.list_item_appinfo, null);

                holder = new ViewHolder();

                holder.iv_icon = (ImageView) view

                        .findViewById(R.id.iv_app_icon);

                holder.tv_location = (TextView) view

                        .findViewById(R.id.tv_app_location);

                holder.tv_name = (TextView) view.findViewById(R.id.tv_app_name);

                holder.iv_status=(ImageView) view.findViewById(R.id.iv_app_status);

                view.setTag(holder);

            }

            holder.iv_icon.setImageDrawable(appInfo.getIcon());

            holder.tv_name.setText(appInfo.getName());

            if (appInfo.isInRom()) {

                holder.tv_location.setText("手机内存"+appInfo.getUid());

            } else {

                holder.tv_location.setText("外部存储"+appInfo.getUid());

            }



            if(dao.find(appInfo.getPackname())){

                holder.iv_status.setImageResource(R.drawable.lock);

            }else{

                holder.iv_status.setImageResource(R.drawable.unlock);

            }







            return view;

        }

 

用的中间类

 

 

static class ViewHolder {

    TextView tv_name;

    TextView tv_location;

    ImageView iv_icon;

    ImageView iv_status;

}

 

免责声明:文章内容不代表本站立场,本站不对其内容的真实性、完整性、准确性给予任何担保、暗示和承诺,仅供读者参考,文章版权归原作者所有。如本文内容影响到您的合法权益(内容、图片等),请及时联系本站,我们会及时删除处理。

为您推荐

android studio查看android手机日志

android studio查看android手机日志

本文在尝试了,使用adb,eclipse查看log未果之后,使用android studio来查看unity打包的apk...
Conversion to Dalvik format failed: Unable to execute dex: java.nio.BufferOverflowException. Check t

Conversion to Dalvik format failed: Unable to execute dex: java.nio.BufferOverflowException. Check t

在android高版本开发环境(sdk 4.4)导入低版本(sdk 3.0)的工程时编译报错,报错信息如:Convers...
用Bundle和直接用Intent.putExtra(“xx”,yy)传递有什么不同

用Bundle和直接用Intent.putExtra(“xx”,yy)传递有什么不同

QQ群里一个提出来了 。。长知识了。。 Intent intent = new Intent(); intent.put...
Universal-Image-Loader解析——DisplayImageOptions的详细配置

Universal-Image-Loader解析——DisplayImageOptions的详细配置

在使用这个框架的时候,我们必须要配置一个DisplayImageOptions对象来作为ImageLoader.getI...
安卓测试环境的配置

安卓测试环境的配置

AndroidManifest.xml 配置 <uses-permission android:name="and...
返回顶部