Android 天气APP(二十二)改动些许UI、增加更多空气质量数据和生活建议数据展示

上一篇:Android 天气APP(二十一)滑动改变UI、增加更多天气数据展示,最多未来15天天气预报

前言

这篇文章接着上一篇来写的,主要是增加更多空气质量的数据展示和更多生活建议数据的展示。

正文

一、改动主页UI

先简单改动一下主页面的UI吧,打开activity_main.xml
在这里插入图片描述
温度的上方原来是天气描述,我给移到下面来了。
在这里插入图片描述
最高温和最低温,一分为二,我希望通过不同的字体颜色来表示高温和低温的区别,这样会更直观。
在这里插入图片描述
我在最高温和最低温的下面放上了空气质量和天气描述。
现在可以修改MainActivity中的代码了。
在这里插入图片描述
把原来的tvLowHeight注释掉,并且增加新的最高温最低温数据展示
在这里插入图片描述
在tvInfo所在位置增加一个星期的数据展示
在这里插入图片描述
再增加一个空气质量的展示。然后运行

在这里插入图片描述
在这里插入图片描述
OK,然后来改一下天气预报列表的样式
在这里插入图片描述
这是好久之前的了,抛开图标不谈我需要改左右两边文字的展示效果,修改item_weather_forecast_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/item_forecast"
    android:foreground="@drawable/bg_white"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:gravity="center_vertical"
        android:padding="@dimen/sp_12"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!--日期-->
        <TextView
            android:id="@+id/tv_date"
            android:text="1234"
            android:textSize="@dimen/sp_14"
            android:textColor="#FFF"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
        <!--天气描述  文字 隐藏-->
        <TextView
            android:visibility="gone"
            android:gravity="center"
            android:id="@+id/tv_info"
            android:textSize="@dimen/sp_14"
            android:textColor="#FFF"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
        <!--天气描述  图标-->
        <ImageView
            android:id="@+id/iv_weather_state"
            android:background="@mipmap/icon_100"
            android:layout_width="30dp"
            android:layout_height="30dp"/>

        <!--最低温、最高温-->
        <LinearLayout
            android:gravity="right"
            android:layout_width="@dimen/dp_0"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <TextView
                android:id="@+id/tv_temp_height"
                android:textSize="@dimen/sp_14"
                android:textColor="#FFF"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/tv_temp_low"
                android:textSize="@dimen/sp_14"
                android:textColor="@color/temp_min_tx"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

就是把里面的温度地段改成了两个,用不同的颜色区分开,然后进入DailyAdapter
在这里插入图片描述
照着这个截图上面的修改就可以了,方法应该是都有的。
最后在渲染数据的时候增加动画
在这里插入图片描述
在这里插入图片描述

我在天气预报的返回和逐小时天气的返回数据中做了动画的渲染,注意到用了两个不同的动画,一个是底部往上弹,一个是从右往左弹。
运行之后效果如下
在这里插入图片描述
这样看起来可能舒服一些吧,

二、更多空气质量数据展示

下面就要开始写更多空气质量的页面展示了。
老样子,在appui包下新建一个MoreAirActivity,然后修改布局
,在修改之前我们先写一个自定义View,这当然也是需要样式的
mvplibrarystyles.xml中新增一个样式

	<!--线性进度条-->
    <declare-styleable name="LineProgressbar">
        <attr name="progressbar_width" format="dimension"/>
        <attr name="progressbar_height" format="dimension"/>
    </declare-styleable>

然后在mvplibraryview包中新建一个LineProgressbar,代码如下:

package com.llw.mvplibrary.view;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import com.llw.mvplibrary.R;

public class LineProgressbar extends View {

    private Paint mPaint;//画笔

    private float mPaintWidth = 6f;//初始画笔宽度

    private int mProgressbarWidth;//控件外边框宽度

    private int mProgressbarHeight;//控件外边框高度

    private int mPercent = 0;//已转化为0至100范围的当前进度,随动画时间改变而改变

