我想在windowmanager中顯示listview。android將ListView添加到windowmanager,無法刪除最後一項
通過將listview添加到windowmanager。我在屏幕上疊加視圖,然後在添加,編輯和刪除項目時,我只能使用適配器和notifydatasetchanged()方法。
但是我得到的問題是,當刪除最後一項時,我看到刪除成功的日誌,但是這個項目仍然顯示在桌面上,儘管我從windowmanager中刪除了Listview,我不知道它爲什麼是這樣?
這裏是我的代碼:
添加列表視圖:
private void addNotificationView(final Context context) {
if (windowManager == null)
windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
// NotificationView[messageNumber] =
// LayoutInflater.from(context).inflate(
// R.layout.fr_alert, null);
lv = new ListView(context);
if (messageNumber == 0) {
lv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
lv.requestLayout();
lv.setBackgroundColor(Color.BLACK);
data.add(messageNumber + " <(^OO^)>");
adapter = new NotifyView(context, data);
lv.setAdapter(adapter);
} else {
data.add(messageNumber + " <(^OO^)>");
adapter.notifyDataSetChanged();
}
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSPARENT);
params.gravity = Gravity.TOP;
// | Gravity.FILL_HORIZONTAL;
params.x = 0;
params.y = 0;
// Logger.error("y asix: " + (messageNumber * notifyHeight));
//
((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
.addView(lv, params);
messageNumber++;
Logger.error("Added View");
}
適配器:
@Override
public android.view.View getView(final int pos,
android.view.View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null)
convertView = LayoutInflater.from(mContext).inflate(
R.layout.fr_alert, parent, false);
LinearLayout lnl = (LinearLayout) convertView
.findViewById(R.id.bgTextContent);
lnl.setBackgroundColor(Color.parseColor("#000000"));
lnl.setAlpha((float) 0.9);
TextView tv = (TextView) convertView.findViewById(R.id.tvContent);
tv.setText("<(^OO^)> " + data.get(pos));
tv.setTextColor(Color.WHITE);
TextView tvDelete = (TextView) convertView
.findViewById(R.id.tvDelete);
tvDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (adapter.getCount() != 1) {
data.remove(pos);
adapter.notifyDataSetChanged();
} else {
// adapter = null;
messageNumber = 0;
destroynotification(mContext);
}
}
});
return convertView;
}
然後當適配器尺寸= 0我刪除列表視圖,但它不是迴環太: (
private void destroynotification(Context context) {
if (lv != null) {
((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
.removeView(lv);
Logger.error("Removed View, mess number = " + messageNumber);
}
}
請看圖像,我點擊刪除最後一項,日誌顯示destroyNotication()方法被調用。但這個項目仍然顯示在這裏:(
當你添加列表視圖到windowmanager? – 2015-01-21 06:50:50
第一次(count = 0)我創建listview並添加到windowmanager。在我只將數據添加到適配器並調用Notifydatasetchanged()方法後。請參閱我的更新代碼。 – kemdo 2015-01-21 06:57:19