利用Listview實現程序列表展示

利用Listview實現程序列表展示

先來看一下效果圖
效果圖

這個Demo其實也沒有什麼難度,主要的難點就是在那兩個title上,下來我就帶大家看看去怎麼實現這個效果

  1. 首先獲取系統中安裝的應用信息

    /**
     * 獲取應用程序信息
     */
    
    public class AppInfoTools {
        public static List<AppInfo> getAppInfo(Context context) {
            List<AppInfo> list = new ArrayList<>();
            //獲取PackageManager
            PackageManager pm = context.getPackageManager();
            //這裏的0指的是要獲取程序的基本信息
            List<PackageInfo> packageInfos = pm.getInstalledPackages(0);
            for (PackageInfo packageInfo : packageInfos) {
                //包名
                String packageName = packageInfo.packageName;
                //applica
                ApplicationInfo applicationInfo = packageInfo.applicationInfo;
                //應用名稱 Label
                String name = applicationInfo.loadLabel(pm).toString();
                //圖標
                Drawable drawable = applicationInfo.loadIcon(pm);
                //佔用空間大小
                long size = new File(applicationInfo.sourceDir).length();
                //程序標記
                boolean isSystem = false;
                boolean isSD = false;
                //判斷是用戶還是系統程序
                int flags = applicationInfo.flags;
                //系統程序
                if ((flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                    isSystem = true;
                } else {
                    isSystem = false;
                }
                //是否在SD中
                if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
                    isSD = true;
                } else {
                    isSD = false;
                }
                list.add(new AppInfo(packageName, name, drawable, size, isSystem, isSD));
            }
            return list;
        }
    }
    
  2. 聲明listview並給其設置adapter

    layout
         <ListView
        android:id="@+id/lv_softwarem"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
    
        //在這聲明個默認的title
        <TextView
            android:id="@+id/tv_softwarem_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_bright"
            android:padding="8dp"
            android:text="用戶程序(0)"
            android:textSize="18sp"
            />
    
    Activity
        public class MainActivity extends AppCompatActivity {
            //用戶程序集合
            private List<AppInfo> userApp = new ArrayList<>();
            //系統程序集合
            private List<AppInfo> systemApp = new ArrayList<>();
            private SoftWareAdapter myAdapter;
            private TextView tv_title;
            private ListView lv;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                initView();
            }
    
            private void initView() {
                tv_title = (TextView) findViewById(R.id.tv_softwarem_title);
                lv = (ListView) findViewById(R.id.lv_softwarem);
                initData();
            }
    
            private void initData() {
                //當手機上APP很多時,這就是個耗時操作,所以需要放在子線程中
                new Thread() {
                    @Override
                    public void run() {
                        final List<AppInfo> appInfos = AppInfoTools.getAppInfo(MainActivity.this);
                        for (AppInfo appInfo : appInfos) {
                            //判斷應用程序信息,將應用程序存放到不同的集合中
                            if (appInfo.isSystem) {
                                systemApp.add(appInfo);
                            } else {
                                userApp.add(appInfo);
                            }
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //給默認title設置text
                                tv_title.setText("用戶程序(" + userApp.size() + ")");
                                //設置adapter
                                myAdapter = new SoftWareAdapter(MainActivity.this, systemApp, userApp);
                                lv.setAdapter(myAdapter);
                            }
                        });
                    }
                }.start();
                //設置滑動監聽
                lv.setOnScrollListener(new AbsListView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(AbsListView view, int scrollState) {
    
                    }
                     /**
                     * 滾動時回調的方法
                     * @param view
                     * @param firstVisibleItem 當前區域內可視的第一個item
                     * @param visibleItemCount 當前區域內可視的item總數
                     * @param totalItemCount item總數
                     */
                    @Override
                    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                        if (userApp != null && systemApp != null) {
                            if (firstVisibleItem >= userApp.size() + 1) {
                                tv_title.setText("系統程序(" + systemApp.size() + ")");
                            } else {
                                tv_title.setText("用戶程序(" + userApp.size() + ")");
                            }
                        }
                    }
                });
            }
        }
    
  3. 接下來就是我們的重頭戲了,創建adapter

    public class SoftWareAdapter extends BaseAdapter {
        private List<AppInfo> userApp ;
        private List<AppInfo> systemApp;
        private Context context;
    
        public SoftWareAdapter(Context context, List<AppInfo> systemApp, List<AppInfo> userApp) {
            this.context = context;
            this.systemApp = systemApp;
            this.userApp = userApp;
        }
    
        @Override
        public int getCount() {
            return userApp.size() + systemApp.size() + 2;
        }
    
        @Override
        public Object getItem(int position) {
            return null;
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        //拿到指定position的條目類型
        @Override
        public int getItemViewType(int position) {
            //標題佈局類型
            if (position == 0 || position == userApp.size() + 1) {
                return 0;
            }
            //條目佈局
            return 1;
        }
    
        //設置條目類型個數
        @Override
        public int getViewTypeCount() {
            return 2;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //調用getItemViewType方法,根據條目的顯示的樣式類型,設置條目顯示相應的樣式
            if (getItemViewType(position) == 0) {
                TextView textView = new TextView(context);
                textView.setTextSize(18);
                textView.setPadding(8, 8, 8, 8);
                textView.setBackgroundColor(Color.BLUE);
                //標題佈局類型
                if (position == 0) {
                    textView.setText("用戶程序(" + userApp.size() + ")");
                } else {
                    textView.setText("系統程序(" + systemApp.size() + ")");
                }
                return textView;
            } else {
                if (convertView == null) {
                    convertView = View.inflate(context, R.layout.item_software, null);
                    convertView.setTag(new ViewHolder(convertView));
                }
                ViewHolder viewHolder = (ViewHolder) convertView.getTag();
                //判斷當前位置
                AppInfo info;
                if (position <= userApp.size()) {
                    info = userApp.get(position - 1);
                } else {
                    info = systemApp.get(position - userApp.size() - 2);
                }
                viewHolder.icon.setImageDrawable(info.icon);
                viewHolder.name.setText(info.name);
                viewHolder.size.setText(android.text.format.Formatter.formatFileSize(context, info.size));
                viewHolder.isSystem.setText(info.isSD ? "SD" : "手機內存");
                return convertView;
            }
        }
    
        static class ViewHolder {
            public TextView name;
            public TextView isSystem;
            public TextView size;
            public ImageView icon;
    
            public ViewHolder(View item) {
                this.name = (TextView) item.findViewById(R.id.tv_item_software_name);
                this.icon = (ImageView) item.findViewById(R.id.iv_item_software);
                this.size = (TextView) item.findViewById(R.id.tv_item_software_size);
                this.isSystem = (TextView) item.findViewById(R.id.tv_item_software_issystem);
            }
        }
    }
    

這個Demo很簡單,相信大家看代碼就很容易看懂的,好了今天就寫到這了,

代碼下載地址:https://github.com/creativityingenuity/listview_prac

發佈了27 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章