如何将数据从适配器 class 发送到片段?

How to send data from a adapter class to a fragment?

我想将音频文件从适配器 class 发送到从片段中的 firebase 数据库检索的片段。

这是我的适配器代码 class。

recyclerViewAdapter.java

public class recyclerViewAdapter extends FirebaseRecyclerAdapter<model, recyclerViewAdapter.recyclerViewHolder> {

public recyclerViewAdapter(@NonNull FirebaseRecyclerOptions<model> options) {
    super(options);
}

@Override
protected void onBindViewHolder(@NonNull recyclerViewAdapter.recyclerViewHolder holder, int position, @NonNull model model) {

    holder.recycler_view_title.setText(model.getTitle());
    Glide.with(holder.recycler_view_image.getContext()).load(model.getImageUrl()).into(holder.recycler_view_image);

    holder.recycler_view_image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            AppCompatActivity appCompatActivity = (AppCompatActivity) v.getContext();
            appCompatActivity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new DescriptionFragment(model.getTitle(), model.getDescription(), model.getImageUrl(), model.getAudioUrl())).addToBackStack(null).commit();

        }
    });

}

@NonNull
@Override
public recyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row_design, parent, false);

    return new recyclerViewHolder(view);
}

public class recyclerViewHolder extends RecyclerView.ViewHolder {

    private ImageView recycler_view_image;
    private TextView recycler_view_title;

    public recyclerViewHolder(@NonNull View itemView) {
        super(itemView);

        recycler_view_image = itemView.findViewById(R.id.recycler_view_image);
        recycler_view_title = itemView.findViewById(R.id.recycler_view_title);

    }

}

}

要在 adapter class 到 fragment 之间发送数据,请使用 interface

在适配器内部创建接口 class

    OnDataSend dataSend;

    public interface OnDataSend {
        void onSend(String audioURL);
    }

    public void OnDataSend(OnDataSend dataSend){
        this.dataSend = dataSend;
    }
   

要发送数据,请将其放入适配器中 class

    //To send data call 
    dataSend.onSend(/*Pass you data*/);

接收片段中的数据

    recyclerViewAdapter recyclerViewAdapter = new recyclerViewAdapter(/*...*/);
    recyclerViewAdapter.OnDataSend(new recyclerViewAdapter.OnDataSend() {
        @Override
        public void onSend(String audioURL) {

          //data received...

        }
    });

详细解答

public class recyclerViewAdapter extends FirebaseRecyclerAdapter<model, recyclerViewAdapter.recyclerViewHolder> {

    OnDataSend dataSend;

    public recyclerViewAdapter(@NonNull FirebaseRecyclerOptions<model> options) {
        super(options);
    }

    public void OnDataSend(OnDataSend dataSend) {
        this.dataSend = dataSend;
    }

    @Override
    protected void onBindViewHolder(@NonNull recyclerViewAdapter.recyclerViewHolder holder, int position, @NonNull model model) {

        holder.recycler_view_title.setText(model.getTitle());
        Glide.with(holder.recycler_view_image.getContext()).load(model.getImageUrl()).into(holder.recycler_view_image);

        holder.recycler_view_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AppCompatActivity appCompatActivity = (AppCompatActivity) v.getContext();
                appCompatActivity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new DescriptionFragment(model.getTitle(), model.getDescription(), model.getImageUrl(), model.getAudioUrl())).addToBackStack(null).commit();

                
                //send data
                dataSend.onSend(model.getAudioUrl());
                
                
            }
        });

    }

    @NonNull
    @Override
    public recyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row_design, parent, false);

        return new recyclerViewHolder(view);
    }

    public interface OnDataSend {
        void onSend(String audioURL);
    }

    public class recyclerViewHolder extends RecyclerView.ViewHolder {

        private ImageView recycler_view_image;
        private TextView recycler_view_title;

        public recyclerViewHolder(@NonNull View itemView) {
            super(itemView);

            recycler_view_image = itemView.findViewById(R.id.recycler_view_image);
            recycler_view_title = itemView.findViewById(R.id.recycler_view_title);

        }

    }
}