Android 天气APP(八)城市切换 之 自定义弹窗与使用

((Activity) mContext).getWindow().setAttributes(lp);

}

/**

  • 设置弹窗动画

  • @param animId

  • @return showPopu

*/

public LiWindow setAnim(int animId) {

if (mPopupWindow != null) {

mPopupWindow.setAnimationStyle(animId);

}

return mLiWindow;

}

//弹窗消失时关闭阴影

public PopupWindow.OnDismissListener closeDismiss = new PopupWindow.OnDismissListener() {

@Override

public void onDismiss() {

WindowManager.LayoutParams nomal = ((Activity)mContext).getWindow().getAttributes();

nomal.alpha = 1f;

((Activity)mContext).getWindow().setAttributes(nomal);

}

};

public void closePopupWindow() {

if (mPopupWindow != null) {

mPopupWindow.dismiss();

}

}

/*

使用方法

  • LiWindow liWindow = new LiWindow(MainActivity.this);

View mView = LayoutInflater.from(MainActivity.this).inflate(R.layout.center_layout,null);

liWindow.showCenterPopupWindow(mView);

  • */

}

弹窗也是需要布局文件的,现在创建一个新的布局文件,用于显示城市列表。

返回图标:

在这里插入图片描述

在项目的layout下创建一个名为window_city_list.xml的布局文件

代码如下:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

android:orientation=“vertical”

android:fitsSystemWindows=“true”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”>

<LinearLayout

android:orientation=“vertical”

android:background=“#FFF”

android:layout_width=“240dp”

android:layout_height=“match_parent”>

<RelativeLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”>

<androidx.appcompat.widget.Toolbar

android:layout_width=“match_parent”

android:layout_height=“?attr/actionBarSize”

app:contentInsetLeft=“16dp”

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=“16sp”

android:textColor=“#000”

android:text=“中国” />

</androidx.appcompat.widget.Toolbar>

<ImageView

android:visibility=“gone”

android:layout_marginLeft=“@dimen/dp_10”

android:layout_centerVertical=“true”

android:id=“@+id/iv_back_city”

android:src=“@mipmap/icon_page_return”

android:padding=“15dp”

android:layout_width=“40dp”

android:layout_height=“40dp”/>

<ImageView

android:visibility=“gone”

android:layout_marginLeft=“@dimen/dp_10”

android:layout_centerVertical=“true”

android:id=“@+id/iv_back_area”

android:src=“@mipmap/icon_page_return”

android:padding=“15dp”

android:layout_width=“40dp”

android:layout_height=“40dp”/>

<View

android:layout_width=“match_parent”

android:layout_height=“0.5dp”

android:background=“#EEEEEE”/>

<androidx.recyclerview.widget.RecyclerView

android:id=“@+id/rv”

android:layout_width=“match_parent”

android:layout_height=“match_parent”/>

为了让点击的时候有一个效果,在模块的res文件下的drawable下创建一个rounded_corners.xml的样式文件,点击的水波纹效果

在这里插入图片描述

代码如下

<?xml version="1.0" encoding="utf-8"?>

接下来在res文件下下新建一个drawable-v21的文件夹,文件夹下创建一个bg_white.xml

在这里插入图片描述

代码如下:

<?xml version="1.0" encoding="utf-8"?>

<ripple xmlns:android=“http://schemas.android.com/apk/res/android”

android:color=“#20000000”

android:drawable=“@drawable/rounded_corners”/>

点击的样式做好了,接下来创建城市列表的item

在项目的layout文件夹下创建一个名为item_city_list.xml的布局文件

在这里插入图片描述

代码如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:id=“@+id/item_city”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:background=“#FFF”

android:orientation=“vertical”>

<TextView

android:id=“@+id/tv_city”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:foreground=“@drawable/bg_white”

android:gravity=“center”

android:padding=“10dp”

android:textColor=“#FF000000”

android:textSize=“15sp” />

<View

android:layout_width=“match_parent”

android:layout_height=“0.5dp”

android:background=“#EEEEEE”/>

接下来就是要创建一个实体Bean用来接收JSON中解析出来的城市数据,里面包含了省、市、区/县

在项目的bean包下新建一个CityResponse

在这里插入图片描述代码如下:

package com.llw.goodweather.bean;

import java.util.List;

