显示放置在ListView Item位置的自定义Toast

时间:2019-06-29 01:34:44

标签: android listview adapter 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 变量仅获取项目索引,而不获取屏幕位置。有人可以帮助我在每个listview项的中心显示自定义吐司吗?

1 个答案:

答案 0 :(得分:0)

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