014/Android_Weather

/Weather/src/main/java/com/example/weather/weather/weather/WeatherFragment.java
package com.example.weather.weather;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.pedant.SweetAlert.SweetAlertDialog;
import cn.pedant.SweetAlert.SweetAlertDialog.SDListener;
import com.example.weather.R;
import com.example.weather.weather.weather.adapter.CityAdapter;
import com.example.weather.weather.weather.adapter.WeatherAdapter;
import com.example.weather.weather.weather.model.City;
import com.example.weather.weather.weather.model.Weather;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.XmlUtil;

public class WeatherFragment extends Fragment {
private static final String TAG = "WeatherFragment";
private static final String URL = "http://wthrcdn.etouch.cn/weather_mini?city=%E6%B7%B1%E5%9C%B3";
private static final String KEY = "Weather";
private WeatherAdapter weatherAdapter;
private CityAdapter cityAdapter;
private List<Weather> weatherList;
private List<City> cityList;
private City city;
private LinearLayout linearLayout;
private SweetAlertDialog sweetAlertDialog;
private Unbinder unbinder;

@BindView(R.id.recyclerview_city)
RecyclerView recyclerViewCity;
@BindView(R.id.recyclerview_weather)
RecyclerView recyclerViewWeather;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_weather, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
unbinder = ButterKnife.bind(this, view);
init();
loadCity();
loadWeather();
}

private void init() {
//初始化列表
linearLayout = new LinearLayout(getContext());
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
linearLayout.setBackgroundColor(Color.parseColor("#f0f0f0"));
linearLayout.setOrientation(LinearLayout.VERTICAL);

//设置城市列表
cityAdapter = new CityAdapter(getContext(), cityList);
recyclerViewCity.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerViewCity.setAdapter(cityAdapter);

//设置天气列表
weatherAdapter = new WeatherAdapter(getContext(), weatherList);
recyclerViewWeather.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerViewWeather.setAdapter(weatherAdapter);

//设置点击事件
recyclerViewCity.addOnItemTouchListener(new CityOnItemClickListener(linearLayout, recyclerViewCity, this));
recyclerViewWeather.addOnItemTouchListener(new WeatherOnItemClickListener(linearLayout, recyclerViewWeather, this));
}

private void loadCity() {
//读取本地数据
SharedPreferences sharedPreferences = getContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
String cityJson = sharedPreferences.getString("city", "");
Gson gson = new Gson();
if (!cityJson.isEmpty()) {
city = gson.fromJson(cityJson, City.class);
if (city != null) {
cityList = new ArrayList<>();
cityList.add(city);
}
}
//读取网络数据
HttpUtils httpUtils = new HttpUtils();
httpUtils.configDefaultRequestParams("key", "3736759a67444d8683a042b94721f78e");
RequestParams requestParams = new RequestParams();
requestParams.setUrl(URL);
httpUtils.download(requestParams, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException e, String s) {

}

@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
String jsonString = responseInfo.result;
Weather weather = new Gson().fromJson(jsonString, new TypeToken<Weather>() {}.getType());
if (weather != null) {
String cityName = weather.city.name;
city = new City(cityName, weather.city.id);
cityList = new ArrayList<>();
cityList.add(city);
saveCityData(city);
//加载城市列表
loadCityData();
}
}
});
}

private void loadWeather() {
HttpUtils httpUtils = new HttpUtils();
httpUtils.configDefaultRequestParams("key", "3736759a67444d8683a042b94721f78e");
RequestParams requestParams = new RequestParams();
requestParams.setUrl(URL);
httpUtils.download(requestParams, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException e, String s) {
if (sweetAlertDialog != null) {
sweetAlertDialog.cancel();
}
Toast.makeText(getContext(), "网络请求失败", Toast.LENGTH_SHORT).show();
}

@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
String jsonString = responseInfo.result;
Weather weather = new Gson().fromJson(jsonString, new Type