【问题标题】:Showing a custom Toast positioned into ListView Item position显示定位到 ListView 项目位置的自定义 Toast
【发布时间】: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


【解决方案1】:

您可以使用 View.getLocationOnScreen() 和/或 getLocationInWindow() 来获取屏幕上视图的绝对坐标,但我认为 Toast 不适合放在那里,Toast 是系统级别的通知,而不是应用程序级别,因此尝试将其强制到您的应用程序中的位置会有点牵强。我认为某种自定义视图会更好地为您服务。

【讨论】:

  • 好的。你能帮我实现个性化的愿景吗?
猜你喜欢
  • 1970-01-01
  • 2023-02-15
  • 2015-01-10
  • 1970-01-01
  • 2018-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多