最终版

This commit is contained in:
fl_shy 2024-06-19 21:04:11 +08:00
parent d8e80c80d3
commit 09eed4f584
17 changed files with 172 additions and 42 deletions

View File

@ -1,5 +1,6 @@
package com.hnucm.weather;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
@ -25,48 +26,70 @@ import retrofit2.Response;
public class WeatherFragment extends Fragment {
//https://ali-weather.showapi.com/area-to-weather-date
RecyclerView recyclerView;
RecyclerView recyclerView,recyclerView1;
MyAdapter myAdapter;
MyAdapter1 myAdapter1;
List<Weather.ShowapiResBodyDTO.DayListDTO> weatherList = new ArrayList<Weather.ShowapiResBodyDTO.DayListDTO>();
List<Air> airList = new ArrayList<>();
List<Hour.ShowapiResBodyDTO.HourListDTO> hourList = new ArrayList<Hour.ShowapiResBodyDTO.HourListDTO>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_weather, container, false);
recyclerView = view.findViewById(R.id.recycleview);
recyclerView1 = view.findViewById(R.id.recycleview1);
myAdapter1 = new MyAdapter1();
myAdapter = new MyAdapter();
recyclerView1.setAdapter(myAdapter1);
recyclerView.setAdapter(myAdapter);
recyclerView1.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
view.findViewById(R.id.imageView52).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),TourismActivity.class);
startActivity(intent);
}
});
Call<Weather> weatherCall = RetrofitUtils.getRetrofit("https://ali-weather.showapi.com/")
.create(Api.class).getWeatherlist("APPCODE e1954955b4ca410c82c39b46677b95bd","长沙");
Call<Air> airCall = RetrofitUtils.getRetrofit("https://ali-weather.showapi.com/")
.create(Api.class).getAirlist("APPCODE e1954955b4ca410c82c39b46677b95bd","长沙");
// Call<Hour> hourCall = RetrofitUtils.getRetrofit("https://ali-weather.showapi.com/")
// .create(Api.class).getHourlist("APPCODE 7675f2257b9b4aea938392b8fba00fdc","长沙");
// Call<Weather7> weather7Call = RetrofitUtils.getRetrofit("https://ali-weather.showapi.com/")
// .create(Api.class).getWearther7list("APPCODE 7675f2257b9b4aea938392b8fba00fdc","长沙");
// weather7Call.enqueue(new Callback<Weather7>() {
// @Override
// public void onResponse(Call<Weather7> call, Response<Weather7> response) {
//
// }
//
// @Override
// public void onFailure(Call<Weather7> call, Throwable t) {
//
// }
// });
// hourCall.enqueue(new Callback<Hour>() {
// @Override
// public void onResponse(Call<Hour> call, Response<Hour> response) {
//
// }
//
// @Override
// public void onFailure(Call<Hour> call, Throwable t) {
//
// }
// });
Call<Weather7> weather7Call = RetrofitUtils.getRetrofit("https://ali-weather.showapi.com/")
.create(Api.class).getWearther7list("APPCODE e1954955b4ca410c82c39b46677b95bd","长沙");
Call<Hour> hourCall = RetrofitUtils.getRetrofit("https://ali-weather.showapi.com/")
.create(Api.class).getHourlist("APPCODE e1954955b4ca410c82c39b46677b95bd","长沙");
hourCall.enqueue(new Callback<Hour>() {
@Override
public void onResponse(Call<Hour> call, Response<Hour> response) {
hourList.addAll(response.body().showapi_res_body.hourList);
myAdapter1.notifyDataSetChanged();
}
@Override
public void onFailure(Call<Hour> call, Throwable t) {
}
});
weather7Call.enqueue(new Callback<Weather7>() {
@Override
public void onResponse(Call<Weather7> call, Response<Weather7> response) {
ImageView imageView = view.findViewById(R.id.imageView50);
TextView textView1,textView2,textView3;
textView1 = view.findViewById(R.id.textView107);
textView2 = view.findViewById(R.id.textView108);
textView3 = view.findViewById(R.id.textView110);
textView1.setText(response.body().showapi_res_body.f1.day_weather);
textView2.setText(response.body().showapi_res_body.f1.night_air_temperature+"/"+response.body().showapi_res_body.f1.day_air_temperature);
Glide.with(getContext())
.load(response.body().showapi_res_body.f1.day_weather_pic)
.into(imageView);
}
@Override
public void onFailure(Call<Weather7> call, Throwable t) {
}
});
weatherCall.enqueue(new Callback<Weather>() {
@Override
public void onResponse(Call<Weather> call, Response<Weather> response) {
@ -112,6 +135,47 @@ public class WeatherFragment extends Fragment {
});
return view;
}
public class MyViewHolder1 extends RecyclerView.ViewHolder{
TextView time,weather,temperature;
//TextView day
//ImageView imageView;
public MyViewHolder1(@NonNull View itemView) {
super(itemView);
time = itemView.findViewById(R.id.textView105);
//day = itemView.findViewById(R.id.textView68);
weather = itemView.findViewById(R.id.textView106);
temperature = itemView.findViewById(R.id.textView109);
// imageView = itemView.findViewById(R.id.imageView);
}
}
public class MyAdapter1 extends RecyclerView.Adapter<MyViewHolder1>{
//加载item布局文件
@NonNull
@Override
public MyViewHolder1 onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(getContext())
.inflate(R.layout.weatherhour_item_layout, parent, false);
return new MyViewHolder1(view);
}
//设置item中控件的值
@Override
public void onBindViewHolder(@NonNull MyViewHolder1 holder, int position) {
String string = hourList.get(position).time;
string=string.substring(string.length()-4);
holder.time.setText(string.substring(0,2)+":"+string.substring(2));
//holder.day.setText(weatherList.get(position).daytime);
holder.weather.setText(hourList.get(position).weather);
// holder.imageView.setImageURI(Uri.parse("https://app1.showapi.com/weather/icon/night/01.png"));
holder.temperature.setText(hourList.get(position).temperature);
}
//列表显示的条数
@Override
public int getItemCount() {return hourList.size();}
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView daytime;
//TextView day;

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -180,7 +180,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/img_22" />
app:srcCompat="@drawable/img_55" />
<TextView
android:id="@+id/textView102"

View File

@ -180,7 +180,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/img_19" />
app:srcCompat="@drawable/img_53" />
<TextView
android:id="@+id/textView102"

View File

@ -180,7 +180,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/img_20" />
app:srcCompat="@drawable/img_52" />
<TextView
android:id="@+id/textView102"

View File

@ -181,7 +181,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/img_23" />
app:srcCompat="@drawable/img_51" />
<TextView
android:id="@+id/textView102"

View File

@ -180,7 +180,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/img_21" />
app:srcCompat="@drawable/img_56" />
<TextView
android:id="@+id/textView102"

View File

@ -180,7 +180,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/img_18" />
app:srcCompat="@drawable/img_58" />
<TextView
android:id="@+id/textView102"

View File

@ -114,6 +114,35 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView107"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="120dp"
android:text="TextView"
android:textStyle="bold"
android:textSize="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/imageView51" />
<ImageView
android:id="@+id/imageView50"
android:layout_width="48dp"
android:layout_height="55dp"
app:layout_constraintStart_toStartOf="@+id/textView107"
app:layout_constraintTop_toBottomOf="@+id/textView107"
tools:src="@tools:sample/avatars" />
<TextView
android:id="@+id/textView108"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="@+id/imageView50"
app:layout_constraintStart_toEndOf="@+id/imageView50"
app:layout_constraintTop_toTopOf="@+id/imageView50" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
@ -129,20 +158,20 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginTop="4dp"
android:text="24小时天气预报"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView65"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="128dp"
android:text="当前"
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleview1"
android:layout_width="match_parent"
android:layout_height="98dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView64" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="40dp">
<TextView
android:id="@+id/textView105"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView106"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="64dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView105"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView109"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="64dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView106"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>