Android 天氣APP(十七)熱門城市 - 國內城市

上一篇:Android 天氣APP(十六)熱門城市 - 海外城市

前言

在上一篇做了國外的熱門城市數據的展示,這一篇就簡單一些,增加國內的熱門城市。
效果圖
在這裏插入圖片描述

正文

① 修改API

在ApiService中修改hotCity這個接口,將固定地址裏面的group分離出來,作爲請求參數。

	/**
     * 熱門城市(包含海外和國內)
     */
    @GET("/top?key=3086e91d66c04ce588a7f538f917c7f4&number=50&lang=zh")
    Call<HotCityResponse> hotCity(@Query("group") String group);

② 修改訂閱器

在HotCityContract中增加一個參數
在這裏插入圖片描述

③ 創建選擇彈窗

之前是在HotActivity中默認查詢海外熱門城市的,現在增加了一個參數,就需要用戶去手動選擇了,我們可以通過一個彈窗來進行選擇。
在layout下創建
在這裏插入圖片描述
佈局代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/dp_280"
    android:layout_height="@dimen/dp_120"
    android:background="@drawable/shape_white_5"
    android:gravity="center"
    android:orientation="vertical">
    <!--國內-->
    <TextView
        android:id="@+id/tv_inland"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:foreground="@drawable/bg_white"
        android:gravity="center"
        android:text="國內熱門城市"
        android:textColor="@color/black"
        android:textSize="@dimen/sp_18" />
    <!--分割線-->
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/gray" />
    <!--海外-->
    <TextView
        android:id="@+id/tv_foreign"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:foreground="@drawable/bg_white"
        android:gravity="center"
        android:text="海外熱門城市"
        android:textColor="@color/black"
        android:textSize="@dimen/sp_18" />
</LinearLayout>

然後就是使用了,我修改了一下LiWindow中的showCenterPopupWindow方法中的入參
在這裏插入圖片描述
將這個值放到外面就可以在調用的時候設置是否可以點擊空白處關閉彈窗,爲true是可以,false是不可以。

修改activity_hot_city.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:fitsSystemWindows="true"
    android:id="@+id/lay_bg"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".ui.HotCityActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:navigationIcon="@mipmap/icon_return"
        app:contentInsetLeft="@dimen/dp_16"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        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:textSize="@dimen/sp_16"
            android:textColor="@color/black"
            android:text="熱門城市" />

    </androidx.appcompat.widget.Toolbar>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

修改的佈局有什麼變化呢?就是裏面的佈局增加了id,還有就是改了顏色,
之後在HotCityActivity中初始化
在這裏插入圖片描述
上圖中標出來的就是新增的,然後創建一個顯示彈窗的方法

	/**
     * 顯示選擇類型彈窗
     */
    private void showTypeWindow() {
        liWindow = new LiWindow(context);
        final View view = LayoutInflater.from(context).inflate(R.layout.window_hot_type, null);
        TextView tvInland = view.findViewById(R.id.tv_inland);//國內
        TextView tvForeign = view.findViewById(R.id.tv_foreign);//海外
        tvInland.setOnClickListener(v -> {
            type = 0;
            initList(type);
            showLoadingDialog();
            mPresent.hotCity(context, "cn");
            liWindow.closePopupWindow();
        });
        tvForeign.setOnClickListener(v -> {
            type = 1;
            initList(type);
            showLoadingDialog();
            mPresent.hotCity(context, "overseas");
            liWindow.closePopupWindow();
        });

        liWindow.showCenterPopupWindow(view, SizeUtils.dp2px(context, 280), SizeUtils.dp2px(context, 120), false);

    }

因爲是要在頁面啓動的時候就出現這個彈窗,而popupWindow顯示依賴activity,並且要等activity所有的生命週期方法全部執行完成才能顯示,所以這裏新開一個線程用於顯示
在這裏插入圖片描述

④ 修改列表item佈局

彈窗搞定之後就可以改動熱門城市的列表item佈局了,首先增加一個顏色
在這裏插入圖片描述

