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

本文描述了一个Android应用中关于布局调整(如添加id和颜色)、弹窗功能(如`showCenterPopupWindow`方法和列表item样式修改)、以及列表适配器(`HotCityAdapter`)的更新过程,重点在于如何实现在页面启动时显示弹窗并管理其关闭行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

然后就是使用了,我修改了一下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”/>

修改的布局有什么变化呢?就是里面的布局增加了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”>

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

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

</androidx.cardview.widget.CardView>

图标也是要修改的

在这里插入图片描述

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

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

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

总结

现在新技术层出不穷,如果每次出新的技术,我们都深入的研究的话,很容易分散精力。新的技术可能很久之后我们才会在工作中用得上,当学的新技术无法学以致用,很容易被我们遗忘,到最后真的需要使用的时候,又要从头来过(虽然上手会更快)。

我觉得身为技术人,针对新技术应该是持拥抱态度的,入了这一行你就应该知道这是一个活到老学到老的行业,所以面对新技术,不要抵触,拥抱变化就好了。

Flutter 明显是一种全新的技术,而对于这个新技术在发布之初,花一个月的时间学习它,成本确实过高。但是周末花一天时间体验一下它的开发流程,了解一下它的优缺点、能干什么或者不能干什么。这个时间,并不是我们不能接受的。

如果有时间,其实通读一遍 Flutter 的文档,是最全面的一次对 Flutter 的了解过程。但是如果我们只有 8 小时的时间,我希望能关注一些最值得关注的点。

(跨平台开发(Flutter)、java基础与原理,自定义view、NDK、架构设计、性能优化、完整商业项目开发等)

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

得身为技术人,针对新技术应该是持拥抱态度的,入了这一行你就应该知道这是一个活到老学到老的行业,所以面对新技术,不要抵触,拥抱变化就好了。

Flutter 明显是一种全新的技术,而对于这个新技术在发布之初,花一个月的时间学习它,成本确实过高。但是周末花一天时间体验一下它的开发流程,了解一下它的优缺点、能干什么或者不能干什么。这个时间,并不是我们不能接受的。

如果有时间,其实通读一遍 Flutter 的文档,是最全面的一次对 Flutter 的了解过程。但是如果我们只有 8 小时的时间,我希望能关注一些最值得关注的点。

(跨平台开发(Flutter)、java基础与原理,自定义view、NDK、架构设计、性能优化、完整商业项目开发等)

[外链图片转存中…(img-0yAuAPTo-1713751626441)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值