【发布时间】:2023-03-30 13:57:01
【问题描述】:
我正在尝试将自定义 toast 完全显示到已删除的 ListView 项目位置。这是我能做的:
适配器:
private View.OnClickListener onDeleteListener(final int position, final ViewHolder holder) {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
showCustomToast(position, position);
}
};
}
public void showCustomToast(int positionX, int positionY) {
LayoutInflater inflater = LayoutInflater.from(context);
View layout = inflater.inflate(R.layout.toast_layout, null, false);
ImageView icon = (ImageView) layout.findViewById(R.id.toast_image);
TextView message = (TextView) layout.findViewById(R.id.toast_text);
LinearLayout fundoMensagem = (LinearLayout) layout.findViewById(R.id.toast_root);
message.setTextColor(Color.BLACK);
fundoMensagem.setBackgroundResource(R.color.error_colors);
message.setText("Item Deleted!");
Toast toast = new Toast(context);
toast.setDuration(toast.LENGTH_LONG);
toast.setView(layout);
toast.setGravity(positionX, positionX, positionX);
toast.show();
}
但这并不是我所需要的,因为 onDeleteListener 方法的 position 变量只获取项目索引而不是屏幕位置。有人可以帮我在每个列表视图项目的中心显示自定义吐司吗?
【问题讨论】:
-
为什么不使用 RecyclerView ?
-
你好@EdgarKhimich。怎么做?
-
使用 RecyclerView。它重用已经创建的视图,可以节省视图创建过程的内存。 android.jlelse.eu/…
-
使用 ListVIew 是一种不好的做法。阅读我在上面附加的链接
标签: android listview adapter toast