ListView中我们可以添加我们自己的adpter,这样我们可以丰富我们的ListView的Item,并且如果我们的ListView很长的时候。如果一下子都显示出来的话,那么我们要通过划屏滚动很长时间才能看到我们要的数据(我们的数据如果在最下边),有时候我们的数据就在前几行,那么我们有必要把全部数据都显示在ListView中么?如果我们的数据是从网络获取,或者通过开的线程,耗时性很长的操作才能返回数据的呢?这就要求我们在没有全部加载必要的情况下部分加载ListView中的内容,通过activity实现OnScrollListener这个接口我们可是在划屏到最底下的时候设置加载来实现动态的添加ListView中的数据。

很简单的例子,通过ListView显示出我们的android手机中都安装了哪些的应用,然后返回。通过浏览到最底部的时候触发OnScrollListener事件,我们就会把更多的应用添加到ListView中去。

直接看代码:

Java代码  收藏代码
  1. package com.jftt.listviewupdate;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.app.AlertDialog;  
  8. import android.content.DialogInterface;  
  9. import android.content.pm.ApplicationInfo;  
  10. import android.content.pm.PackageInfo;  
  11. import android.content.pm.PackageManager;  
  12. import android.content.pm.PackageManager.NameNotFoundException;  
  13. import android.os.Bundle;  
  14. import android.os.Handler;  
  15. import android.os.Message;  
  16. import android.view.View;  
  17. import android.widget.AbsListView;  
  18. import android.widget.AdapterView;  
  19. import android.widget.LinearLayout;  
  20. import android.widget.ListView;  
  21. import android.widget.Toast;  
  22. import android.widget.AbsListView.OnScrollListener;  
  23. import android.widget.AdapterView.OnItemClickListener;  
  24.   
  25. public class ListViewUpdate extends Activity implements OnScrollListener,  
  26.         OnItemClickListener {  
  27.     private List<ApplicationItemInfo> applicationItemInfoList;  
  28.     private List<ApplicationItemInfo> applicationItemInfoListTemp;  
  29.     private ApplicationItemAdapter applicationItemAdapter;  
  30.     private Handler handler;  
  31.   
  32.     private ListView listView;  
  33.   
  34.     private LinearLayout loading;  
  35.     private boolean refreshable;  
  36.   
  37.     @Override  
  38.     public void onCreate(Bundle savedInstanceState) {  
  39.         super.onCreate(savedInstanceState);  
  40.         init();  
  41.         updateApplicationItemInfoList();  
  42.     }  
  43.   
  44.     private void init() {  
  45.         setContentView(R.layout.main);  
  46.         listView = (ListView) findViewById(R.id.list);  
  47.         loading = (LinearLayout) findViewById(R.id.loading);  
  48.   
  49.         applicationItemInfoList = new ArrayList<ApplicationItemInfo>();  
  50.         applicationItemAdapter = new ApplicationItemAdapter(this,  
  51.                 applicationItemInfoList);  
  52.         handler = createHandler();  
  53.         refreshable = true;  
  54.   
  55.         listView.setAdapter(applicationItemAdapter);  
  56.         listView.setOnItemClickListener(this);  
  57.         listView.setOnScrollListener(this);  
  58.     }  
  59.   
  60.     private Handler createHandler() {  
  61.         Handler handler = new Handler() {  
  62.             @Override  
  63.             public void handleMessage(Message msg) {  
  64.                 super.handleMessage(msg);  
  65.                 switch (msg.what) {  
  66.                 case 1:  
  67.                     if (applicationItemInfoListTemp != null) {  
  68.                         ListViewUpdate.this.applicationItemInfoList  
  69.                                 .addAll(applicationItemInfoListTemp);  
  70.                         ListViewUpdate.this.applicationItemAdapter  
  71.                                 .notifyDataSetChanged();  
  72.                         ListViewUpdate.this.loading.setVisibility(View.GONE);  
  73.                         ListViewUpdate.this.refreshable = true;  
  74.                     } else {  
  75.                         ListViewUpdate.this.loading.setVisibility(View.GONE);  
  76.                         Toast.makeText(ListViewUpdate.this,  
  77.                                 "All of applications have been shown!",  
  78.                                 Toast.LENGTH_LONG).show();  
  79.                     }  
  80.                     break;  
  81.                 default:  
  82.                     break;  
  83.                 }  
  84.             }  
  85.         };  
  86.         return handler;  
  87.     }  
  88.   
  89.     public Handler getHandler() {  
  90.         return handler;  
  91.     }  
  92.   
  93.     private void updateApplicationItemInfoList() {  
  94.         new Thread(new Runnable() {  
  95.   
  96.             @Override  
  97.             public void run() {  
  98.                 if (ListViewUpdate.this.applicationItemInfoList.size() < 80) {  
  99.                     ListViewUpdate.this.applicationItemInfoListTemp = ListViewUpdate.this  
  100.                             .getLocalApplicationInfo();  
  101.                 } else {  
  102.                     ListViewUpdate.this.applicationItemInfoListTemp = null;  
  103.                 }  
  104.   
  105.                 try {  
  106.                     Thread.sleep(5000);  
  107.                 } catch (InterruptedException e) {  
  108.                     e.printStackTrace();  
  109.                 }  
  110.   
  111.                 Message message = new Message();  
  112.                 message.what = 1;  
  113.                 ListViewUpdate.this.getHandler().sendMessage(message);  
  114.             }  
  115.   
  116.         }).start();  
  117.     }  
  118.   
  119.     private ArrayList<ApplicationItemInfo> getLocalApplicationInfo() {  
  120.   
  121.         PackageManager packageManager = this.getPackageManager();  
  122.         ArrayList<ApplicationItemInfo> applicationItemInfoList = new ArrayList<ApplicationItemInfo>();  
  123.         List<PackageInfo> packageInfoList = packageManager  
  124.                 .getInstalledPackages(0);  
  125.   
  126.         for (int i = packageInfoList.size(); i > 0; i--) {  
  127.   
  128.             ApplicationInfo applicationInfo = packageInfoList.get(i - 1).applicationInfo;  
  129.   
  130.             if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {  
  131.                 try {  
  132.                     ApplicationItemInfo applicationItemInfo = new ApplicationItemInfo();  
  133.   
  134.                     String packageName = packageInfoList.get(i - 1).packageName;  
  135.                     String[] permissions = packageManager.getPackageInfo(  
  136.                             packageName, PackageManager.GET_PERMISSIONS).requestedPermissions;  
  137.   
  138.                     applicationItemInfo.setPackageName(packageName);  
  139.                     applicationItemInfo.setPermissions(permissions);  
  140.   
  141.                     applicationItemInfo.setIcon(packageManager  
  142.                             .getApplicationIcon(applicationInfo));  
  143.                     applicationItemInfo.setName(packageManager  
  144.                             .getApplicationLabel(applicationInfo).toString());  
  145.   
  146.                     applicationItemInfoList.add(applicationItemInfo);  
  147.   
  148.                 } catch (NameNotFoundException e) {  
  149.                     // TODO Auto-generated catch block  
  150.                 }  
  151.             }  
  152.         }  
  153.         if ((!applicationItemInfoList.isEmpty())  
  154.                 && (applicationItemInfoList.size() < 10)) {  
  155.             for (int i = applicationItemInfoList.size(); i < 10; i++) {  
  156.                 applicationItemInfoList.add(applicationItemInfoList.get(9 - i));  
  157.             }  
  158.         }  
  159.         return applicationItemInfoList;  
  160.     }  
  161.   
  162.     @Override  
  163.     public void onScroll(AbsListView view, int firstVisibleItem,  
  164.             int visibleItemCount, int totalItemCount) {  
  165.         // TODO Auto-generated method stub  
  166.         if ((firstVisibleItem + visibleItemCount == totalItemCount)  
  167.                 && (totalItemCount != 0)) {  
  168.             if (refreshable) {  
  169.                 refreshable = false;  
  170.                 loading.setVisibility(View.VISIBLE);  
  171.                 updateApplicationItemInfoList();  
  172.             }  
  173.         }  
  174.     }  
  175.   
  176.     @Override  
  177.     public void onScrollStateChanged(AbsListView view, int scrollState) {  
  178.         // TODO Auto-generated method stub  
  179.   
  180.     }  
  181.   
  182.     @Override  
  183.     public void onItemClick(AdapterView<?> arg0, View view, int position,  
  184.             long id) {  
  185.         // TODO Auto-generated method stub  
  186.         AlertDialog.Builder permissionDialog = new AlertDialog.Builder(this);  
  187.         permissionDialog.setIcon(applicationItemInfoList.get(position)  
  188.                 .getIcon());  
  189.         permissionDialog.setTitle(applicationItemInfoList.get(position)  
  190.                 .getName());  
  191.         String[] permissions = applicationItemInfoList.get(position)  
  192.                 .getPermissions();  
  193.   
  194.         StringBuffer temp = new StringBuffer();  
  195.         if (permissions != null) {  
  196.             for (int i = 0; i < permissions.length; i++) {  
  197.                 temp.append(permissions[i]);  
  198.                 temp.append("\n");  
  199.             }  
  200.         } else {  
  201.             temp.append("permission is null");  
  202.         }  
  203.         permissionDialog.setMessage(temp.toString());  
  204.         permissionDialog.setPositiveButton("OK",  
  205.                 new DialogInterface.OnClickListener() {  
  206.   
  207.                     @Override  
  208.                     public void onClick(DialogInterface dialog, int which) {  
  209.                         // TODO Auto-generated method stub  
  210.                         dialog.cancel();  
  211.                     }  
  212.                 });  
  213.         permissionDialog.create().show();  
  214.     }  
  215. }  
