四、手动更新天气和切换城市
1、手动更新天气
使用下拉刷新的方法
(1)修改activity_weather.xml
......
<!--下拉刷新-->
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--滚动显示-->
<ScrollView
.......
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
(2)修改WeatherActivity中代码
public class WeatherActivity extends AppCompatActivity {
public SwipeRefreshLayout swipeRefresh;
private String mWeatherId;//城市天气id
......
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......
swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);//获取实例
swipeRefresh.setColorSchemeResources(R.color.colorPrimary);//设置下拉进度条的颜色
......
if (weatherString != null){//有缓存时,直接解析天气数据
Weather weather = Utility.handleWeatherResponse(weatherString);
mWeatherId = weather.basic.weatherId;
showWeatherInfo(weather);
}else {
mWeatherId = getIntent().getStringExtra("weather_id");
weatherLayout.setVisibility(View.INVISIBLE);//无缓存,将ScrollView隐藏
requestWeather(mWeatherId);
}
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){//下拉进度条监听器
@Override
public void onRefresh() {
requestWeather(mWeatherId);//回调方法
}
});
......
/**
* 根据天气id请求城市天气信息
*/
public void requestWeather(final String weatherId){
......
f (weather != null && "ok".equals(weather.status)){
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(WeatherActivity.this).edit();
editor.putString("weather", responseText);
editor.apply();
mWeatherId = weather.basic.weatherId;
showWeatherInfo(weather);
}else {
Toast.makeText(WeatherActivity.this, "获取天气信息失败", Toast.LENGTH_SHORT).show();
}
swipeRefresh.setRefreshing(false);
}
});
......
2、切换城市
通过滑动菜单方式
(1)在头布局中加入切换城市按钮,修改title.xml中代码
android:layout_height="?attr/actionBarSize">
<Button
android:id="@+id/nav_button"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@drawable/ic_home"/>
......
(2)修改activity_weather.xml,加入滑动菜单功能(书中P415)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
......
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--下拉刷新-->
<android.support.v4.widget.SwipeRefreshLayout
......
</android.support.v4.widget.SwipeRefreshLayout>
<fragment
android:id="@+id/choose_area_fragment"
android:name="com.coolweather.android.ChooseAreaFragment"//遍历省市县数据的碎片
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
</FrameLayout>
(3)在WeatherActivity中加入滑动菜单的处理逻辑,修改WeatherActivity中代码
package com.coolweather.android;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.coolweather.android.gson.Forecast;
import com.coolweather.android.gson.Weather;
import com.coolweather.android.service.AutoUpdateService;
import com.coolweather.android.util.HttpUtil;
import com.coolweather.android.util.Utility;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class WeatherActivity extends AppCompatActivity {
public DrawerLayout drawerLayout;
public Button navButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navButton = (Button) findViewById(R.id.nav_button);
......
navButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
drawerLayout.openDrawer(GravityCompat.START);//打开滑动菜单(书中P417)
}
......
}
}
(4)修改ChooseAreaFragment中代码,处理切换城市后的逻辑,使用不同状态进行不同逻辑的处理
由于之前选中某个城市后是跳转到WeatherActivity的,现在我们本来就在WeatherActivity中,因此不需要跳转,只是请求新选择城市天气的信息就可以。
package com.coolweather.android;
import android.content.Intent;
import android.support.v4.app.Fragment;
//package com.coolweather.android;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.coolweather.android.R;
import com.coolweather.android.db.City;
import com.coolweather.android.db.County;
import com.coolweather.android.db.Province;
import com.coolweather.android.util.HttpUtil;
import com.coolweather.android.util.Utility;
import org.litepal.crud.DataSupport;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class ChooseAreaFragment extends Fragment {
......
@Override
public void onActivityCreated( Bundle savedInstanceState) {
......
}else if (currentLevel == LEVEL_COUNTY){//是县
String weatherId = countyList.get(position).getWeatherId();
if (getActivity() instanceof MainActivity) {//在滑动窗口界面,则不变,重新选择城市
Intent intent = new Intent(getActivity(), WeatherActivity.class);//启动WeatherActivity
intent.putExtra("weather_id", weatherId);//传值P50
startActivity(intent);
getActivity().finish();
}else if (getActivity() instanceof WeatherActivity){//在WeatherActivity活动中
WeatherActivity activity = (WeatherActivity) getActivity();
activity.drawerLayout.closeDrawers();//关闭滑动菜单
activity.swipeRefresh.setRefreshing(true);//下拉刷新进度条
activity.requestWeather(weatherId);
}
}
}
});
......
}
3、提交代码
git add .
git commit -m "新增切换城市和手动更新天气的功能"
git push origin master
4、效果图