Android 天气APP(十七)热门城市 - 国内城市

简介: Android 天气APP(十七)热门城市 - 国内城市

前言


在上一篇做了国外的热门城市数据的展示,这一篇就简单一些,增加国内的热门城市。

效果图


image.gif


正文


① 修改API


在ApiService中修改hotCity这个接口,将固定地址里面的group分离出来,作为请求参数。


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


② 修改订阅器


在HotCityContract中增加一个参数

image.png


③ 创建选择弹窗


之前是在HotActivity中默认查询海外热门城市的,现在增加了一个参数,就需要用户去手动选择了,我们可以通过一个弹窗来进行选择。


在layout下创建


20200624162312417.png


布局代码如下:


<?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方法中的入参


20200624162758462.png


将这个值放到外面就可以在调用的时候设置是否可以点击空白处关闭弹窗,为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中初始化


20200624163608376.png


上图中标出来的就是新增的,然后创建一个显示弹窗的方法


  /**
     * 显示选择类型弹窗
     */
    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所有的生命周期方法全部执行完成才能显示,所以这里新开一个线程用于显示


20200624163845777.png


④ 修改列表item布局


弹窗搞定之后就可以改动热门城市的列表item布局了,首先增加一个颜色


20200624164204179.png

20200624164355396.png


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>


图标也是要修改的


20200624164557259.png


因为是白色的你看不见很正常,你把页面的主题改成黑色就可以看到了。


icon_hot_city_china.ping


20200624164552458.png


icon_open_blue.png


20200624164552460.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方法中增加一个入参


20200624165437946.png


这样传入的类型就会影响到适配器中的样式了,最后一步就是在

getHotCityResult方法中对返回值中做数据的处理了。


20200624165659963.png


新增部分的代码如下:

      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));//背景  橙色
            }


运行一下,看看效果


20200624170058320.gif

相关文章
|
16天前
|
XML Java 数据库
安卓项目:app注册/登录界面设计
本文介绍了如何设计一个Android应用的注册/登录界面,包括布局文件的创建、登录和注册逻辑的实现,以及运行效果的展示。
66 0
安卓项目:app注册/登录界面设计
|
1月前
|
Java 数据库 Android开发
一个Android App最少有几个线程?实现多线程的方式有哪些?
本文介绍了Android多线程编程的重要性及其实现方法,涵盖了基本概念、常见线程类型(如主线程、工作线程)以及多种多线程实现方式(如`Thread`、`HandlerThread`、`Executors`、Kotlin协程等)。通过合理的多线程管理,可大幅提升应用性能和用户体验。
74 15
一个Android App最少有几个线程?实现多线程的方式有哪些?
|
1月前
|
存储 开发工具 Android开发
使用.NET MAUI开发第一个安卓APP
【9月更文挑战第24天】使用.NET MAUI开发首个安卓APP需完成以下步骤:首先,安装Visual Studio 2022并勾选“.NET Multi-platform App UI development”工作负载;接着,安装Android SDK。然后,创建新项目时选择“.NET Multi-platform App (MAUI)”模板,并仅针对Android平台进行配置。了解项目结构,包括`.csproj`配置文件、`Properties`配置文件夹、平台特定代码及共享代码等。
|
1月前
|
XML Android开发 数据格式
🌐Android国际化与本地化全攻略!让你的App走遍全球无障碍!🌍
在全球化背景下,实现Android应用的国际化与本地化至关重要。本文以一款旅游指南App为例,详细介绍如何通过资源文件拆分与命名、适配布局与方向、处理日期时间及货币格式、考虑文化习俗等步骤,完成多语言支持和本地化调整。通过邀请用户测试并收集反馈,确保应用能无缝融入不同市场,提升用户体验与满意度。
62 3
|
15天前
|
安全 网络安全 Android开发
深度解析:利用Universal Links与Android App Links实现无缝网页至应用跳转的安全考量
【10月更文挑战第2天】在移动互联网时代,用户经常需要从网页无缝跳转到移动应用中。这种跳转不仅需要提供流畅的用户体验,还要确保安全性。本文将深入探讨如何利用Universal Links(仅限于iOS)和Android App Links技术实现这一目标,并分析其安全性。
74 0
|
1月前
|
Java 数据库 Android开发
一个Android App最少有几个线程?实现多线程的方式有哪些?
本文介绍了Android应用开发中的多线程编程,涵盖基本概念、常见实现方式及最佳实践。主要内容包括主线程与工作线程的作用、多线程的多种实现方法(如 `Thread`、`HandlerThread`、`Executors` 和 Kotlin 协程),以及如何避免内存泄漏和合理使用线程池。通过有效的多线程管理,可以显著提升应用性能和用户体验。
60 10
|
1月前
|
XML 数据库 Android开发
10分钟手把手教你用Android手撸一个简易的个人记账App
该文章提供了使用Android Studio从零开始创建一个简单的个人记账应用的详细步骤,包括项目搭建、界面设计、数据库处理及各功能模块的实现方法。
|
2月前
|
API Android开发
Android P 性能优化:创建APP进程白名单,杀死白名单之外的进程
本文介绍了在Android P系统中通过创建应用进程白名单并杀死白名单之外的进程来优化性能的方法,包括设置权限、获取运行中的APP列表、配置白名单以及在应用启动时杀死非白名单进程的代码实现。
57 1
|
2月前
|
Android开发 iOS开发 C#
Xamarin:用C#打造跨平台移动应用的终极利器——从零开始构建你的第一个iOS与Android通用App,体验前所未有的高效与便捷开发之旅
【8月更文挑战第31天】Xamarin 是一个强大的框架,允许开发者使用单一的 C# 代码库构建高性能的原生移动应用,支持 iOS、Android 和 Windows 平台。作为微软的一部分,Xamarin 充分利用了 .NET 框架的强大功能,提供了丰富的 API 和工具集,简化了跨平台移动应用开发。本文通过一个简单的示例应用介绍了如何使用 Xamarin.Forms 快速创建跨平台应用,包括设置开发环境、定义用户界面和实现按钮点击事件处理逻辑。这个示例展示了 Xamarin.Forms 的基本功能,帮助开发者提高开发效率并实现一致的用户体验。
119 0
|
2月前
|
存储 XML Linux
深入理解操作系统:进程管理与调度策略探索安卓应用开发:从零开始构建你的第一个App
【8月更文挑战第28天】在数字世界里航行,操作系统是掌控一切的舵手。本文将带你领略操作系统的精妙设计,特别是进程管理和调度策略这两大核心领域。我们将从基础概念出发,逐步深入到复杂的实现机制,最后通过实际代码示例,揭示操作系统如何高效协调资源,确保多任务顺畅运行的秘密。准备好了吗?让我们启航,探索那些隐藏在日常电脑使用背后的奥秘。 【8月更文挑战第28天】在这个数字时代,拥有一款自己的移动应用程序不仅是技术的展示,也是实现创意和解决问题的一种方式。本文将引导初学者了解安卓开发的基础知识,通过一个简单的待办事项列表App项目,逐步介绍如何利用安卓开发工具和语言来创建、测试并发布一个基本的安卓应用