最近做商城,有个收藏功能,需要弹出窗口。在网上找了类似的效果。方法不少,还是觉得用PopupWindow最省力效果也是最好的。下面介绍下PopupWindow。
PopupWindow就是弹出窗口的意思,类似windows下面的开始按钮。PopupWindow可以实现浮层效果,而且可以自定义显示位置,出现和退出时的动画。
首先自定义顶部栏这个根据自己的需求样式做,这里就不写了。直接上最主要代码:
private PopupWindow popupWindow;
private View view;
private ListView CollectBookView;//定义的列表显示数据
private void showWindow() {
// TODO Auto-generated method stub
if (popupWindow == null) {
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.book_collect, null);
popupWindow = new PopupWindow(view, 260, 421);//显示位置
}
// 加载数据
CollectBookView = (ListView) view.findViewById(R.id.this_book);
String columns[] = new String[] {
DataCenter.Collect.COLUMN_NAME_APPNAME,
DataCenter.Collect.COLUMN_NAME_APPID };
Cursor c = getContentResolver().query(DataCenter.Collect.CONTENT_URI,
columns, DataCenter.Collect.COLUMN_NAME_USERID+"="+GeneralUtil.getUid(getApplicationContext()), null, null);
List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
c.moveToFirst();
while (!c.isAfterLast()) {
mAppName = c.getString(c
.getColumnIndex(DataCenter.Collect.COLUMN_NAME_APPNAME));
HashMap<String, Object> item = new HashMap<String, Object>();
mAppId = c.getInt(c
.getColumnIndex(DataCenter.Collect.COLUMN_NAME_APPID));
item.put("id",mAppId);
item.put("name", mAppName);
data.add(item);
c.moveToNext();
}
c.close();
SimpleAdapter adapter = new SimpleAdapter(this, data,
R.layout.book_collect_item, new String[] { "name" },
new int[] { R.id.book_collect_item_text });
CollectBookView.setAdapter(adapter);
CollectBookView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
SimpleAdapter adapter = (SimpleAdapter) CollectBookView.getAdapter();
HashMap<String, Object> item = (HashMap<String, Object>) adapter.getItem(arg2);
Intent i = new Intent(getApplicationContext(),
DetailAppInfoActivity.class);
i.putExtra(DetailAppInfoActivity.EXTRA_KEY_APPID, item.get("id").toString());
popupWindow.dismiss();
startActivityForResult(i,DetailAppInfoActivity.REQUEST_SHOW_DETAILS);
finish();
}
});
//长按选项删除内容
CollectBookView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
SimpleAdapter adapter = (SimpleAdapter) CollectBookView.getAdapter();
final HashMap<String, Object> item = (HashMap<String, Object>) adapter.getItem(arg2);
new AlertDialog.Builder(BaseBottomTabActivity.this)
.setTitle("是否删除"+item.get("name").toString())
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
// TODO Auto-generated method stub
getContentResolver().delete(DataCenter.Collect.CONTENT_URI,
DataCenter.Collect.COLUMN_NAME_USERID+"=? and "+DataCenter.Collect.COLUMN_NAME_APPID+"=?",
new String[] {GeneralUtil.getUid(getApplicationContext()), item.get("id").toString()});
String columns[] = new String[] {
DataCenter.Collect.COLUMN_NAME_APPNAME,
DataCenter.Collect.COLUMN_NAME_APPID };
Cursor c = getContentResolver().query(DataCenter.Collect.CONTENT_URI,
columns, null, null, null);
List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
c.moveToFirst();
while (!c.isAfterLast()) {
mAppName = c.getString(c
.getColumnIndex(DataCenter.Collect.COLUMN_NAME_APPNAME));
HashMap<String, Object> item = new HashMap<String, Object>();
mAppId = c.getInt(c
.getColumnIndex(DataCenter.Collect.COLUMN_NAME_APPID));
item.put("id",mAppId);
item.put("name", mAppName);
data.add(item);
c.moveToNext();
}
c.close();
SimpleAdapter adapter = new SimpleAdapter(BaseBottomTabActivity.this, data,
R.layout.book_collect_item, new String[] { "name" },
new int[] { R.id.book_collect_item_text });
CollectBookView.setAdapter(adapter);
}
}).setNegativeButton("取消", null).show();
return false;
}
});
popupWindow.setFocusable(true);//获取焦点
popupWindow.setOutsideTouchable(true);//点击popupWindow以外的地方popupWindow消失
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.showAsDropDown(book_collect, 793, 0);
}
效果如图:(此图网络截取)
一般此种需求是全局性的,最好定义一个BaseActivity,让其他Activity能够继承使用。
已经测试,如有错误,敬请指出。