public class CityResponse {

/**

  • name : 北京市

  • city : [{“name”:“北京市”,“area”:[“东城区”,“西城区”,“崇文区”,“宣武区”,“朝阳区”,“丰台区”,“石景山区”,“海淀区”,“门头沟区”,“房山区”,“通州区”,“顺义区”,“昌平区”,“大兴区”,“平谷区”,“怀柔区”,“密云县”,“延庆县”]}]

*/

private String name;

private List city;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public List getCity() {

return city;

}

public void setCity(List city) {

this.city = city;

}

public static class CityBean {

/**

  • name : 北京市

  • area : [“东城区”,“西城区”,“崇文区”,“宣武区”,“朝阳区”,“丰台区”,“石景山区”,“海淀区”,“门头沟区”,“房山区”,“通州区”,“顺义区”,“昌平区”,“大兴区”,“平谷区”,“怀柔区”,“密云县”,“延庆县”]

*/

private String name;

private List area;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public static class AreaBean {

/**

  • name : 北京市

  • area : [“东城区”,“西城区”,“崇文区”,“宣武区”,“朝阳区”,“丰台区”,“石景山区”,“海淀区”,“门头沟区”,“房山区”,“通州区”,“顺义区”,“昌平区”,“大兴区”,“平谷区”,“怀柔区”,“密云县”,“延庆县”]

*/

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

}

}

接下来创建适配器,需要三个适配器,省、市、区/县。在adapter包下创建ProvinceAdapterCityAdapterAreaAdapter

在这里插入图片描述

ProvinceAdapter.java

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

import java.util.List;

/**

  • 省列表适配器

*/

public class ProvinceAdapter extends BaseQuickAdapter<CityResponse, BaseViewHolder> {

public ProvinceAdapter(int layoutResId, @Nullable List data) {

super(layoutResId, data);

}

@Override

protected void convert(BaseViewHolder helper, CityResponse item) {

helper.setText(R.id.tv_city,item.getName());//省名称

helper.addOnClickListener(R.id.item_city);//点击之后进入市级列表

}

}

CityAdapter.java

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

import java.util.List;

/**

  • 市列表适配器

*/

public class CityAdapter extends BaseQuickAdapter<CityResponse.CityBean, BaseViewHolder> {

public CityAdapter(int layoutResId, @Nullable List<CityResponse.CityBean> data) {

super(layoutResId, data);

}

@Override

protected void convert(BaseViewHolder helper, CityResponse.CityBean item) {

helper.setText(R.id.tv_city,item.getName());//市名称

helper.addOnClickListener(R.id.item_city);//点击事件 点击进入区/县列表

}

}

AreaAdapter.java

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

import java.util.List;

/**

  • 区/县列表适配器

*/

public class AreaAdapter extends BaseQuickAdapter<CityResponse.CityBean.AreaBean, BaseViewHolder> {

public AreaAdapter(int layoutResId, @Nullable List<CityResponse.CityBean.AreaBean> data) {

super(layoutResId, data);

}

@Override

protected void convert(BaseViewHolder helper, CityResponse.CityBean.AreaBean item) {

helper.setText(R.id.tv_city,item.getName());//区/县的名称

helper.addOnClickListener(R.id.item_city);//点击事件 点击之后得到区/县 然后查询天气数据

}

}

万事具备了,接下来就是在MainActivity.java里面实现这个城市弹窗数据的渲染了。

在这里插入图片描述

private List list;//字符串列表

private List provinceList;//省列表数据

private List<CityResponse.CityBean> citylist;//市列表数据

private List<CityResponse.CityBean.AreaBean> arealist;//区/县列表数据

ProvinceAdapter provinceAdapter;//省数据适配器

CityAdapter cityAdapter;//市数据适配器

AreaAdapter areaAdapter;//县/区数据适配器

String provinceTitle;//标题

LiWindow liWindow;//自定义弹窗

使用弹窗

在这里插入图片描述

/**

  • 城市弹窗

*/

private void showCityWindow() {

provinceList = new ArrayList<>();

citylist = new ArrayList<>();

arealist = new ArrayList<>();

list = new ArrayList<>();

liWindow = new LiWindow(context);

final View view = LayoutInflater.from(context).inflate(R.layout.window_city_list, null);

ImageView areaBack = (ImageView) view.findViewById(R.id.iv_back_area);

ImageView cityBack = (ImageView) view.findViewById(R.id.iv_back_city);

TextView windowTitle = (TextView) view.findViewById(R.id.tv_title);

RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rv);

liWindow.showRightPopupWindow(view);

}