Java代码  收藏代码
  1. package com.jftt.listviewupdate;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.content.Context;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10. import android.widget.ImageView;  
  11. import android.widget.TextView;  
  12.   
  13. public class ApplicationItemAdapter extends BaseAdapter {  
  14.   
  15.     private LayoutInflater layoutInflater;  
  16.     private List<ApplicationItemInfo> applicationItemInfoList;  
  17.   
  18.     public ApplicationItemAdapter(Context context,  
  19.             List<ApplicationItemInfo> applicationItemInfoList) {  
  20.   
  21.         this.applicationItemInfoList = applicationItemInfoList;  
  22.         layoutInflater = LayoutInflater.from(context);  
  23.   
  24.     }  
  25.   
  26.     @Override  
  27.     public int getCount() {  
  28.         // TODO Auto-generated method stub  
  29.         return applicationItemInfoList.size();  
  30.     }  
  31.   
  32.     @Override  
  33.     public Object getItem(int position) {  
  34.         // TODO Auto-generated method stub  
  35.         return applicationItemInfoList.get(position);  
  36.     }  
  37.   
  38.     @Override  
  39.     public long getItemId(int position) {  
  40.         // TODO Auto-generated method stub  
  41.         return position;  
  42.     }  
  43.   
  44.     @Override  
  45.     public View getView(int position, View view, ViewGroup viewGroup) {  
  46.   
  47.         ViewHolder viewHolder;  
  48.   
  49.         // TODO Auto-generated method stub  
  50.         if (view == null) {  
  51.             view = layoutInflater.inflate(R.layout.application_item, null);  
  52.             viewHolder = new ViewHolder();  
  53.             viewHolder.icon = (ImageView) view.findViewById(R.id.icon);  
  54.             viewHolder.title = (TextView) view.findViewById(R.id.title);  
  55.             viewHolder.packageName = (TextView) view  
  56.                     .findViewById(R.id.packagename);  
  57.             view.setTag(viewHolder);  
  58.         } else {  
  59.             viewHolder = (ViewHolder) view.getTag();  
  60.         }  
  61.   
  62.         viewHolder.icon.setImageDrawable(applicationItemInfoList.get(position)  
  63.                 .getIcon());  
  64.         viewHolder.title.setText(applicationItemInfoList.get(position)  
  65.                 .getName());  
  66.         viewHolder.packageName.setText(applicationItemInfoList.get(position)  
  67.                 .getPackageName());  
  68.   
  69.         return view;  
  70.     }  
  71.   
  72.     private class ViewHolder {  
  73.         ImageView icon;  
  74.         TextView title;  
  75.         TextView packageName;  
  76.     }  
  77.   
  78. }  
