如何在 android 的另一个 RecyclerView 中显示一个 RecyclerView(从 API 获取数据)作为列表项?

How to show a RecyclerView (with data fetched from API) as a list item in another RecyclerView in android?

想要实现:一个聊天机器人,当用户输入 'weather'.[=20= 时,聊天机器人 returns 在该聊天项(在外部 RecyclerView 中)中作为 RecyclerView 进行天气预报]

我希望天气信息成为实际的聊天项目。

我有分别处理消息和天气预报信息的 MessageAdapter 和 WeatherAdapter,我很难处理 MessageAdapter 的 onBindViewHolder 中的 'returning weather forecast' 情况。

我尝试了很多不同的方法都无济于事: making a ConcatAdapter, , 制作一个从主要 activity 接收天气数据的片段,我在那里进行天气获取和 showing that fragment in adapter...

java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.os.Bundle.getSerializable(java.lang.String)' on a null object reference at com.demo.chatbot.WeatherFragment.onCreateView(WeatherFragment.java:25)

weatherList = (ArrayList<Weather>) getArguments().getSerializable("weather"); (WeatherFragment.java:25)

数据获取尚未开始,片段正在尝试加载数据...

主要问题是我必须在获取 API 后设置 weatherAdapter.setWeatherList(weatherList.getWeeklyWeather()); 并且这必须在我的主 activity 中完成并且 RecyclerViews 必须在主 activity 何时应在 MessageAdapter 中完成布局。

rvWeather = findViewById(R.id.rvWeather);
// rvWeather is supposed to be inside my item_chat_bot.xml
// but I have to add it to main activity for it to show
rvWeather.setAdapter(weatherAdapter);
// otherwise get No adapter attached; skipping layout

我不明白为什么这么复杂。在本机反应中,这只是使用 .map 或 for 循环的问题。我是否遗漏了一些明显的东西?

重点是让天气预报成为一条消息。使用两个 for 循环(一个在 WeatherViewHolder 中,一个在 onBindViewHolder 中)和 TextView 数组 in adapter 来处理天气布局。这是我的适配器代码:

public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
    Message message = messageArrayList.get(position);

    switch (message.getType()) {
        case "weather":
            ((WeatherViewHolder) holder).message.setText(message.getMessage());

            for (int i = 0; i < weatherList.size(); i++) {
                Weather weather = weatherList.get(i);
                Glide
                        .with(context)
                        .load(context.getResources().getIdentifier(
                                "icon_" + weather.getWeatherDetails().get(0).getIcon(),
                                "drawable", context.getPackageName()))
                        .into(((WeatherViewHolder) holder).mWeatherIcon[i]);

                Calendar calendar = new GregorianCalendar();
                calendar.setTime(new java.util.Date((long) weather.getDate() * 1000));
                ((WeatherViewHolder) holder).mDate[i].setText(weekdays[calendar.get(Calendar.DAY_OF_WEEK) % 7]);
                ((WeatherViewHolder) holder).mMaxTemp[i].setText((int) weather.getTemp().getMax() + "°C");
                ((WeatherViewHolder) holder).mMinTemp[i].setText((int) weather.getTemp().getMin() + "°C");
                ((WeatherViewHolder) holder).mWeatherCondition[i].setText(String.valueOf(weather.getWeatherDetails().get(0).getShortDescription()));
            }
            break;
    }
}

public class WeatherViewHolder extends RecyclerView.ViewHolder{
    private LinearLayout weatherLayout;
    private TextView[] mDate = new TextView[weatherList.size()];
    private TextView[] mMaxTemp = new TextView[weatherList.size()];
    private TextView[] mMinTemp = new TextView[weatherList.size()];
    private TextView[] mWeatherCondition = new TextView[weatherList.size()];
    private ImageView[] mWeatherIcon = new ImageView[weatherList.size()];
    TextView message;

    public WeatherViewHolder(View itemView) {
        super(itemView);
        message = itemView.findViewById(R.id.message);
        weatherLayout = itemView.findViewById(R.id.chat_bot_linear_layout);

        for (int i = 0; i < weatherList.size(); i++) {
            View view = View.inflate(context, R.layout.item_weather, null);
            mWeatherIcon[i] = view.findViewById(R.id.imageViewWeatherIcon);
            mDate[i] = view.findViewById(R.id.textViewDate);
            mMaxTemp[i] = view.findViewById(R.id.textViewMaxTemp);
            mMinTemp[i] = view.findViewById(R.id.textViewMinTemp);
            mWeatherCondition[i] = view.findViewById(R.id.textViewWeatherCondition);

            weatherLayout.addView(view);
        }
    }
}