为什么我的RecyclerView仅包含1个项目?

时间:2018-07-04 14:23:32

标签: android android-recyclerview android-adapter recyclerview-layout

我正在创建天气预报应用程序。为此,我想使用RecyclerView来显示项目。问题是我的RecyclerView仅显示一项而不是全部。我正在使用OpenWeatherMap API预测。

这是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/weather_background2"
android:orientation="vertical"
android:padding="5dp">

<TextView
    android:id="@+id/txt_close_weather"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_gravity="end"
    android:layout_marginRight="2dp"
    android:layout_marginTop="2dp"
    android:background="@drawable/circle"
    android:gravity="center"
    android:text="X"
    android:textColor="@color/blackTransparent"
    android:textStyle="bold" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="8"
    android:orientation="vertical">

    <TextView
        android:id="@+id/city_country_weather"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="0dp"
        android:text="KOTA"
        android:textColor="@color/blackTransparent"
        android:textSize="24dp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="2dp"
        android:text="Today"
        android:textColor="@color/blackTransparent"
        android:textSize="16dp" />

    <TextView
        android:id="@+id/current_date_weather"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="4dp"
        android:textColor="@color/blackTransparent"
        android:textSize="16dp" />

    <ImageView
        android:id="@+id/weather_icon"
        android:layout_width="160dp"
        android:layout_height="160dp"
        android:layout_gravity="center|center_horizontal"
        android:layout_marginTop="8dp"
        android:contentDescription="@string/app_name"
        android:src="@drawable/img_02d" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="2dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:weightSum="3">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hummidity"
                android:textColor="@color/blackTransparent"
                android:textSize="14dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/wind_result"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:text="@string/app_name"
                android:textColor="@color/blackTransparent"
                android:textSize="14dp" />

            <TextView
                android:id="@+id/temperature_result"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:text="@string/app_name"
                android:textColor="@color/blackTransparent"
                android:textSize="16dp" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:gravity="center_horizontal"
    android:background="@color/whiteTr"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2.5"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/weather_daily_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal"
        android:scrollbars="none" />
</LinearLayout>
</LinearLayout>

这是我的活动代码:

public class DialogWeather extends android.app.DialogFragment {
public static final int WIDTH = 60;
public static final int HEIGHT = 80;
public static final String DATE_FORMAT_WITHOUT_TIME = "dd/MM";
private static final String ICON_PREFIX = "img_";
private static final String API_KEY = "2fc56d498a3b4d87ba298c232e65e6b0";
private static final String UNITS = "metric";
final String q = "Jakarta";
PopupWindow myPopupWindow;
Dialog myDialog;
DialogWeather dialogWeather;
private TextView txtCloseWeather, txtCityCountry,
        txtCurrentDate, tvDescription, tvWindResult, tvTemperatureResult;
private ImageView weatherIcon;
private RecyclerView mRecyclerViewWeather;
DialogWeatherAdapter dialogWeatherAdapter;
private ArrayList<Example2> weathers = new ArrayList<>();
Example2 example2List;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle 
savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_dialog_weather, 
container, false);
    getDialog().getWindow().setBackgroundDrawable(new 
ColorDrawable(Color.TRANSPARENT));

    tvDescription = rootView.findViewById(R.id.tv_description);
    tvWindResult = rootView.findViewById(R.id.wind_result);
    tvTemperatureResult = rootView.findViewById(R.id.temperature_result);
    weatherIcon = rootView.findViewById(R.id.weather_icon);
    txtCityCountry = rootView.findViewById(R.id.city_country_weather);
    txtCityCountry.setTypeface(Typeface.DEFAULT_BOLD);
    txtCurrentDate = rootView.findViewById(R.id.current_date_weather);
    txtCloseWeather = rootView.findViewById(R.id.txt_close_weather);
    txtCloseWeather.setTypeface(Typeface.DEFAULT_BOLD);
    Fragment fragment = this;
    txtCloseWeather.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "Button Close", 
Toast.LENGTH_LONG).show(); 
getActivity().getFragmentManager().beginTransaction()
.remove(fragment).commit();
        }
    });
    mRecyclerViewWeather = rootView.findViewById(R.id.weather_daily_list);
    LinearLayoutManager layoutManager = new 
 LinearLayoutManager(getApplicationContext());
    layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    mRecyclerViewWeather.setLayoutManager(layoutManager);
    mRecyclerViewWeather.setHasFixedSize(true);

    dialogWeatherAdapter = new DialogWeatherAdapter(weathers);
    mRecyclerViewWeather.setAdapter(dialogWeatherAdapter);

    loadWeathers(q, API_KEY, UNITS);

    return rootView;
}

