ListView 分段加载数据

ListView 分段加载数据

[java]  view plain copy print ?
  1. ListView listView = getListView();  
  2. //添加到脚页显示  这个要继承ListActivity 实现OnScrollListener  
  3. listView.addFooterView(loadingLayout);  

 

 

这位兄弟的Demo非常不错.

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

你可能感兴趣的:(ListView)