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

相关文章
|
JavaScript 前端开发 Android开发
【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
807 13
【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
|
Android开发 数据安全/隐私保护 开发者
Android自定义view之模仿登录界面文本输入框(华为云APP)
本文介绍了一款自定义输入框的实现,包含静态效果、hint值浮动动画及功能扩展。通过组合多个控件完成界面布局,使用TranslateAnimation与AlphaAnimation实现hint文字上下浮动效果,支持密码加密解密显示、去除键盘回车空格输入、光标定位等功能。代码基于Android平台,提供完整源码与attrs配置,方便复用与定制。希望对开发者有所帮助。
401 0
|
数据采集 JSON 网络安全
移动端数据抓取:Android App的TLS流量解密方案
本文介绍了一种通过TLS流量解密技术抓取知乎App热榜数据的方法。利用Charles Proxy解密HTTPS流量,分析App与服务器通信内容;结合Python Requests库模拟请求,配置特定请求头以绕过反爬机制。同时使用代理IP隐藏真实IP地址,确保抓取稳定。最终成功提取热榜标题、内容简介、链接等信息,为分析热点话题和用户趋势提供数据支持。此方法也可应用于其他Android App的数据采集,但需注意选择可靠的代理服务。
840 11
移动端数据抓取:Android App的TLS流量解密方案
|
存储 Android开发 数据安全/隐私保护
Thanox安卓系统增加工具下载,管理、阻止、限制后台每个APP运行情况
Thanox是一款Android系统管理工具,专注于权限、后台启动及运行管理。支持应用冻结、系统优化、UI自定义和模块管理,基于Xposed框架开发,安全可靠且开源免费,兼容Android 6.0及以上版本。
2073 4
|
JavaScript 搜索推荐 Android开发
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
823 8
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
|
数据采集 JavaScript Android开发
【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
937 7
【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
|
Dart 前端开发 Android开发
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
794 4
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
缓存 开发工具 Android开发
Android App性能评测分析-启动时间篇
1、前言 随着项目版本的迭代,App的性能问题会逐渐暴露出来,而好的用户体验与性能表现紧密相关,性能问题从应用的启动优化开始,下面会根据实际app性能测试案例,进行app性能评测之启动时间的分析和总结。
5022 0
|
Shell Linux 测试技术
Android App性能评测分析-cpu占用篇
1、前言 很多时候在使用APP的时候,手机可能会发热发烫。这是因为CPU使用率过高,CPU过于繁忙,会使整个手机无法响应用户,整体性能降低,用户体验就会很差,也容易引起ANR等等一系列问题。
6671 0
|
Java 测试技术 程序员
Android App性能评测分析-内存篇
1、内存了解 在Android App的性能优化的各个部分里,内存方面的知识较多且不易理解,内存的问题绝对是最令人头疼的一部分,需要对内存基础知识、内存分配、内存管理机制等非常熟悉,才能排查问题。
3047 0

热门文章

最新文章