android:textColor=“@color/blue_one”
android:textSize=“12sp” />
<com.llw.mvplibrary.view.LineProgressbar
android:id=“@+id/progress_pm10”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“center”
app:progressbar_width=“@dimen/dp_80”
app:progressbar_height=“@dimen/dp_10”/>
<TextView
android:id=“@+id/tv_pm10”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:gravity=“center”
android:textColor=“@color/white”
android:textSize=“12sp” />
<LinearLayout
android:gravity=“center_vertical”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“12dp”>
<TextView
android:layout_width=“@dimen/dp_44”
android:layout_height=“wrap_content”
android:gravity=“center”
android:text=“PM2.5”
android:textColor=“@color/blue_one”
android:textSize=“12sp” />
<com.llw.mvplibrary.view.LineProgressbar
android:id=“@+id/progress_pm25”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“center”
app:progressbar_width=“@dimen/dp_80”
app:progressbar_height=“@dimen/dp_10”/>
<TextView
android:id=“@+id/tv_pm25”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:gravity=“center”
android:textColor=“@color/white”
android:textSize=“12sp” />
<LinearLayout
android:gravity=“center_vertical”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“12dp”>
<LinearLayout
android:layout_width=“@dimen/dp_44”
android:layout_height=“wrap_content”
android:gravity=“center”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“NO”
android:textColor=“@color/blue_one”
android:textSize=“12sp” />
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“2”
android:textColor=“@color/blue_one”
android:textSize=“8sp” />
<com.llw.mvplibrary.view.LineProgressbar
android:id=“@+id/progress_no2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“center”
app:progressbar_width=“@dimen/dp_80”
app:progressbar_height=“@dimen/dp_10”/>
<TextView
android:id=“@+id/tv_no2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:gravity=“center”
android:textColor=“@color/white”
android:textSize=“12sp” />
<LinearLayout
android:gravity=“center_vertical”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“12dp”>
<LinearLayout
android:layout_width=“@dimen/dp_44”
android:layout_height=“wrap_content”
android:gravity=“center”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“SO”
android:textColor=“@color/blue_one”
android:textSize=“12sp” />
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“2”
android:textColor=“@color/blue_one”
android:textSize=“8sp” />
<com.llw.mvplibrary.view.LineProgressbar
android:id=“@+id/progress_so2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“center”
app:progressbar_width=“@dimen/dp_80”
app:progressbar_height=“@dimen/dp_10”/>
<TextView
android:id=“@+id/tv_so2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:gravity=“center”
android:textColor=“@color/white”
android:textSize=“12sp” />
<LinearLayout
android:gravity=“center_vertical”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“12dp”>
<LinearLayout
android:layout_width=“@dimen/dp_44”
android:layout_height=“wrap_content”
android:gravity=“center”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“O”
android:textColor=“@color/blue_one”
android:textSize=“12sp” />
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“3”
android:textColor=“@color/blue_one”
android:textSize=“8sp” />
<com.llw.mvplibrary.view.LineProgressbar
android:id=“@+id/progress_o3”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“center”
app:progressbar_width=“@dimen/dp_80”
app:progressbar_height=“@dimen/dp_10”/>
<TextView
android:id=“@+id/tv_o3”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:gravity=“center”
android:textColor=“@color/white”
android:textSize=“12sp” />
<LinearLayout
android:gravity=“center_vertical”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“12dp”>
<TextView
android:layout_width=“@dimen/dp_44”
android:layout_height=“wrap_content”
android:gravity=“center”
android:text=“CO”
android:textColor=“@color/blue_one”
android:textSize=“12sp” />
<com.llw.mvplibrary.view.LineProgressbar
android:id=“@+id/progress_co”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“center”
app:progressbar_width=“@dimen/dp_80”
app:progressbar_height=“@dimen/dp_10”/>
<TextView
android:id=“@+id/tv_co”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:gravity=“center”
android:textColor=“@color/white”
android:textSize=“12sp” />
<TextView
android:layout_marginLeft=“@dimen/dp_12”
android:text=“监测站空气质量”
android:textColor=“@color/white”
android:textSize=“@dimen/sp_16”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”/>
<androidx.recyclerview.widget.RecyclerView
android:id=“@+id/rv_station”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:overScrollMode=“never” />
<TextView
android:layout_marginLeft=“@dimen/dp_12”
android:text=“未来5天空气质量预报”
android:textColor=“@color/white”
android:textSize=“@dimen/sp_16”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”/>
<androidx.recyclerview.widget.RecyclerView
android:id=“@+id/rv_five_air”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginBottom=“@dimen/dp_12”
android:overScrollMode=“never” />
</androidx.core.widget.NestedScrollView>
现在可以创建item的布局文件里,这里面有两个列表,自然就需要两个item的布局,
先来看第一个的布局,在layout下新建一个item_more_air_station_list.xml,这个用于展示检测站的空气质量。代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“vertical”
android:padding=“@dimen/dp_12”>
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:background=“@drawable/shape_transparent_12”
android:gravity=“center_horizontal”
android:orientation=“vertical”
android:padding=“@dimen/dp_12”>
<TextView
android:id=“@+id/tv_station_name”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:textColor=“@color/white”
android:textSize=“@dimen/sp_18” />
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“@dimen/dp_20”
android:orientation=“vertical”>
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“horizontal”>
<TextView
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:text=“空气质量”
android:textColor=“@color/blue_one”
android:textSize=“12sp” />
<TextView
android:id=“@+id/tv_air_category”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:textColor=“@color/white”
android:textSize=“12sp” />
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“@dimen/dp_12”
android:orientation=“horizontal”>
<TextView
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:text=“空气质量指数”
android:textColor=“@color/blue_one”
android:textSize=“12sp” />
<TextView
android:id=“@+id/tv_aqi”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:textColor=“@color/white”
android:textSize=“12sp” />
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“@dimen/dp_12”
android:orientation=“horizontal”>
<TextView
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:text=“主要污染物”
android:textColor=“@color/blue_one”
android:textSize=“12sp” />
<TextView
android:id=“@+id/tv_primary”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:textColor=“@color/white”
android:textSize=“12sp” />
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“12dp”>
<TextView
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:text=“PM10”
android:textColor=“@color/blue_one”
android:textSize=“12sp” />
<TextView
android:id=“@+id/tv_pm10”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:textColor=“@color/white”
android:textSize=“12sp” />
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“12dp”>
<TextView
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:text=“PM2.5”
android:textColor=“@color/blue_one”
android:textSize=“12sp” />
<TextView
android:id=“@+id/tv_pm25”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:textColor=“@color/white”
android:textSize=“12sp” />
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“12dp”>
<TextView
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:text=“二氧化氮”
android:textColor=“@color/blue_one”
android:textSize=“12sp” />
<TextView
android:id=“@+id/tv_no2”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:textColor=“@color/white”
android:textSize=“12sp” />
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“12dp”>
<TextView
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:text=“二氧化硫”
android:textColor=“@color/blue_one”
android:textSize=“12sp” />
<TextView
android:id=“@+id/tv_so2”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:textColor=“@color/white”
android:textSize=“12sp” />
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“12dp”>
<TextView
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:text=“一氧化碳”
android:textColor=“@color/blue_one”
android:textSize=“12sp” />
<TextView
android:id=“@+id/tv_o3”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:textColor=“@color/white”
android:textSize=“12sp” />
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“12dp”>
<TextView
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:text=“臭氧”
android:textColor=“@color/blue_one”
android:textSize=“12sp” />
<TextView
android:id=“@+id/tv_co”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:textColor=“@color/white”
android:textSize=“12sp” />
比较的简单,下面在layout下创建item_more_air_five_list.xml,用于展示当前城市未来五天的空气质量预报。布局如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:orientation=“vertical”
android:padding=“@dimen/dp_12”>
<LinearLayout
android:layout_width=“wrap_content”
android:layout_height=“match_parent”
android:background=“@drawable/shape_transparent_12”
android:gravity=“center_horizontal”
android:orientation=“vertical”
android:padding=“@dimen/dp_12”>
<LinearLayout
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:gravity=“center”
android:orientation=“vertical”>
<TextView
android:id=“@+id/tv_date_info”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:textColor=“@color/white”
android:textSize=“@dimen/sp_18” />
<TextView
android:id=“@+id/tv_date”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:textColor=“@color/white”
android:textSize=“@dimen/sp_14” />
<LinearLayout
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“AQI指数”
android:textColor=“@color/white” />
<TextView
android:id=“@+id/tv_aqi”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginLeft=“@dimen/dp_20”
android:text=“空气质量”
android:textColor=“@color/white” />
<LinearLayout
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginTop=“@dimen/dp_4”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“空气质量”
android:textColor=“@color/white” />
<TextView
android:id=“@+id/tv_category”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginLeft=“@dimen/dp_20”
android:text=“空气质量”
android:textColor=“@color/white” />
<LinearLayout
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginTop=“@dimen/dp_4”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“污染物”
android:textColor=“@color/white” />
<TextView
android:id=“@+id/tv_primary”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginLeft=“@dimen/dp_20”
android:text=“空气质量”
android:textColor=“@color/white” />
因为这个查询多天的空气质量之前并没有写上去,所以要在ApiService中新增一个,同时需要在app的bean包下面新建一个数据实体MoreAirFiveResponse,代码如下:
package com.llw.goodweather.bean;
import java.util.List;
public class MoreAirFiveResponse {
/**
-
code : 200
-
updateTime : 2020-08-06T09:28+08:00
-
fxLink : http://hfx.link/2ax4
-
daily : [{“fxDate”:“2020-08-06”,“aqi”:“60”,“level”:“2”,“category”:“良”,“primary”:“NA”},{“fxDate”:“2020-08-07”,“aqi”:“90”,“level”:“2”,“category”:“良”,“primary”:“NA”},{“fxDate”:“2020-08-08”,“aqi”:“100”,“level”:“2”,“category”:“良”,“primary”:“NA”},{“fxDate”:“2020-08-09”,“aqi”:“110”,“level”:“3”,“category”:“轻度污染”,“primary”:“NA”},{“fxDate”:“2020-08-10”,“aqi”:“90”,“level”:“2”,“category”:“良”,“primary”:“NA”}]
-
refer : {“sources”:[“cnemc”],“license”:[“no commercial use”]}
*/
private String code;
private String updateTime;
private String fxLink;
private ReferBean refer;
private List daily;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getUpdateTime() {
return updateTime;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
public String getFxLink() {
return fxLink;
}
public void setFxLink(String fxLink) {
this.fxLink = fxLink;
}
public ReferBean getRefer() {
return refer;
}
public void setRefer(ReferBean refer) {
this.refer = refer;
}
public List getDaily() {
return daily;
}
public void setDaily(List daily) {
this.daily = daily;
}
public static class ReferBean {
private List sources;
private List license;
public List getSources() {
return sources;
}
public void setSources(List sources) {
this.sources = sources;
}
public List getLicense() {
return license;
}
public void setLicense(List license) {
this.license = license;
}
}
public static class DailyBean {
/**
-
fxDate : 2020-08-06
-
aqi : 60
-
level : 2
-
category : 良
-
primary : NA
*/
private String fxDate;
private String aqi;
private String level;
private String category;
private String primary;
public String getFxDate() {
return fxDate;
}
public void setFxDate(String fxDate) {
this.fxDate = fxDate;
}
public String getAqi() {
return aqi;
}
public void setAqi(String aqi) {
this.aqi = aqi;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getPrimary() {
return primary;
}
public void setPrimary(String primary) {
this.primary = primary;
}
}
}
ApiService中新增如下接口
/**
-
空气质量5天预报
-
@param location 城市id
-
@return 返回空气质量5天预报数据
*/
@GET(“/v7/air/5d?key=3086e91d66c04ce588a7f538f917c7f4”)
Call airFiveWeather(@Query(“location”) String location);
现在可以创建一个新的订阅器了,在app的contract包下新建一个MoreAirContract,代码如下:
package com.llw.goodweather.contract;
import com.llw.goodweather.api.ApiService;
import com.llw.goodweather.bean.AirNowResponse;
import com.llw.goodweather.bean.DailyResponse;
import com.llw.goodweather.bean.MoreAirFiveResponse;
import com.llw.goodweather.bean.NewSearchCityResponse;
import com.llw.mvplibrary.base.BasePresenter;
import com.llw.mvplibrary.base.BaseView;
import com.llw.mvplibrary.net.NetCallBack;
import com.llw.mvplibrary.net.ServiceGenerator;
import retrofit2.Call;
import retrofit2.Response;
/**
- 更多空气质量数据订阅器
*/
public class MoreAirContract {
public static class MoreAirPresenter extends BasePresenter {
/**
-
搜索城市 搜索站点的城市id,用于查询空气质量
-
@param location 城市名
*/
public void searchCityId(String location) {//注意这里的4表示新的搜索城市地址接口
ApiService service = ServiceGenerator.createService(ApiService.class, 4);//指明访问的地址
service.newSearchCity(location,“exact”).enqueue(new NetCallBack() {
@Override
public void onSuccess(Call call, Response response) {
if(getView() != null){
getView().getSearchCityIdResult(response);
}
}
@Override
public void onFailed() {
if(getView() != null){
getView().getDataFailed();
}
}
});
}
/**
-
空气质量 V7
-
@param location 城市id
*/
public void air(String location) {
ApiService service = ServiceGenerator.createService(ApiService.class,3);
service.airNowWeather(location).enqueue(new NetCallBack() {
@Override
public void onSuccess(Call call, Response response) {
if(getView() != null){
getView().getMoreAirResult(response);
}
}
@Override
public void onFailed() {
if(getView() != null){
getView().getDataFailed();
}
}
});
}
/**
-
五天空气质量数据 V7
-
@param location 城市id
*/
public void airFive(String location) {
ApiService service = ServiceGenerator.createService(ApiService.class,3);
service.airFiveWeather(location).enqueue(new NetCallBack() {
@Override
public void onSuccess(Call call, Response response) {
if(getView() != null){
getView().getMoreAirFiveResult(response);
}
}
@Override
public void onFailed() {
if(getView() != null){
getView().getDataFailed();
}
}
});
}
}
public interface IMoreAirView extends BaseView {
//搜索城市Id
void getSearchCityIdResult(Response response);
//空气质量返回数据 V7
void getMoreAirResult(Response response);
//五天空气质量数据返回 V7
void getMoreAirFiveResult(Response response);
//错误返回
void getDataFailed();
}
}
里面有三个请求和返回以及一个异常返回,因为空气质量比较特殊,所以我需要先使用搜索城市接口,将MainActivity中传递过来的上级城市(监测站)先查询一次,得到这个城市的id,再通过这个id去请求当前空气质量和未来五天空气质量的接口,这样才能拿到数据。以便于展示。
下面创建两个列表数据适配器,在app的adapter包下新建MoreAirStationAdapter,用于展示检测站点的数据
package com.llw.goodweather.adapter;
import androidx.annotation.Nullable;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.llw.goodweather.R;
import com.llw.goodweather.bean.AirNowResponse;
import java.util.List;
/**
- 更多空气质量之空气站点列表适配器
*/
public class MoreAirStationAdapter extends BaseQuickAdapter<AirNowResponse.StationBean, BaseViewHolder> {
public MoreAirStationAdapter(int layoutResId, @Nullable List<AirNowResponse.StationBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, AirNowResponse.StationBean item) {
helper.setText(R.id.tv_station_name, item.getName())//监测站名称
.setText(R.id.tv_air_category, item.getCategory())//空气质量
.setText(R.id.tv_aqi, item.getAqi())//空气质量指数
.setText(R.id.tv_primary, item.getPrimary().equals(“NA”) ? “无污染” : item.getPrimary())//污染物
.setText(R.id.tv_pm10, item.getPm10())//pm10
.setText(R.id.tv_pm25, item.getPm2p5())//pm2.5
.setText(R.id.tv_no2, item.getNo2())//二氧化氮
.setText(R.id.tv_so2, item.getSo2())//二氧化硫
.setText(R.id.tv_o3, item.getO3())//臭氧
.setText(R.id.tv_co, item.getCo());//一氧化碳
}
}
OK,再创建一个MoreAirFiveAdapter,代码如下:
package com.llw.goodweather.adapter;
import androidx.annotation.Nullable;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.llw.goodweather.R;
import com.llw.goodweather.bean.MoreAirFiveResponse;
import com.llw.goodweather.utils.DateUtils;
import java.util.List;
/**
- 5天空气质量预报适配器
*/
public class MoreAirFiveAdapter extends BaseQuickAdapter<MoreAirFiveResponse.DailyBean, BaseViewHolder> {
public MoreAirFiveAdapter(int layoutResId, @Nullable List<MoreAirFiveResponse.DailyBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, MoreAirFiveResponse.DailyBean item) {
helper.setText(R.id.tv_date_info, DateUtils.Week(item.getFxDate()))//日期描述
.setText(R.id.tv_date, DateUtils.dateSplit(item.getFxDate()))//日期
.setText(R.id.tv_aqi,item.getAqi())//空气质量指数
.setText(R.id.tv_category,item.getCategory())//空气质量描述
.setText(R.id.tv_primary, item.getPrimary().equals(“NA”) ? “无污染” : item.getPrimary());//污染物
}
}
然后进入到MoreAirActivity,里面的代码如下:
package com.llw.goodweather.ui;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import com.llw.goodweather.R;
import com.llw.goodweather.adapter.MoreAirFiveAdapter;
import com.llw.goodweather.adapter.MoreAirStationAdapter;
import com.llw.goodweather.bean.AirNowResponse;
import com.llw.goodweather.bean.MoreAirFiveResponse;
import com.llw.goodweather.bean.NewSearchCityResponse;
import com.llw.goodweather.contract.MoreAirContract;
import com.llw.goodweather.utils.CodeToStringUtils;
import com.llw.goodweather.utils.Constant;
import com.llw.goodweather.utils.DateUtils;
import com.llw.mvplibrary.view.LineProgressbar;
import com.llw.goodweather.utils.StatusBarUtil;
import com.llw.goodweather.utils.ToastUtils;
import com.llw.goodweather.utils.WeatherUtil;
import com.llw.mvplibrary.mvp.MvpActivity;
import com.llw.mvplibrary.view.RoundProgressBar;
import java.util.List;
import butterknife.BindView;
import retrofit2.Response;
/**
- 更多空气质量信息
*/
public class MoreAirActivity extends MvpActivity<MoreAirContract.MoreAirPresenter> implements MoreAirContract.IMoreAirView {
@BindView(R.id.tv_title)
TextView tvTitle;//标题
@BindView(R.id.toolbar)
Toolbar toolbar;//toolbar
@BindView(R.id.rpb_aqi)
RoundProgressBar rpbAqi;//圆环进度条
@BindView(R.id.tv_pm10)
TextView tvPm10;//pm10
@BindView(R.id.tv_pm25)
TextView tvPm25;//pm2.5
@BindView(R.id.tv_no2)
TextView tvNo2;//二氧化氮
@BindView(R.id.tv_so2)
TextView tvSo2;//二氧化硫
@BindView(R.id.tv_o3)
TextView tvO3;//臭氧
@BindView(R.id.tv_co)
TextView tvCo;//一氧化碳
@BindView(R.id.tv_old_time)
TextView tvOldTime;//最近更新时间
@BindView(R.id.rv_station)
RecyclerView rvStation;//检测站数据列表
@BindView(R.id.progress_pm10)
LineProgressbar progressPm10;//pm10含量进度条展示
@BindView(R.id.progress_pm25)
LineProgressbar progressPm25;//pm2.5含量进度条展示
@BindView(R.id.progress_no2)
LineProgressbar progressNo2;//二氧化氮含量进度条展示
@BindView(R.id.progress_so2)
LineProgressbar progressSo2;//二氧化硫含量进度条展示
@BindView(R.id.progress_o3)
LineProgressbar progressO3;//臭氧含量进度条展示
@BindView(R.id.progress_co)
LineProgressbar progressCo;//一氧化碳含量进度条展示
@BindView(R.id.rv_five_air)
RecyclerView rvFiveAir;//最近5天空气质量列表
@Override
public void initData(Bundle savedInstanceState) {
StatusBarUtil.transparencyBar(context);//透明状态栏
Back(toolbar);
showLoadingDialog();
String stationName = getIntent().getStringExtra(“stationName”);
tvTitle.setText(stationName + " - " + getIntent().getStringExtra(“cityName”));
mPresent.searchCityId(stationName);//搜索城市返回Id
}
@Override
public int getLayoutId() {
return R.layout.activity_more_air;
}
@Override
protected MoreAirContract.MoreAirPresenter createPresent() {
return new MoreAirContract.MoreAirPresenter();
}
/**
-
搜索城市返回Id 通过id查询城市的空气质量和站点空气质量
-
@param response
*/
@Override
public void getSearchCityIdResult(Response response) {
dismissLoadingDialog();
if (response.body().getStatus().equals(Constant.SUCCESS_CODE)) {
showLoadingDialog();
List<NewSearchCityResponse.LocationBean> data = response.body().getLocation();
if (data != null && data.size() > 0) {
mPresent.air(data.get(0).getId());//查询该站点的空气质量数据
mPresent.airFive(data.get(0).getId());//查询该站点的空气质量数据
} else {
ToastUtils.showShortToast(context, “未查询到相关数据”);
}
} else {
ToastUtils.showShortToast(context, CodeToStringUtils.WeatherCode(response.body().getStatus()));
}
}
/**
-
更多空气质量的数据展示
-
@param response
*/
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void getMoreAirResult(Response response) {
dismissLoadingDialog();
if (response.body().getCode().equals(Constant.SUCCESS_CODE)) {
AirNowResponse.NowBean data = response.body().getNow();
List<AirNowResponse.StationBean> station = response.body().getStation();
if (response.body().getNow() != null) {
String time = DateUtils.updateTime(response.body().getUpdateTime());//截去前面的字符,保留后面所有的字符,就剩下 22:00
tvOldTime.setText(“最近更新时间:” + WeatherUtil.showTimeInfo(time) + time);
showAirBasicData(data);//展示基础数据
//展示检测站列表数据
MoreAirStationAdapter mAdapter = new MoreAirStationAdapter(R.layout.item_more_air_station_list, station);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
rvStation.setLayoutManager(linearLayoutManager);
PagerSnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(rvStation);//滚动对齐,使RecyclerView像ViewPage一样,一次滑动一项,居中
rvStation.setAdapter(mAdapter);
} else {
ToastUtils.showShortToast(context, “空气质量数据为空”);
}
} else {
ToastUtils.showShortToast(context, CodeToStringUtils.WeatherCode(response.body().getCode()));
}
}
/**
-
未来5天空气质量返回
-
@param response
*/
@Override
public void getMoreAirFiveResult(Response response) {
if (response.body().getCode().equals(Constant.SUCCESS_CODE)) {
List<MoreAirFiveResponse.DailyBean> data = response.body().getDaily();
if (data != null && data.size() > 0) {
MoreAirFiveAdapter adapter = new MoreAirFiveAdapter(R.layout.item_more_air_five_list, data);
LinearLayoutManager manager = new LinearLayoutManager(context);
manager.setOrientation(RecyclerView.HORIZONTAL);
rvFiveAir.setLayoutManager(manager);
rvFiveAir.setAdapter(adapter);
} else {
ToastUtils.showShortToast(context, “未来5天空气质量数据为空”);
}
} else {
ToastUtils.showShortToast(context, CodeToStringUtils.WeatherCode(response.body().getCode()));
}
}
/**
-
展示基础数据
-
@param data 数据源
*/
private void showAirBasicData(AirNowResponse.NowBean data) {
rpbAqi.setMaxProgress(300);//最大进度,用于计算
rpbAqi.setMinText(“0”);//设置显示最小值
rpbAqi.setMinTextSize(32f);
rpbAqi.setMaxText(“300”);//设置显示最大值
rpbAqi.setMaxTextSize(32f);
rpbAqi.setProgress(Float.valueOf(data.getAqi()));//当前进度
rpbAqi.setArcBgColor(getResources().getColor(R.color.arc_bg_color));//圆弧的颜色
rpbAqi.setProgressColor(getResources().getColor(R.color.arc_progress_color));//进度圆弧的颜色
rpbAqi.setFirstText(data.getCategory());//空气质量描述 取值范围:优,良,轻度污染,中度污染,重度污染,严重污染
rpbAqi.setFirstTextSize(44f);//第一行文本的字体大小
rpbAqi.setSecondText(data.getAqi());//空气质量值
rpbAqi.setSecondTextSize(64f);//第二行文本的字体大小
rpbAqi.setMinText(“0”);
rpbAqi.setMinTextColor(getResources().getColor(R.color.arc_progress_color));
tvPm10.setText(data.getPm10());//PM10 + " μg/m3"
progressPm10.setProgress(data.getPm10(), 100);
tvPm25.setText(data.getPm2p5());//PM2.5
progressPm25.setProgress(data.getPm2p5(), 100);
tvNo2.setText(data.getNo2());//二氧化氮
progressNo2.setProgress(data.getNo2(), 100);
tvSo2.setText(data.getSo2());//二氧化硫
progressSo2.setProgress(data.getSo2(), 100);
tvO3.setText(data.getO3());//臭氧
progressO3.setProgress(data.getO3(), 100);
tvCo.setText(data.getCo());//一氧化碳
progressCo.setProgress(data.getCo(), 100);
}
/**
- 其他异常返回
*/
@Override
public void getDataFailed() {
dismissLoadingDialog();
ToastUtils.showShortToast(context, “更多空气质量数据获取异常”);
}
}
注意到
我在initData的时候获取intent携带的数据,检测站名称,这就说明我要在MainActivity中进行传递才行,否则你进这个页面就会空指针的。下面进入到MainActivity.
在搜索城市返回时获取到这个检测站的名称,stationName是在上一篇就创建了,如果你没有的话就创建就可以了
private String stationName = null;
现在已经赋值了,然后在goToMore中增加一个传递过去的参数,这样就不会报错了。
跳转进入
指定到这个MoreAirActivity,现在就可以运行测试一下了。
三、更多生活建议数据展示
下面来写更多生活建议数据展示,app的ui包下新建MoreLifestyleActivity,
背景图
activity_more_lifestyle.xml布局代码如下:
<?xml version="1.0" encoding="utf-8"?><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:orientation=“vertical”
android:fitsSystemWindows=“true”
android:background=“@drawable/more_lifestyle”
tools:context=“.ui.MoreLifestyleActivity”>
<androidx.appcompat.widget.Toolbar
android:id=“@+id/toolbar”
android:layout_width=“match_parent”
android:layout_height=“?attr/actionBarSize”
app:contentInsetLeft=“@dimen/dp_16”
app:layout_constraintEnd_toEndOf=“parent”
app:layout_constraintLeft_toLeftOf=“parent”
app:layout_constraintTop_toTopOf=“parent”
app:navigationIcon=“@mipmap/icon_return_white”
app:popupTheme=“@style/AppTheme.PopupOverlay”>
<TextView
android:id=“@+id/tv_title”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“center”
android:text=“更多生活建议”
android:textColor=“@color/white”
android:textSize=“@dimen/sp_18” />
</androidx.appcompat.widget.Toolbar>
<androidx.recyclerview.widget.RecyclerView
android:id=“@+id/rv”
android:overScrollMode=“never”
android:layout_width=“match_parent”
android:layout_height=“match_parent”/>
然后写item的布局,在app的layout下新建一个item_more_lifestyle_list.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“vertical”
android:padding=“@dimen/dp_12”>
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:background=“@drawable/shape_transparent_12”
android:orientation=“vertical”
android:padding=“@dimen/dp_12”>
<TextView
android:id=“@+id/tv_name”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:textColor=“@color/white”
android:textSize=“@dimen/sp_16” />
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“@dimen/dp_4”
android:gravity=“center_vertical”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginRight=“@dimen/dp_8”
android:text=“指数等级”
android:textColor=“@color/white”
android:textSize=“@dimen/sp_12” />
<ProgressBar
android:id=“@+id/progressBar”
style=“?android:attr/progressBarStyleHorizontal”
android:layout_width=“0dp”
android:layout_height=“6dp”
android:layout_weight=“1”
android:max=“10”
android:progress=“10”
android:progressDrawable=“@drawable/progress_bg”
android:visibility=“visible” />
<TextView
android:id=“@+id/tv_content”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“@dimen/dp_4”
android:textColor=“@color/white”
android:textSize=“@dimen/sp_12” />
里面用到了一个样式progress_bg.xml,在drawable下创建一个,代码如下:
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
文末
面试:如果不准备充分的面试,完全是浪费时间,更是对自己的不负责!
不管怎么样,不论是什么样的大小面试,要想不被面试官虐的不要不要的,只有刷爆面试题题做好全面的准备,当然除了这个还需要在平时把自己的基础打扎实,这样不论面试官怎么样一个知识点里往死里凿,你也能应付如流啊
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
.android.com/apk/res/android"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“vertical”
android:padding=“@dimen/dp_12”>
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:background=“@drawable/shape_transparent_12”
android:orientation=“vertical”
android:padding=“@dimen/dp_12”>
<TextView
android:id=“@+id/tv_name”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:textColor=“@color/white”
android:textSize=“@dimen/sp_16” />
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“@dimen/dp_4”
android:gravity=“center_vertical”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginRight=“@dimen/dp_8”
android:text=“指数等级”
android:textColor=“@color/white”
android:textSize=“@dimen/sp_12” />
<ProgressBar
android:id=“@+id/progressBar”
style=“?android:attr/progressBarStyleHorizontal”
android:layout_width=“0dp”
android:layout_height=“6dp”
android:layout_weight=“1”
android:max=“10”
android:progress=“10”
android:progressDrawable=“@drawable/progress_bg”
android:visibility=“visible” />
<TextView
android:id=“@+id/tv_content”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“@dimen/dp_4”
android:textColor=“@color/white”
android:textSize=“@dimen/sp_12” />
里面用到了一个样式progress_bg.xml,在drawable下创建一个,代码如下:
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-X9slnyHD-1713751240571)]
[外链图片转存中…(img-TWX7Amd2-1713751240572)]
[外链图片转存中…(img-0HvmeHzR-1713751240574)]
[外链图片转存中…(img-HDHD9ecU-1713751240575)]
[外链图片转存中…(img-JC6J1bS3-1713751240576)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
[外链图片转存中…(img-7oGXvnp4-1713751240577)]
文末
面试:如果不准备充分的面试,完全是浪费时间,更是对自己的不负责!
不管怎么样,不论是什么样的大小面试,要想不被面试官虐的不要不要的,只有刷爆面试题题做好全面的准备,当然除了这个还需要在平时把自己的基础打扎实,这样不论面试官怎么样一个知识点里往死里凿,你也能应付如流啊
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!