Java代码  收藏代码
  1. package com.jftt.listviewupdate;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. import android.graphics.drawable.Drawable;  
  6.   
  7. public class ApplicationItemInfo implements Serializable {  
  8.   
  9.     private static final long serialVersionUID = -738265888959292655L;  
  10.   
  11.     private Drawable icon;  
  12.   
  13.     private String packageName;  
  14.   
  15.     private String name;  
  16.   
  17.     private String[] permissions;  
  18.   
  19.     public Drawable getIcon() {  
  20.         return icon;  
  21.     }  
  22.   
  23.     public void setIcon(Drawable icon) {  
  24.         this.icon = icon;  
  25.     }  
  26.   
  27.     public String getPackageName() {  
  28.         return packageName;  
  29.     }  
  30.   
  31.     public void setPackageName(String packageName) {  
  32.         this.packageName = packageName;  
  33.     }  
  34.   
  35.     public String getName() {  
  36.         return name;  
  37.     }  
  38.   
  39.     public void setName(String name) {  
  40.         this.name = name;  
  41.     }  
  42.   
  43.     public String[] getPermissions() {  
  44.         return permissions;  
  45.     }  
  46.   
  47.     public void setPermissions(String[] permissions) {  
  48.         this.permissions = permissions;  
  49.     }  
  50.   
  51. }  
posted on 2013-01-25 22:29  zhengbeibei  阅读(705)  评论(0编辑  收藏  举报