ListView---复杂的listview显示

1 . 初始化数据
private void fillData() {
        ll_loading.setVisibility(View.VISIBLE);     // 显示进度
        new Thread() {
            public void run() {
                appInfos = AppInfoProvider.getAppInfos(AppManagerActivity.this);      //得到所有的应用程序
                userAppInfos = new ArrayList<AppInfo>();      //userAppinfos用户的应用程序    
                systemAppInfos = new ArrayList<AppInfo>();        //systemAppInfos系统的应用程序
                for (AppInfo info : appInfos) {      //遍历出所有的应用程序
                    if (info.isUserApp()) {
                        userAppInfos.add(info);
                    } else {
                        systemAppInfos.add(info);
                    }
                }
                // 加载listview的数据适配器
                runOnUiThread(new Runnable() {     // UI更新界面
                    @Override
                    public void run() {
                        if (adapter == null) {
                            adapter = new AppManagerAdapter();    // Adapter
                            lv_app_manager.setAdapter(adapter);
                        } else {
                            adapter.notifyDataSetChanged();
                        }
                        ll_loading.setVisibility(View.INVISIBLE);   // 隐藏进度
                    }
                });
            };
        }.start();
}
2. 
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_status);
                view.setTag(holder);
            }
            holder.iv_icon.setImageDrawable(appInfo.getIcon());
            holder.tv_name.setText(appInfo.getName());
            if (appInfo.isInRom()) {
                holder.tv_location.setText("手机内存");
            } else {
                holder.tv_location.setText("外部存储");
            }
            if(dao.find(appInfo.getPackname())){
                holder.iv_status.setImageResource(R.drawable.lock);
            }else{
                holder.iv_status.setImageResource(R.drawable.unlock);
            }
            return view;
        }

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

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

}

    static class ViewHolder {
        TextView tv_name;
        TextView tv_location;
        ImageView iv_icon;
        ImageView iv_status;
    }
3// 给listview注册一个滚动的监听器
lv_app_manager.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            // 滚动的时候调用的方法。
            // firstVisibleItem 第一个可见条目在listview集合里面的位置。
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                dismissPopupWindow();
                if (userAppInfos != null && systemAppInfos != null) {
                    if (firstVisibleItem > userAppInfos.size()) {
                        tv_status.setText("系统程序:" + systemAppInfos.size() + "个");      //list分类显示出:系统程序
                    } else {
                        tv_status.setText("用户程序:" + userAppInfos.size() + "个");    //list分类显示出:用户程序
                    }
                }
            }
});
附录:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="55dip"
        android:background="#8866ff00"
        android:gravity="center"
        android:text="软件管理器"
        android:textColor="#000000"
        android:textSize="22sp" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tv_avail_rom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="内存可用:"
            android:textColor="#000000" />

        <TextView
            android:id="@+id/tv_avail_sd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="SD卡可用:"
            android:textColor="#000000" />
    </RelativeLayout>

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:id="@+id/ll_loading"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:orientation="vertical"
            android:visibility="invisible" >

            <ProgressBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="正在加载程序信息..." />
        </LinearLayout>

        <ListView
            android:id="@+id/lv_app_manager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:fastScrollEnabled="true"
            android:overScrollMode="never" >
        </ListView>

        <TextView
            android:id="@+id/tv_status"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#ff888888"
            android:text="用户程序:6个"
            android:textColor="#ffffff" />
    </FrameLayout>

</LinearLayout>
原文地址:https://www.cnblogs.com/java-g/p/4129665.html