android 添加ListView到windowmanager,不能删除最后一项
android add ListView to windowmanager, can not remove the last item
我想在窗口管理器中显示列表视图。
通过将列表视图添加到窗口管理器。我在屏幕上有视图覆盖,然后在添加、编辑、删除项目时我将只使用适配器和 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;
}
然后,当 Adapter size = 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()方法。但是这个项目仍然显示在这里:(。
这里你每次都在创建新的列表视图,
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();
}
改为:
if (messageNumber == 0) {
lv = new ListView(context);
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();
}
表示创建一个列表视图并将项目添加到列表视图,如果你想删除列表视图使用你的代码。像这样:
private void destroynotification(Context context) {
if (lv != null) {
((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
.removeView(lv);
Logger.error("Removed View, mess number = " + messageNumber);
}
}
结论:
您正在创建一个列表视图并将其添加到 window 管理器。但是您要从 window 管理器中删除不同的视图,这就是问题所在。
我想在窗口管理器中显示列表视图。
通过将列表视图添加到窗口管理器。我在屏幕上有视图覆盖,然后在添加、编辑、删除项目时我将只使用适配器和 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;
}
然后,当 Adapter size = 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()方法。但是这个项目仍然显示在这里:(。
这里你每次都在创建新的列表视图,
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();
}
改为:
if (messageNumber == 0) {
lv = new ListView(context);
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();
}
表示创建一个列表视图并将项目添加到列表视图,如果你想删除列表视图使用你的代码。像这样:
private void destroynotification(Context context) {
if (lv != null) {
((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
.removeView(lv);
Logger.error("Removed View, mess number = " + messageNumber);
}
}
结论: 您正在创建一个列表视图并将其添加到 window 管理器。但是您要从 window 管理器中删除不同的视图,这就是问题所在。