显示来自非 activity class 的 toast

Displaying toast from a non activity class

我想显示来自非activity class 的 toast,这是我的 RecyclerView 适配器。

我该怎么做才能实现这一目标?

我想在 onLoadingStateChanged() switch 语句中设置 toasts。

我尝试了一些旧代码,但它们似乎不起作用。

我不希望 RecylerView 适配器位于 MainActivity

我的适配器Activity:

public class TalesAdapter extends FirestorePagingAdapter<TalesDetails, TalesAdapter.TalesViewHolder> {

    public TalesAdapter(@NonNull FirestorePagingOptions<TalesDetails> options) {
        super(options);
    }



    @Override
    protected void onBindViewHolder(@NonNull TalesViewHolder holder, int position, @NonNull TalesDetails model) {
        holder.bind(model);

    }

    @NonNull
    @Override
    public TalesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.talesrecyclerview, parent, false);
        return new TalesViewHolder(view);
    }
   

    @Override

    protected void onLoadingStateChanged(@NonNull LoadingState state) {
        switch (state) {
            case LOADING_INITIAL:
            case LOADING_MORE:
             //toast here
                break;

            case LOADED:
             //toast here
            case FINISHED:
             //toast here
                break;

            case ERROR:
             //toast here

                break;
        }

    }

    public class TalesViewHolder extends RecyclerView.ViewHolder  {

        private TextView Title;
        private TextView Matter;
        private TextView Name;

        public TalesViewHolder(View itemView ) {
            super(itemView);
            Name = itemView.findViewById(R.id.tvName);
            Title = itemView.findViewById(R.id.tvTitle);
            Matter = itemView.findViewById(R.id.tvMatter);
        }

        public void bind(TalesDetails tales){
            Name.setText(tales.name);
            Title.setText(tales.title);
            Matter.setText(tales.matter);

        }
    }
}

你有两种可能:

1 - 创建 Context 变量

private Context context;

public TalesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.talesrecyclerview, parent, false);
        return new TalesViewHolder(view);
    }

2 - 使用 implementation 'com.blankj:utilcodex:1.29.0'

ToastUtils.showShort("YOUR TEXT HERE");

声明 YourActivity 的变量类型 class 并在构造 TalesAdapter

时传递 activity 引用
public class TalesAdapter extends FirestorePagingAdapter<TalesDetails, TalesAdapter.TalesViewHolder> {
Context mContext = null;

    public TalesAdapter(@NonNull FirestorePagingOptions<TalesDetails> options, Context mContext) {
        this.mContext = mContext;
        super(options);
    }
@Override

    protected void onLoadingStateChanged(@NonNull LoadingState state) {
        switch (state) {
            case LOADING_INITIAL:
            case LOADING_MORE:
             Toast.makeText(mContext, "your message", Toast.LENGTH_SHORT).show()
                break;

            case LOADED:
             //toast here
            case FINISHED:
             //toast here
                break;

            case ERROR:
             //toast here

                break;
        }

    }



}

您可以在适配器中创建构造函数 喜欢:

Context mContext;

public TalesAdapter(@NonNull FirestorePagingOptions<TalesDetails> options, Context mContext) {
        this.mContext = mContext;
        super(options);
    }



@Override   
    protected void onLoadingStateChanged(@NonNull LoadingState state) {
        switch (state) {
            case LOADING_INITIAL:
            case LOADING_MORE:
             Toast.makeText(mContext, "Taost", Toast.LENGTH_SHORT).show();
                break;
            case LOADED:
             Toast.makeText(mContext, "Taost", Toast.LENGTH_SHORT).show();
              break;
            case FINISHED:
             Toast.makeText(mContext, "Taost", Toast.LENGTH_SHORT).show();
                break;
            case ERROR:
             Toast.makeText(mContext, "Taost", Toast.LENGTH_SHORT).show();
                break;
        }

    }

并且在您的 Activity 中,无论是 MainActivity.java 还是 RecyclerView 或 ListView 中的任何此适配器

Context mContext = this;
adapter = new TalesAdapter(mContext);
mRecycler.setAdapter(adapter)