《第一行代码》看过已经有一段时间了,但是最后的酷欧天气实战却一直没有尝试,其实全书的精华都在这个实战项目里了,但是因为版本的更迭,虽然是按照书上的代码一步一步尝试,却也遇到了一些问题。
我把代码放在Github上了,可以参考一下。
https://github.com/Yx-u/CoolWeather
Android官方文档中写到:
This class was deprecated in API level 26. ProgressDialog is a modal
dialog, which prevents the user from interacting with the app. Instead
of using this class, you should use a progress indicator like
ProgressBar, which can be embedded in your app’s UI. Alternatively,
you can use a notification to inform the user of the task’s progress.
该类在API级别26中已被弃用。ProgressDialog是一个模式对话框,它阻止用户与应用程序交互。不要使用这个类,您应该使用ProgressBar这样的进度指示器,它可以嵌入到应用程序的UI中。或者,可以使用通知通知用户任务的进度。
ProgressDialog会禁止用户与app进行交互,所以被弃用了,那么我们如何替代呢?
替代方法:
ProgressBar + Dialog
ProgressBar的出现和消失
progressBar.setVisibility(View.VISIBLE); //To show ProgressBar
progressBar.setVisibility(View.GONE); // To Hide ProgressBar
当然也可以直接把ProgressDialog相关的代码删掉,不影响使用。
在进行遍历省市县三级列表的时候,网络请求一直失败,无法显示数据,虽然知道问题的原因出在网络上,但是却不知道如何修正。
https://blog.csdn.net/kikii233/article/details/103830339
这篇文章给出了解决方法:
新建res/xml/network_security_config.xml
内容:
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
network-security-config>
在AndroidManifest.xml的application中添加android:networkSecurityConfig="@xml/network_security_config"
现在重新运行就可以了
官方文档
虽然显示被弃用,但是如果继续使用还是没有影响。。。
在手动更新天气界面,使用下拉刷新功能时,书上使用的是SwipeRefreshLayout 控件,但是使用时一直标红,你会发现support.v4这个包已经没有了
最新版应该使用:
androidx.drawerlayout.widget.DrawerLayout
androidx.swiperefreshlayout.widget.SwipeRefreshLayout
在使用这个控件前需要导入依赖:
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
这个问题和上一个问题一样,是support.v4包的原因,因为这个包已经失效了
换用androidx.drawerlayout.widget.DrawerLayout
就可以了,这个不需要添加依赖
完整的activity_weather.xml
代码
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
<ImageView
android:id="@+id/bing_pic_img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/weather_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:orientation="vertical">
<include layout="@layout/title" />
<include layout="@layout/now" />
<include layout="@layout/forecast" />
<include layout="@layout/aqi" />
<include layout="@layout/suggestion" />
LinearLayout>
ScrollView>
androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<fragment
android:id="@+id/choose_area_fragment"
android:name="com.example.coolweather.ChooseAreaFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start" />
androidx.drawerlayout.widget.DrawerLayout>
FrameLayout>
书中激活更新服务时,代码是放在if- else语句中的,但是我并没有找到这个语句。从github下载了源码之后发现源码里面并没有这个if-else语句,可能是出现了小错误,直接把激活代码放在后面就可以了。
showWeatherInfo() 方法的完整代码
/**
* 处理Weather实体类中的数据并展示到对应的控件中
*/
public void showWeatherInfo(Weather weather) {
String cityName = weather.basic.cityName;
String updateTime = weather.basic.update.updateTime.split(" ")[1];
String degree = weather.now.temperature + "℃";
String weatherInfo = weather.now.more.info;
titleCity.setText(cityName);
titleUpdateTime.setText(updateTime);
degreeText.setText(degree);
weatherInfoText.setText(weatherInfo);
forecastLayout.removeAllViews();//刷新数据
//遍历Weather信息中forecast
for (Forecast forecast : weather.forecastList) {
View view = LayoutInflater.from(this).inflate(R.layout.forecast_item, forecastLayout, false);
TextView dataText = (TextView) view.findViewById(R.id.data_text);
TextView infoText = (TextView) view.findViewById(R.id.info_text);
TextView maxText = (TextView) view.findViewById(R.id.max_text);
TextView minText = (TextView) view.findViewById(R.id.min_text);
dataText.setText(forecast.date);
infoText.setText(forecast.more.info);
maxText.setText(forecast.temperature.max);
minText.setText(forecast.temperature.min);
forecastLayout.addView(view);
}
if (weather.aqi != null) {
aqiText.setText(weather.aqi.city.aqi);
pm25Text.setText(weather.aqi.city.pm25);
}
String comfort = "舒适度:" + weather.suggestion.comfort.info;
String cashWash = "洗车指数:" + weather.suggestion.comfort.info;
String sport = "运动建议:" + weather.suggestion.sport.info;
comfortText.setText(comfort);
carWashText.setText(cashWash);
sportText.setText(sport);
weatherLayout.setVisibility(View.VISIBLE);
Intent intent = new Intent(this, AutoUpdateService.class);
startService(intent);
}
虽然书上的代码都敲完了,但是我感觉并不是很好,选择城市的时候会有延迟,如果可以我会继续完善,不得不说,做一个项目是真的难