Android project: Mobile Security Guard (16)-complex ListView Analysis

Source: Internet
Author: User

Android project: Mobile Security Guard (16)-complex ListView Analysis
Android project: Mobile Security Guard (16)-Introduction to complicated ListView1

Next, the content of yesterday will continue to improve the Application List today. First, applications are divided into system applications and user applications. The installation location is divided into mobile phone memory and sdcard. Therefore, we add a category in ListView, it can be divided into system applications and user applications. Each item shows the installation location. The final effect is as follows:

2. determine the application type and installation location

The ApplicationInfo object has a flags attribute, which has many State values. We use it and FLAG_EXTERNAL_STORAGE and FLAG_SYSTEM to determine whether the State value exists. The Code is as follows:

Int flag = applicationInfo. flags; if (flag & ApplicationInfo. FLAG_EXTERNAL_STORAGE) = ApplicationInfo. FLAG_EXTERNAL_STORAGE) {// install appInfo in sdcard. isSdcardApp = true;} else {// installed on the mobile phone memory appInfo. isSdcardApp = false;} if (flag & ApplicationInfo. FLAG_SYSTEM) = ApplicationInfo. FLAG_SYSTEM) {// System Application appInfo. isUserApp = false;} else {// appInfo of the user application. isUserApp = true ;}

In the getView () method of AppManagerAdapter, judge and assign values. The Code is as follows:

If (appInfo. isSdcardApp) {holder. tvLocation. setText ("External Storage") ;}else {holder. tvLocation. setText ("mobile phone memory ");}
3. Add two la s to ListView

The next step is the focus of today. Add the second item layout for ListView, which divides applications into two categories: system applications and user applications, and store them using two lists. The Code is as follows:

ArrayList installedApp = getInstalledApp (); ArrayList userList = new ArrayList (); ArrayList systemList = new ArrayList (); // split it into two lists for (AppInfo info: installedApp) {if (info. isUserApp) {userList. add (info);} else {systemList. add (info );}}

To add two or more la s to the ListView, You need to override the two methods of the Adapter: getViewTypeCount () and getItemViewType (). The number of layout types and the type of the current item are returned respectively, the code for AppManagerAdapter is modified as follows:

/*** Application List adapter ** Created by XWdoor on. * blog: http://blog.csdn.net/xwdoor */public class AppManagerAdapter extends BaseAdapter {private final ArrayList mUserList; private final ArrayList mSystemList; private final Context mContext; public AppManagerAdapter (Context ctx, ArrayList userList, ArrayList systemList) {this. mContext = ctx; this. mUserList = userList; this. mSystemList = systemList;} @ Override public int getCount () {return mUserList. size () + mSystemList. size () + 2; // Add two title bars} @ Override public AppInfo getItem (int position) {// in case of a title bar, returns null if (position = 0 | position = mUserList. size () + 1) {return null;} if (position <mUserList. size () + 1) {return mUserList. get (position-1); // remove the placeholder of the title bar} else {return mSystemList. get (position-mUserList. size ()-2); // The placeholder of the two title bars to be removed }@override public long getItemId (int position) {return position;} // return the number of layout types, the two convertView @ Override public int getViewTypeCount () {return 2;} // return the corresponding layout type based on the current location, @ Override public int getItemViewType (int position) {if (position = 0 | position = mUserList. size () + 1) {// return 0;} else {return 1 ;}@override public View getView (int position, View convertView, ViewGroup parent) {// determine the current layout type. The system caches multiple convertviews and returns the corresponding convertView based on the current layout type, load different Layout switch (getItemViewType (position) {case 0: // Title HeaderHolder headerHolder = null; if (convertView = null) {convertView = View. inflate (mContext, R. layout. item_app_manager_header, null); headerHolder = new HeaderHolder (); headerHolder. tvHeader = (TextView) convertView. findViewById (R. id. TV _header); convertView. setTag (headerHolder);} else {headerHolder = (HeaderHolder) convertView. getTag () ;}if (position = 0) {headerHolder. tvHeader. setText ("user application (" + mUserList. size () + ")");} else {headerHolder. tvHeader. setText ("System Application (" + mSystemList. size () + ")");} break; case 1: // apply item ViewHolder holder = null; if (convertView = null) {convertView = View. inflate (mContext, R. layout. item_app_manager_adapter, null); holder = new ViewHolder (); holder. tvName = (TextView) convertView. findViewById (R. id. TV _name); holder. tvLocation = (TextView) convertView. findViewById (R. id. TV _location); holder. ivIcon = (ImageView) convertView. findViewById (R. id. iv_icon); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag ();} AppInfo appInfo = getItem (position); holder. tvName. setText (appInfo. appName); holder. ivIcon. setImageDrawable (appInfo. icon); if (appInfo. isSdcardApp) {holder. tvLocation. setText ("external memory");} else {holder. tvLocation. setText ("cell phone memory") ;}break;} return convertView;} static class ViewHolder {public TextView tvName; public ImageView ivIcon; public TextView tvLocation ;} static class HeaderHolder {public TextView tvHeader ;}}

First, the constructor needs to input two application lists. The getCount () method needs to return the total number of two lists plus two title bars. The getItem () method returns results based on the situation. If the title bar is used, null is returned, if an item is specific, the placeholder of the title bar needs to be removed. Similarly, in the getView () method, different la s must be loaded based on different types, here we need to talk about the reuse of views. According to the return value of the getViewTypeCount () method, the system will save multiple convertviews and return the corresponding convertView based on the current layout type. Best results

4. ListView title bar Suspension

Every time the Title Bar slides to the top, there will be a floating effect, indicating that the following items belong to the title type. In fact, this is only a blind way, that title bar has been, you only need to modify the display text when appropriate. You need to modify the layout file of AppManagerActivity, put the ListView in a FrameLayout layout, and add a TextView to overwrite it, add a progress bar for loading data. The Code is as follows:

<Framelayout android: layout_height = "match_parent" android: layout_width = "match_parent">
          
          
              
               
           
  </Framelayout>

Add a scroll listener for ListView to change the text display in the title bar. The Code is as follows:

Final TextView tvHeader = (TextView) findViewById (R. id. TV _header); lvList. setOnScrollListener (new AbsListView. onScrollListener () {@ Override public void onScrollStateChanged (AbsListView view, int scrollState) {}@ Override public void onScroll (AbsListView view, int preview, int visibleItemCount, int totalItemCount) {// firstVisibleItem: first visible element location // visibleItemCount number of visible elements // totalItemCoun Total t elements if (mUserList! = Null & mSystemList! = Null) {if (firstVisibleItem> = mUserList. size () + 1) {// calculate the title bar placeholder tvHeader. setText ("System Application (" + mSystemList. size () + ")");} else {tvHeader. setText ("user application (" + mUserList. size () + ")");}}}});

The effect is as follows:

5. load data in a thread

Retrieving all installed apps on the mobile phone is a time-consuming operation. Therefore, we need to load data in the thread to prevent white screen display for the user experience. Therefore, a progress bar is added and the code is as follows:

@ Override protected void loadData () {llLoading. setVisibility (View. VISIBLE); // load data in the Thread new Thread () {@ Override public void run () {String availRom = getAvailSpace (Environment. getExternalStorageDirectory (). getAbsolutePath (); String availSdcard = getAvailSpace (Environment. getDataDirectory (). getAbsolutePath (); tvRomAvail. setText ("internal storage available:" + availRom); tvSdcardAvail. setText ("sdcard available:" + availSdcard); ArrayList installedApp = getInstalledApp (); mUserList = new ArrayList (); mSystemList = new ArrayList (); for (AppInfo info: installedApp) {if (info. isUserApp) {mUserList. add (info);} else {mSystemList. add (info) ;}}// update the UI data runOnUiThread (new Runnable () {@ Override public void run () {lvList. setAdapter (new AppManagerAdapter (AppManagerActivity. this, mUserList, mSystemList); llLoading. setVisibility (View. GONE );}});}}. start ();}

We usually use Handler to update the UI in the thread, but there are only one or two lines of statements, which seems to be useless. here we can use the runOnUiThread () method, which runs in the main thread, so you can use it with confidence.

6. Conclusion

Today's content is quite full. I learned two knowledge points:

Convenient UI update in implementation threads of multiple items in ListView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.