//点击事件

@OnClick(R.id.iv_city_select)

public void onViewClicked() {//显示城市弹窗

showCityWindow();

}

接下来就是花里胡哨的操作了,首先我希望我的列表市动画展示出来的。

先创建动画文件,在模块中的anim文件

加下

在这里插入图片描述

item_animation_from_bottom.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android=“http://schemas.android.com/apk/res/android”

android:duration=“500”>

<translate

android:interpolator=“@android:anim/accelerate_decelerate_interpolator”

android:fromYDelta=“50%p”

android:toYDelta=“0”/>

<alpha

android:fromAlpha=“0”

android:toAlpha=“1”

android:interpolator=“@android:anim/accelerate_decelerate_interpolator” />

item_animation_from_right.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android=“http://schemas.android.com/apk/res/android”

android:duration=“500”>

<translate

android:interpolator=“@android:anim/decelerate_interpolator”

android:fromXDelta=“100%p”

android:toXDelta=“0”/>

<alpha

android:fromAlpha=“0.5”

android:toAlpha=“1”

android:interpolator=“@android:anim/accelerate_decelerate_interpolator”/>

layout_animation_from_bottom.xml

<?xml version="1.0" encoding="utf-8"?>

<layoutAnimation

xmlns:android=“http://schemas.android.com/apk/res/android”

android:animation=“@anim/item_animation_from_bottom”

android:delay=“15%”

android:animationOrder=“normal” />

layout_animation_slide_right.xml

<?xml version="1.0" encoding="utf-8"?>

<layoutAnimation

xmlns:android=“http://schemas.android.com/apk/res/android”

android:animation=“@anim/item_animation_from_right”

android:delay=“10%”

android:animationOrder=“normal”

/>

工具类

在模块的utils包下创建RecyclerViewAnimation

在这里插入图片描述

RecyclerViewAnimation.java

代码如下:

package com.llw.mvplibrary.utils;

import android.content.Context;

import android.view.animation.AnimationUtils;

import android.view.animation.LayoutAnimationController;

import androidx.recyclerview.widget.RecyclerView;

import com.llw.mvplibrary.R;

/**

  • 动画RecycleView

*/

public class RecyclerViewAnimation {

//数据变化时显示动画 底部动画

public static void runLayoutAnimation(final RecyclerView recyclerView) {

final Context context = recyclerView.getContext();

final LayoutAnimationController controller =

AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation_from_bottom);

recyclerView.setLayoutAnimation(controller);

recyclerView.getAdapter().notifyDataSetChanged();

recyclerView.scheduleLayoutAnimation();

}

//数据变化时显示动画 右侧动画

public static void runLayoutAnimationRight(final RecyclerView recyclerView) {

final Context context = recyclerView.getContext();

final LayoutAnimationController controller =

AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation_slide_right);

recyclerView.setLayoutAnimation(controller);

recyclerView.getAdapter().notifyDataSetChanged();

recyclerView.scheduleLayoutAnimation();

}

}

MainActivity.java代码中

在这里插入图片描述

initCityData(recyclerView,areaBack,cityBack,windowTitle);//加载城市列表数据

/**

  • 省市县数据渲染

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

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

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

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

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

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

学习分享

①「Android面试真题解析大全」PDF完整高清版+②「Android面试知识体系」学习思维导图压缩包

/**

  • 省市县数据渲染

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

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

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-hT5Igsun-1711909937327)]
[外链图片转存中…(img-FpMI0BeW-1711909937327)]
[外链图片转存中…(img-RMoBOoDk-1711909937328)]
[外链图片转存中…(img-qftwvlGT-1711909937328)]
[外链图片转存中…(img-eRPuhA5m-1711909937328)]
[外链图片转存中…(img-U7Ih00Ph-1711909937328)]
img

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

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

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-f6N9BJyf-1711909937329)]

学习分享

①「Android面试真题解析大全」PDF完整高清版+②「Android面试知识体系」学习思维导图压缩包

[外链图片转存中…(img-EVtmLakD-1711909937329)]

[外链图片转存中…(img-eRWSekS8-1711909937329)]

[外链图片转存中…(img-kHWq1mCo-1711909937330)]

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

  • 23
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值