我们在使用ListView的时候,可能会碰到需要动态改变ListView的显示的情况。比如说在ListView后面加一个Button,点击Button后,就重刷ListView将该List Item去掉,怎么实现呢?
可以用notifyDataSetChanged。
简单举例,我们想要列出系统中部分ACTION的Application,然后列举出所有应用后,在List Item后面增加一个按钮,点击该按钮,我们可以去掉该List Item。
1、setupAppLists()
if (mPackMgr == null) {
return ;
}
// create a list to store all custom app infos.
mEnableAppInfoList = new ArrayList<DisplayAppInfo>();
mDisableAppInfoList = new ArrayList<DisplayAppInfo>();
mEnableAppInfoList.clear();
mDisableAppInfoList.clear();
String[] actions = Actions.getActions();
for (String action: actions) {
Intent intent = new Intent(action, null);
List<ResolveInfo> resolveInfoList = mPackMgr.queryBroadcastReceivers(intent,
PackageManager.GET_DISABLED_COMPONENTS);
if (resolveInfoList.size() == 0) {
continue;
}
Collections.sort(resolveInfoList, new ResolveInfo.DisplayNameComparator(mPackMgr));
for (ResolveInfo resolveInfo: resolveInfoList) {
ComponentInfo ci = resolveInfo.activityInfo != null ? resolveInfo.activityInfo : resolveInfo.serviceInfo;
ApplicationInfo appInfo = ci.applicationInfo;
if (isUserApp(appInfo.flags)) {
boolean compState = mPmCompHelper.checkoutComponentEnableSetting(resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name);
// set display list filter by package name, and fill up the auto start description.
setupDisplayListFilterByName(resolveInfo, action, compState);
}
}
}
Set<String> keys = mDisplayData.keySet();
for (String key : keys) {
DisplayAppInfo App = mDisplayData.get(key);
if (App.getApplicationStartPermit()) {
mEnableAppInfoList.add(App);
} else {
mDisableAppInfoList.add(App);
}
}
以上大家可以看出来,实际上我是用了两个ListView来显示的,这里和主题关系不大,就不详细讲了。然后我们创建完ListView后呢,肯定会设置对应的adapter。
2、setAdapter
if ((mEnableAppInfoList.size() > 0)
|| (mDisableAppInfoList.size() > 0)) {
mEnableTitle.setVisibility(View.VISIBLE);
mDisableTitle.setVisibility(View.VISIBLE);
}
mEnableAppListAdapter = new ListViewAdapter(mContext, mEnableAppInfoList, mMsgHandler);
mEnableAppListView.setAdapter(mEnableAppListAdapter);
mUtil.setListViewHeightBasedOnChildren(mEnableAppListView);
mDisableAppListAdapter = new ListViewAdapter(mContext, mDisableAppInfoList, mMsgHandler);
mDisableAppListView.setAdapter(mDisableAppListAdapter);
mUtil.setListViewHeightBasedOnChildren(mDisableAppListView);
如果我们点击ListView后面的Button按钮,该按钮作用为取消在当前ListView显示,那么我们需要重新将ListView数据做调整,并调用notifyDataSetChanged()。
3、updateListView
private Handler mMsgHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case MSG_UPDATE_LIST:
{
Bundle bundle = msg.getData();
// get the list item info
String pkgName = bundle.getString("pkgname");
int newState = bundle.getInt("newstate");
// update the enable ListView and disable ListView
DisplayAppInfo info = mDisplayData.get(pkgName);
if (newState == PmEnableStateSetting.BUTTON_STATE_ENABLE) {
mDisableAppInfoList.remove(info);
......
info.setApplicationButtonText(getString(R.string.disable));
mEnableAppInfoList.add(info);
} else {
mEnableAppInfoList.remove(info);
......
info.setApplicationButtonText(getString(R.string.enable));
mDisableAppInfoList.add(info);
}
mEnableAppListAdapter.notifyDataSetChanged();
mUtil.setListViewHeightBasedOnChildren(mEnableAppListView);
mDisableAppListAdapter.notifyDataSetChanged();
mUtil.setListViewHeightBasedOnChildren(mDisableAppListView);
}
break;
default:
break;
}
}
};
这样ListView列表就重新刷新显示,如何调整ListView的布局参数请参考前一篇博客《
ScrollView和ListView冲突问题解决》,敬请关注,谢谢!