在這裏插入圖片描述
item_hot_city_list.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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <androidx.cardview.widget.CardView
        android:id="@+id/item_hot_city"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/dp_6"
        android:foreground="@drawable/bg_white"
        app:cardBackgroundColor="@color/white"
        app:cardCornerRadius="@dimen/dp_8"
        app:cardElevation="@dimen/dp_4">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">
            <!--增加id-->
            <ImageView
                android:id="@+id/iv_mark"
                android:layout_width="@dimen/dp_80"
                android:layout_height="@dimen/dp_80"
                android:background="@drawable/shape_orange_8"
                android:gravity="center"
                android:padding="@dimen/dp_20"
                android:src="@mipmap/icon_hot_city" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:orientation="vertical"
                android:paddingLeft="@dimen/dp_16">

                <TextView
                    android:id="@+id/tv_hot_city_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="巴黎"
                    android:textColor="@color/black_3"
                    android:textSize="@dimen/sp_16"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/tv_cnty_and_area"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dp_8"
                    android:text="巴黎"
                    android:textColor="@color/gray"
                    android:textSize="@dimen/sp_14"
                    android:textStyle="bold" />

            </LinearLayout>
            <!--增加id-->
            <ImageView
                android:id="@+id/iv_open"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/dp_12"
                android:src="@mipmap/icon_open_orange" />

        </LinearLayout>
    </androidx.cardview.widget.CardView>
</LinearLayout>

圖標也是要修改的
在這裏插入圖片描述
因爲是白色的你看不見很正常,你把頁面的主題改成黑色就可以看到了。
icon_hot_city_china.ping
在這裏插入圖片描述
icon_open_blue.png
在這裏插入圖片描述

⑤ 修改列表適配器

修改HotCityAdapter

package com.llw.goodweather.adapter;

import android.widget.ImageView;

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

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

/**
 * 熱門城市列表適配器
 */
public class HotCityAdapter extends BaseQuickAdapter<HotCityResponse.HeWeather6Bean.BasicBean, BaseViewHolder> {

    private int mType;

    //  增加一個item樣式類型,在Activity中傳入
    public HotCityAdapter(int layoutResId, @Nullable List<HotCityResponse.HeWeather6Bean.BasicBean> data,int type) {
        super(layoutResId, data);
        this.mType = type;
    }

    @Override
    protected void convert(BaseViewHolder helper, HotCityResponse.HeWeather6Bean.BasicBean item) {
        ImageView ivMark = helper.getView(R.id.iv_mark);
        ImageView ivOpen = helper.getView(R.id.iv_open);
        if (mType == 0) {//國內
            ivMark.setBackground(mContext.getResources().getDrawable(R.drawable.shape_blue_8));//背景
            ivMark.setImageDrawable(mContext.getDrawable(R.mipmap.icon_hot_city_china));//圖標
            ivOpen.setImageDrawable(mContext.getDrawable(R.mipmap.icon_open_blue));//圖標

        } else {//國外
            ivMark.setBackground(mContext.getResources().getDrawable(R.drawable.shape_orange_8));//背景
            ivMark.setImageDrawable(mContext.getDrawable(R.mipmap.icon_hot_city));//圖標
            ivOpen.setImageDrawable(mContext.getDrawable(R.mipmap.icon_open_orange));//圖標
        }

        helper.setText(R.id.tv_hot_city_name,item.getLocation())
                .setText(R.id.tv_cnty_and_area,item.getCnty()+" —— "+item.getAdmin_area());
        helper.addOnClickListener(R.id.item_hot_city);
    }

}

⑥ 樣式調整

然後回到HotCityActivity中首先是在initList方法中增加一個入參
在這裏插入圖片描述
這樣傳入的類型就會影響到適配器中的樣式了,最後一步就是在getHotCityResult方法中對返回值中做數據的處理了。
在這裏插入圖片描述
新增部分的代碼如下:

			toolbar.setNavigationIcon(getResources().getDrawable(R.mipmap.icon_return_white));//返回箭頭顏色
            tvTitle.setTextColor(getResources().getColor(R.color.white));//標題顏色

            if (type == 0) {//標題判斷
                tvTitle.setText("國內熱門城市");
                StatusBarUtil.setStatusBarColor(context, R.color.blue_one);//藍色底狀態欄
                toolbar.setBackgroundColor(getResources().getColor(R.color.blue_one));//標題  藍色
                layBg.setBackgroundColor(getResources().getColor(R.color.shallow_blue));//背景 藍色
            } else {
                tvTitle.setText("海外熱門城市");
                StatusBarUtil.setStatusBarColor(context, R.color.orange);//橙色底  狀態欄
                toolbar.setBackgroundColor(getResources().getColor(R.color.orange));//標題  橙色
                layBg.setBackgroundColor(getResources().getColor(R.color.shallow_orange));//背景  橙色
            }

運行一下,看看效果
在這裏插入圖片描述

下一篇:Android 天氣APP(十八)常用城市

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章