    public LineProgressbar(Context context) {
        super(context);
    }

    @SuppressLint("Recycle")
    public LineProgressbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.LineProgressbar);
        mProgressbarWidth = (int) array.getDimension(R.styleable.LineProgressbar_progressbar_width, 100);
        mProgressbarHeight = (int) array.getDimension(R.styleable.LineProgressbar_progressbar_height, 10);
    }

    public LineProgressbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(mProgressbarWidth, mProgressbarHeight);
    }

    @Override
    @SuppressLint("DrawAllocation")
    protected void onDraw(Canvas canvas) {
        mPaint = new Paint();

        //绘制背景
        mPaint.setColor(getResources().getColor(R.color.arc_bg_color));
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(mPaintWidth);
        RectF frameRectF = new RectF(mPaintWidth, mPaintWidth, mProgressbarWidth - mPaintWidth, mProgressbarHeight - mPaintWidth);
        canvas.drawRoundRect(frameRectF, 15, 15, mPaint);

        //填充内部进度
        mPaint.setPathEffect(null);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        //内部进度填充长度,随动画时间改变而改变
        float percent = (float) mPercent / 100f;
        RectF progressRectF = new RectF(mPaintWidth, mPaintWidth, mPaintWidth + percent * (mProgressbarWidth - 2 * mPaintWidth - 2), mProgressbarHeight - mPaintWidth);
        canvas.drawRoundRect(progressRectF, 15, 15, mPaint);
    }

    public void setProgress(String progress, int maxProgress) {

        int percent = 0;

        //得出当前progress占最大进度值百分比(0-100)
        if (progress.contains(".")) {//float或者double类型
            percent = ((int) Float.parseFloat(progress) * 10) * 100 / (maxProgress * 10);
        } else {//int类型
            percent = Integer.parseInt(progress) * 100 / maxProgress;
        }

        if (percent < 0) {
            percent = 0;
        }
        if (percent > 100) {
            percent = 100;
        }

        //属性动画
        ValueAnimator animator = ValueAnimator.ofInt(0, percent);
        animator.setDuration(1000);
        animator.setInterpolator(new AccelerateInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                mPercent = (int) valueAnimator.getAnimatedValue();
                invalidate();
            }
        });
        animator.start();
    }
}

现在可以看布局文件activity_more_air.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:background="@drawable/more_air_bg"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context=".ui.MoreAirActivity">

    <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_16" />
    </androidx.appcompat.widget.Toolbar>

    <androidx.core.widget.NestedScrollView
        android:overScrollMode="never"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <!--实时空气质量-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/dp_12"
                android:background="@drawable/shape_transparent_12"
                android:orientation="vertical"
                android:padding="@dimen/dp_12">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="实时空气质量"
                        android:textColor="@color/white"
                        android:textSize="@dimen/sp_16" />

                    <TextView
                        android:id="@+id/tv_old_time"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="right"
                        android:text="上次更新时间:"
                        android:textColor="@color/white"
                        android:textSize="@dimen/sp_12" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dp_12">
                    <!--污染指数 动画展示-->
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:orientation="vertical">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="8dp"
                            android:text="污染指数"
                            android:textColor="#DAEBEE"
                            android:textSize="14sp" />
                        <!--显示污染指数进度值-->
                        <com.llw.mvplibrary.view.RoundProgressBar
                            android:id="@+id/rpb_aqi"
                            android:layout_width="120dp"
                            android:layout_height="120dp"
                            android:layout_gravity="center"
                            app:round_bg_color="#C6D7F4"
                            app:round_progress_color="#FBFEF7" />
                    </LinearLayout>

                    <!--其他指数-->
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:orientation="vertical">
                        <!--PM10-->
                        <LinearLayout
                            android:gravity="center_vertical"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                            <TextView
                                android:layout_width="@dimen/dp_44"
                                android:layout_height="wrap_content"
                                android:gravity="center"
                                android:text="PM10"
                                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>

                        <!--PM2.5-->
                        <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>

                        <!--NO2 二氧化氮-->
                        <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" />
                            </LinearLayout>

                            <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>

                        <!--SO2 二氧化硫-->
                        <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" />
                            </LinearLayout>

                            <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>

                        <!--O3 臭氧-->
                        <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" />
                            </LinearLayout>
                            <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>

                        <!--CO 一氧化碳-->
                        <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" />
                        </LinearLayout>
                    </LinearLayout>
                </LinearLayout>

            </LinearLayout>

            <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" />
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

