ListView中我们可以添加我们自己的adpter,这样我们可以丰富我们的ListView的Item,并且如果我们的ListView很长的时候。如果一下子都显示出来的话,那么我们要通过划屏滚动很长时间才能看到我们要的数据(我们的数据如果在最下边),有时候我们的数据就在前几行,那么我们有必要把全部数据都显示在ListView中么?如果我们的数据是从网络获取,或者通过开的线程,耗时性很长的操作才能返回数据的呢?这就要求我们在没有全部加载必要的情况下部分加载ListView中的内容,通过activity实现OnScrollListener这个接口我们可是在划屏到最底下的时候设置加载来实现动态的添加ListView中的数据。
很简单的例子,通过ListView显示出我们的android手机中都安装了哪些的应用,然后返回。通过浏览到最底部的时候触发OnScrollListener事件,我们就会把更多的应用添加到ListView中去。
直接看代码:
package com.jftt.listviewupdate;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
public class ListViewUpdate extends Activity implements OnScrollListener,
OnItemClickListener {
private List<ApplicationItemInfo> applicationItemInfoList;
private List<ApplicationItemInfo> applicationItemInfoListTemp;
private ApplicationItemAdapter applicationItemAdapter;
private Handler handler;
private ListView listView;
private LinearLayout loading;
private boolean refreshable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
updateApplicationItemInfoList();
}
private void init() {
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.list);
loading = (LinearLayout) findViewById(R.id.loading);
applicationItemInfoList = new ArrayList<ApplicationItemInfo>();
applicationItemAdapter = new ApplicationItemAdapter(this,
applicationItemInfoList);
handler = createHandler();
refreshable = true;
listView.setAdapter(applicationItemAdapter);
listView.setOnItemClickListener(this);
listView.setOnScrollListener(this);
}
private Handler createHandler() {
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
if (applicationItemInfoListTemp != null) {
ListViewUpdate.this.applicationItemInfoList
.addAll(applicationItemInfoListTemp);
ListViewUpdate.this.applicationItemAdapter
.notifyDataSetChanged();
ListViewUpdate.this.loading.setVisibility(View.GONE);
ListViewUpdate.this.refreshable = true;
} else {
ListViewUpdate.this.loading.setVisibility(View.GONE);
Toast.makeText(ListViewUpdate.this,
"All of applications have been shown!",
Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
};
return handler;
}
public Handler getHandler() {
return handler;
}
private void updateApplicationItemInfoList() {
new Thread(new Runnable() {
@Override
public void run() {
if (ListViewUpdate.this.applicationItemInfoList.size() < 80) {
ListViewUpdate.this.applicationItemInfoListTemp = ListViewUpdate.this
.getLocalApplicationInfo();
} else {
ListViewUpdate.this.applicationItemInfoListTemp = null;
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message message = new Message();
message.what = 1;
ListViewUpdate.this.getHandler().sendMessage(message);
}
}).start();
}
private ArrayList<ApplicationItemInfo> getLocalApplicationInfo() {
PackageManager packageManager = this.getPackageManager();
ArrayList<ApplicationItemInfo> applicationItemInfoList = new ArrayList<ApplicationItemInfo>();
List<PackageInfo> packageInfoList = packageManager
.getInstalledPackages(0);
for (int i = packageInfoList.size(); i > 0; i--) {
ApplicationInfo applicationInfo = packageInfoList.get(i - 1).applicationInfo;
if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
try {
ApplicationItemInfo applicationItemInfo = new ApplicationItemInfo();
String packageName = packageInfoList.get(i - 1).packageName;
String[] permissions = packageManager.getPackageInfo(
packageName, PackageManager.GET_PERMISSIONS).requestedPermissions;
applicationItemInfo.setPackageName(packageName);
applicationItemInfo.setPermissions(permissions);
applicationItemInfo.setIcon(packageManager
.getApplicationIcon(applicationInfo));
applicationItemInfo.setName(packageManager
.getApplicationLabel(applicationInfo).toString());
applicationItemInfoList.add(applicationItemInfo);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
}
}
}
if ((!applicationItemInfoList.isEmpty())
&& (applicationItemInfoList.size() < 10)) {
for (int i = applicationItemInfoList.size(); i < 10; i++) {
applicationItemInfoList.add(applicationItemInfoList.get(9 - i));
}
}
return applicationItemInfoList;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
if ((firstVisibleItem + visibleItemCount == totalItemCount)
&& (totalItemCount != 0)) {
if (refreshable) {
refreshable = false;
loading.setVisibility(View.VISIBLE);
updateApplicationItemInfoList();
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
// TODO Auto-generated method stub
AlertDialog.Builder permissionDialog = new AlertDialog.Builder(this);
permissionDialog.setIcon(applicationItemInfoList.get(position)
.getIcon());
permissionDialog.setTitle(applicationItemInfoList.get(position)
.getName());
String[] permissions = applicationItemInfoList.get(position)
.getPermissions();
StringBuffer temp = new StringBuffer();
if (permissions != null) {
for (int i = 0; i < permissions.length; i++) {
temp.append(permissions[i]);
temp.append("\n");
}
} else {
temp.append("permission is null");
}
permissionDialog.setMessage(temp.toString());
permissionDialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
permissionDialog.create().show();
}
}
package com.jftt.listviewupdate;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ApplicationItemAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<ApplicationItemInfo> applicationItemInfoList;
public ApplicationItemAdapter(Context context,
List<ApplicationItemInfo> applicationItemInfoList) {
this.applicationItemInfoList = applicationItemInfoList;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return applicationItemInfoList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return applicationItemInfoList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
// TODO Auto-generated method stub
if (view == null) {
view = layoutInflater.inflate(R.layout.application_item, null);
viewHolder = new ViewHolder();
viewHolder.icon = (ImageView) view.findViewById(R.id.icon);
viewHolder.title = (TextView) view.findViewById(R.id.title);
viewHolder.packageName = (TextView) view
.findViewById(R.id.packagename);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.icon.setImageDrawable(applicationItemInfoList.get(position)
.getIcon());
viewHolder.title.setText(applicationItemInfoList.get(position)
.getName());
viewHolder.packageName.setText(applicationItemInfoList.get(position)
.getPackageName());
return view;
}
private class ViewHolder {
ImageView icon;
TextView title;
TextView packageName;
}
}
package com.jftt.listviewupdate;
import java.io.Serializable;
import android.graphics.drawable.Drawable;
public class ApplicationItemInfo implements Serializable {
private static final long serialVersionUID = -738265888959292655L;
private Drawable icon;
private String packageName;
private String name;
private String[] permissions;
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getPermissions() {
return permissions;
}
public void setPermissions(String[] permissions) {
this.permissions = permissions;
}
}