private void loadWeathers(String q, String apiKey, String units) {
    ApiServicesWeather weather = InitRetrofitWeather.getServicesWeather();
    Call<Example2> exampleCall = weather.getDetailWeather(q, apiKey, units);
    exampleCall.enqueue(new Callback<Example2>() {
        @Override
        public void onResponse(Call<Example2> call, Response<Example2> 
response) {
            if (response.isSuccessful()) {
                Toast.makeText(getApplicationContext(), "Success", 
Toast.LENGTH_LONG).show();
                example2List = response.body();
                weathers.addAll(Collections.singleton(example2List));
                loadSetView();
                dialogWeatherAdapter.updateList(weathers);
                dialogWeatherAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onFailure(Call<Example2> call, Throwable t) {
            Toast.makeText(getApplicationContext(), "Gagal " + 
 t.getMessage(), Toast.LENGTH_LONG).show();
        }
    });
}

 private void loadSetView() {
    txtCityCountry.setText(weathers.get(getId()).getCity().getName());    
 txtCurrentDate.setText(Util.formatDate(weathers
.get(getId()).getList().get(getId()).getDtTxt(), "EEE MMM dd, yyyy"));
 weatherIcon.setImageResource(Util 
.getDrawableResourceIdByName(getApplicationContext(),ICON_PREFIX + weathers

.get(getId()).getList().get(getId()).getWeather().get(getId()).getIcon()));


tvDescription.setText(weathers.get(getId()).getList().get(getId())
 .getWeather().get(getId()).getDescription());
    tvWindResult.setText("Wind : " + 
 weathers.get(getId()).getList().get(getId()).getWind().getSpeed() + " m/s");
    double mTemperature = (double) 
 weathers.get(getId()).getList().get(getId()).getMain().getTemp();




 tvTemperatureResult.setText(String.valueOf(weathers
.get(getId()).getList().get(ge 
 tId()).getMain().getTemp()) + " °C");
}

@Override
public void onResume() {
    super.onResume();
    ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
    params.width = 700;
    params.height = 1100;
    getDialog().getWindow().setAttributes((WindowManager.LayoutParams) 
params);
}

此“我的适配器”:

       public class DialogWeatherAdapter extends 
    RecyclerView.Adapter<DialogWeatherAdapter.DialogViewHolder> {
    ArrayList<Example2> data;
    Context context;
    Activity c;

    public DialogWeatherAdapter(ArrayList<Example2> data, Context context, 
  Activity c) {
        this.data = data;
        this.context = context;
        this.c = c;
    }

    public DialogWeatherAdapter(ArrayList<Example2> weathers) {
        this.data = weathers;
    }

    @Override
    public DialogViewHolder onCreateViewHolder(ViewGroup parent, int 
 viewType) {
        LayoutInflater layoutInflater = (LayoutInflater) 
 parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.item_weathers, parent, 
 false);
        return new DialogViewHolder(view);
    }

    @Override
    public void onBindViewHolder(DialogViewHolder holder, int position) {
        if (holder instanceof DialogViewHolder) {
            final DialogViewHolder viewHolder = (DialogViewHolder) holder;
            if (data != null) {
                for (int i = 0; i < data.size(); i++) {
viewHolder.tvCurrentDateItem.setText(Util.formatDate(data
 .get(position).getList() 
 .get(position).getDtTxt(), "dd/MM"));
                    viewHolder.ivIconItem.setImageResource(Util
  .getDrawableResourceIdByName(getApplicationContext(), ICON_PREFIX + data

 .get(getId()).getList().get(getId()).getWeather().get(getId()).getIcon()));
                    viewHolder.tvDescriptionItem.setText(data.get(position)
 .getList().get(position).getWeather().get(position).getDescription());
viewHolder.tvTemperatureItem.setText(String.valueOf(weathers
 .get(getId()).getList 
 ().get(getId()).getMain().getTemp()) + " °C");
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void updateList(ArrayList<Example2> weathers) {
        this.data = weathers;
    }

    public class DialogViewHolder extends RecyclerView.ViewHolder {
        TextView tvCurrentDateItem, tvDescriptionItem, tvTemperatureItem;
        ImageView ivIconItem;

        public DialogViewHolder(View itemView) {
            super(itemView);
            tvCurrentDateItem = itemView.findViewById(R.id.current_date_weather_item);
            tvDescriptionItem = itemView.findViewById(R.id.description_item);
            tvTemperatureItem = itemView.findViewById(R.id.temperature_item);
            ivIconItem = itemView.findViewById(R.id.icon_weather_item);
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

原因是-

项目文件(R.layout.item_weathers)具有高度匹配的父级。如图所示将其更改为wrap_content-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

它将显示所有项目。