</LinearLayout>

在这里插入图片描述

现在可以创建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>

            <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>

            <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>

            <!--PM10-->
            <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>

            <!--PM2.5-->
            <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>

            <!--NO2 二氧化氮-->
            <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>

            <!--SO2 二氧化硫-->
            <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>

            <!--O3 臭氧-->
            <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>

            <!--CO 一氧化碳-->
            <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" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

在这里插入图片描述

比较的简单,下面在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>

        <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>

        <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>

        <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" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

在这里插入图片描述
因为这个查询多天的空气质量之前并没有写上去,所以要在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<DailyBean> 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<DailyBean> getDaily() {
        return daily;
    }

    public void setDaily(List<DailyBean> daily) {
        this.daily = daily;
    }

    public static class ReferBean {
        private List<String> sources;
        private List<String> license;

        public List<String> getSources() {
            return sources;
        }

        public void setSources(List<String> sources) {
            this.sources = sources;
        }

        public List<String> getLicense() {
            return license;
        }

        public void setLicense(List<String> 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<MoreAirFiveResponse> airFiveWeather(@Query("location") String location);

现在可以创建一个新的订阅器了,在appcontract包下新建一个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<IMoreAirView> {


        /**
         * 搜索城市  搜索站点的城市id,用于查询空气质量
         * @param location 城市名
         */
        public void searchCityId(String location) {//注意这里的4表示新的搜索城市地址接口
            ApiService service = ServiceGenerator.createService(ApiService.class, 4);//指明访问的地址
            service.newSearchCity(location,"exact").enqueue(new NetCallBack<NewSearchCityResponse>() {
                @Override
                public void onSuccess(Call<NewSearchCityResponse> call, Response<NewSearchCityResponse> 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<AirNowResponse>() {
                @Override
                public void onSuccess(Call<AirNowResponse> call, Response<AirNowResponse> 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<MoreAirFiveResponse>() {
                @Override
                public void onSuccess(Call<MoreAirFiveResponse> call, Response<MoreAirFiveResponse> 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<NewSearchCityResponse> response);

        //空气质量返回数据 V7
        void getMoreAirResult(Response<AirNowResponse> response);

        //五天空气质量数据返回 V7
        void getMoreAirFiveResult(Response<MoreAirFiveResponse> response);

        //错误返回
        void getDataFailed();



    }
}

里面有三个请求和返回以及一个异常返回,因为空气质量比较特殊,所以我需要先使用搜索城市接口,将MainActivity中传递过来的上级城市(监测站)先查询一次,得到这个城市的id,再通过这个id去请求当前空气质量和未来五天空气质量的接口,这样才能拿到数据。以便于展示。
下面创建两个列表数据适配器,在appadapter包下新建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<NewSearchCityResponse> 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<AirNowResponse> 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<MoreAirFiveResponse> 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,现在就可以运行测试一下了。

在这里插入图片描述

三、更多生活建议数据展示

下面来写更多生活建议数据展示,appui包下新建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"/>


</LinearLayout>

在这里插入图片描述
然后写item的布局,在applayout下新建一个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" />
        </LinearLayout>

        <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" />
    </LinearLayout>
</LinearLayout>

里面用到了一个样式progress_bg.xml,在drawable下创建一个,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!--背景效果(浅灰色)-->
    <item android:id="@android:id/background">

        <shape>
            <corners android:radius="40dp" />
            <solid android:color="#0c000000"/>
        </shape>

    </item>

    <!--第一进度条背景(渐变),clip:裁剪进度条右侧部分-->
    <item android:id="@android:id/progress">

        <clip>
            <shape>
                <corners android:radius="40dp" />
                <gradient
                    android:angle="180"
                    android:startColor="#50ACEE"
                    android:centerColor="#6ACCF1"
                    android:endColor="#76EAE7" />
            </shape>
        </clip>
    </item>

</layer-list>

然后在app下的adapter包下新建一个MoreLifestyleAdapter,代码如下:

package com.llw.goodweather.adapter;

import android.widget.ProgressBar;

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.LifestyleResponse;

import java.util.List;

public class MoreLifestyleAdapter extends BaseQuickAdapter<LifestyleResponse.DailyBean, BaseViewHolder> {

    public MoreLifestyleAdapter(int layoutResId, @Nullable List<LifestyleResponse.DailyBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, LifestyleResponse.DailyBean item) {
        helper.setText(R.id.tv_name, item.getName());//名称
        helper.setText(R.id.tv_content, "生活建议:"+item.getText());//内容
        ProgressBar progressBar = helper.getView(R.id.progressBar);
        String type = item.getType();
        int level = Integer.parseInt(item.getLevel());
        switch (type) {//根据不同的类型设置不同的最大进度
            case "1"://运动指数	1	适宜(1)、较适宜(2)、较不宜(3)
            case "4"://钓鱼指数	4	适宜(1)、较适宜(2)、不宜(3)
                progressBar.setMax(3);
                break;
            case "2"://洗车指数	2	适宜(1)、较适宜(2)、较不宜(3)、不宜(4)
            case "9"://感冒指数	9	少发(1)、较易发(2)、易发(3)、极易发(4)
            case "11"://空调开启指数	11	长时间开启(1)、部分时间开启(2)、较少开启(3)、开启制暖空调(4)
                progressBar.setMax(4);
                break;
            case "5"://紫外线指数	5	最弱(1)、弱(2)、中等(3)、强(4)、很强(5)
            case "6"://旅游指数	6	适宜(1)、较适宜(2)、一般(3)、较不宜(4)、不适宜(5)
            case "7"://花粉过敏指数	7	极不易发(1)、不易发(2)、较易发(3)、易发(4)、极易发(5)
            case "10"://空气污染扩散条件指数	10(1)、良(2)、中(3)、较差(4)、很差(5)
            case "12"://太阳镜指数	12	不需要(1)、需要(2)、必要(3)、很必要(4)、非常必要(5)
            case "15"://交通指数	15	良好(1)、较好(2)、一般(3)、较差(4)、很差(5)
            case "16"://防晒指数	16(1)、较弱(2)、中等(3)、强(4)、极强(5)
                progressBar.setMax(5);
                break;
            case "14"://晾晒指数	14	极适宜(1)、适宜(2)、基本适宜(3)、不太适宜(4)、不宜(5)、不适宜(6)
                progressBar.setMax(6);
                break;
            case "3"://穿衣指数	3	寒冷(1)、冷(2)、较冷(3)、较舒适(4)、舒适(5)、热(6)、炎热(7)
            case "8"://舒适度指数	8	舒适(1)、较舒适(2)、较不舒适(3)、很不舒适(4)、极不舒适(5)、不舒适(6)、非常不舒适(7)
                progressBar.setMax(7);
                break;
            case "13"://化妆指数	13	保湿(1)、保湿防晒(2)、去油防晒(3)、防脱水防晒(4)、去油(5)、防脱水(6)、防晒(7)、滋润保湿(8)
                progressBar.setMax(8);
                break;

        }
        progressBar.setProgress(level);//当前等级
    }
}

下面就是订阅器,在app下的contract包下新建一个MoreLifestyleContract,代码如下:

package com.llw.goodweather.contract;

import com.llw.goodweather.api.ApiService;
import com.llw.goodweather.bean.DailyResponse;
import com.llw.goodweather.bean.LifestyleResponse;
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 MoreLifestyleContract {

    public static class MoreLifestylePresenter extends BasePresenter<IMoreLifestyleView> {

        /**
         * 更多生活指数  V7
         * @param location  城市id
         */
        public void worldCity(String location) {
            ApiService service = ServiceGenerator.createService(ApiService.class,3);
            service.lifestyle("0",location).enqueue(new NetCallBack<LifestyleResponse>() {
                @Override
                public void onSuccess(Call<LifestyleResponse> call, Response<LifestyleResponse> response) {
                    if(getView() != null){
                        getView().getMoreLifestyleResult(response);
                    }
                }

                @Override
                public void onFailed() {
                    if(getView() != null){
                        getView().getDataFailed();
                    }
                }
            });
        }

    }

    public interface IMoreLifestyleView extends BaseView {

        //更多生活指数返回数据 V7
        void getMoreLifestyleResult(Response<LifestyleResponse> response);

        //错误返回
        void getDataFailed();
    }
}

然后再回到MoreLifestyleActivity,代码如下:

package com.llw.goodweather.ui;

import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.llw.goodweather.R;
import com.llw.goodweather.adapter.MoreLifestyleAdapter;
import com.llw.goodweather.bean.LifestyleResponse;
import com.llw.goodweather.contract.MoreLifestyleContract;
import com.llw.goodweather.utils.CodeToStringUtils;
import com.llw.goodweather.utils.Constant;
import com.llw.goodweather.utils.StatusBarUtil;
import com.llw.goodweather.utils.ToastUtils;
import com.llw.mvplibrary.mvp.MvpActivity;
import com.llw.mvplibrary.utils.RecyclerViewAnimation;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Response;

import static com.llw.mvplibrary.utils.RecyclerViewAnimation.runLayoutAnimation;

/**
 * 更多生活指数信息
 */
public class MoreLifestyleActivity extends MvpActivity<MoreLifestyleContract.MoreLifestylePresenter> implements MoreLifestyleContract.IMoreLifestyleView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv)
    RecyclerView rv;

    @Override
    public void initData(Bundle savedInstanceState) {
        StatusBarUtil.transparencyBar(context);//透明状态栏
        Back(toolbar);
        showLoadingDialog();
        tvTitle.setText(getIntent().getStringExtra("cityName"));
        mPresent.worldCity(getIntent().getStringExtra("locationId"));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_more_lifestyle;
    }

    @Override
    protected MoreLifestyleContract.MoreLifestylePresenter createPresent() {
        return new MoreLifestyleContract.MoreLifestylePresenter();
    }

    /**
     * 更多生活质量数据返回
     *
     * @param response
     */
    @Override
    public void getMoreLifestyleResult(Response<LifestyleResponse> response) {
        dismissLoadingDialog();
        if (response.body().getCode().equals(Constant.SUCCESS_CODE)) {
            List<LifestyleResponse.DailyBean> data = response.body().getDaily();
            if (data != null && data.size() > 0) {
                MoreLifestyleAdapter adapter = new MoreLifestyleAdapter(R.layout.item_more_lifestyle_list, data);
                rv.setLayoutManager(new LinearLayoutManager(context));
                rv.setAdapter(adapter);
                runLayoutAnimation(rv);
            } else {
                ToastUtils.showShortToast(context, "生活质量数据为空");
            }
        } else {
            ToastUtils.showShortToast(context, CodeToStringUtils.WeatherCode(response.body().getCode()));
        }
    }

    /**
     * 其他异常返回
     */
    @Override
    public void getDataFailed() {
        dismissLoadingDialog();
        ToastUtils.showShortToast(context, "更多天气数据获取异常");
    }

}

最后修改MainActivity中的
在这里插入图片描述
然后运行:
在这里插入图片描述

下一篇:Android 天气APP(二十三)增加灾害预警、优化主页面UI

猜你喜欢

转载自blog.csdn.net/qq_38436214/article/details/107867980