Android 天气APP(十四)修复UI显示异常、优化业务代码逻辑、增加详情天气显示(1)

public void setWind_spd(String wind_spd) {

this.wind_spd = wind_spd;

}

}

public static class DailyForecastBean {

/**

  • cond_code_d : 101

  • cond_code_n : 104

  • cond_txt_d : 多云

  • cond_txt_n : 阴

  • date : 2020-05-16

  • hum : 83

  • mr : 01:57

  • ms : 13:36

  • pcpn : 0.0

  • pop : 4

  • pres : 998

  • sr : 05:42

  • ss : 18:57

  • tmp_max : 34

  • tmp_min : 24

  • uv_index : 9

  • vis : 25

  • wind_deg : -1

  • wind_dir : 无持续风向

  • wind_sc : 1-2

  • wind_spd : 6

*/

private String cond_code_d;

private String cond_code_n;

private String cond_txt_d;

private String cond_txt_n;

private String date;

private String hum;

private String mr;

private String ms;

private String pcpn;

private String pop;

private String pres;

private String sr;

private String ss;

private String tmp_max;

private String tmp_min;

private String uv_index;

private String vis;

private String wind_deg;

private String wind_dir;

private String wind_sc;

private String wind_spd;

public String getCond_code_d() {

return cond_code_d;

}

public void setCond_code_d(String cond_code_d) {

this.cond_code_d = cond_code_d;

}

public String getCond_code_n() {

return cond_code_n;

}

public void setCond_code_n(String cond_code_n) {

this.cond_code_n = cond_code_n;

}

public String getCond_txt_d() {

return cond_txt_d;

}

public void setCond_txt_d(String cond_txt_d) {

this.cond_txt_d = cond_txt_d;

}

public String getCond_txt_n() {

return cond_txt_n;

}

public void setCond_txt_n(String cond_txt_n) {

this.cond_txt_n = cond_txt_n;

}

public String getDate() {

return date;

}

public void setDate(String date) {

this.date = date;

}

public String getHum() {

return hum;

}

public void setHum(String hum) {

this.hum = hum;

}

public String getMr() {

return mr;

}

public void setMr(String mr) {

this.mr = mr;

}

public String getMs() {

return ms;

}

public void setMs(String ms) {

this.ms = ms;

}

public String getPcpn() {

return pcpn;

}

public void setPcpn(String pcpn) {

this.pcpn = pcpn;

}

public String getPop() {

return pop;

}

public void setPop(String pop) {

this.pop = pop;

}

public String getPres() {

return pres;

}

public void setPres(String pres) {

this.pres = pres;

}

public String getSr() {

return sr;

}

public void setSr(String sr) {

this.sr = sr;

}

public String getSs() {

return ss;

}

public void setSs(String ss) {

this.ss = ss;

}

public String getTmp_max() {

return tmp_max;

}

public void setTmp_max(String tmp_max) {

this.tmp_max = tmp_max;

}

public String getTmp_min() {

return tmp_min;

}

public void setTmp_min(String tmp_min) {

this.tmp_min = tmp_min;

}

public String getUv_index() {

return uv_index;

}

public void setUv_index(String uv_index) {

this.uv_index = uv_index;

}

public String getVis() {

return vis;

}

public void setVis(String vis) {

this.vis = vis;

}

public String getWind_deg() {

return wind_deg;

}

public void setWind_deg(String wind_deg) {

this.wind_deg = wind_deg;

}

public String getWind_dir() {

return wind_dir;

}

public void setWind_dir(String wind_dir) {

this.wind_dir = wind_dir;

}

public String getWind_sc() {

return wind_sc;

}

public void setWind_sc(String wind_sc) {

this.wind_sc = wind_sc;

}

public String getWind_spd() {

return wind_spd;

}

public void setWind_spd(String wind_spd) {

this.wind_spd = wind_spd;

}

}

public static class HourlyBean {

/**

  • cloud : 11

  • cond_code : 101

  • cond_txt : 多云

  • dew : 22

  • hum : 73

  • pop : 7

  • pres : 998

  • time : 2020-05-16 13:00

  • tmp : 33

  • wind_deg : 174

  • wind_dir : 南风

  • wind_sc : 1-2

  • wind_spd : 5

*/

private String cloud;

private String cond_code;

private String cond_txt;

private String dew;

private String hum;

private String pop;

private String pres;

private String time;

private String tmp;

private String wind_deg;

private String wind_dir;

private String wind_sc;

private String wind_spd;

public String getCloud() {

return cloud;

}

public void setCloud(String cloud) {

this.cloud = cloud;

}

public String getCond_code() {

return cond_code;

}

public void setCond_code(String cond_code) {

this.cond_code = cond_code;

}

public String getCond_txt() {

return cond_txt;

}

public void setCond_txt(String cond_txt) {

this.cond_txt = cond_txt;

}

public String getDew() {

return dew;

}

public void setDew(String dew) {

this.dew = dew;

}

public String getHum() {

return hum;

}

public void setHum(String hum) {

this.hum = hum;

}

public String getPop() {

return pop;

}

public void setPop(String pop) {

this.pop = pop;

}

public String getPres() {

return pres;

}

public void setPres(String pres) {

this.pres = pres;

}

public String getTime() {

return time;

}

public void setTime(String time) {

this.time = time;

}

public String getTmp() {

return tmp;

}

public void setTmp(String tmp) {

this.tmp = tmp;

}

public String getWind_deg() {

return wind_deg;

}

public void setWind_deg(String wind_deg) {

this.wind_deg = wind_deg;

}

public String getWind_dir() {

return wind_dir;

}

public void setWind_dir(String wind_dir) {

this.wind_dir = wind_dir;

}

public String getWind_sc() {

return wind_sc;

}

public void setWind_sc(String wind_sc) {

this.wind_sc = wind_sc;

}

public String getWind_spd() {

return wind_spd;

}

public void setWind_spd(String wind_spd) {

this.wind_spd = wind_spd;

}

}

public static class LifestyleBean {

/**

  • type : comf

  • brf : 较不舒适

  • txt : 白天天气多云,并且空气湿度偏大,在这种天气条件下,您会感到有些闷热,不很舒适。

*/

private String type;

private String brf;

private String txt;

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public String getBrf() {

return brf;

}

public void setBrf(String brf) {

this.brf = brf;

}

public String getTxt() {

return txt;

}

public void setTxt(String txt) {

this.txt = txt;

}

}

}

}

在ApiService中增加

在这里插入图片描述

/**

  • 获取所有天气数据,在返回值中再做处理

  • @param location

  • @return

*/

@GET(“/s6/weather?key=3086e91d66c04ce588a7f538f917c7f4”)

Call weatherData(@Query(“location”) String location);

在WeatherContract中增加订阅

在这里插入图片描述

在这里插入图片描述

/**

  • 天气所有数据

  • @param context

  • @param location

*/

public void weatherData(final Context context,String location){

ApiService service = ServiceGenerator.createService(ApiService.class,0);

service.weatherData(location).enqueue(new NetCallBack() {

@Override

public void onSuccess(Call call, Response response) {

if(getView() != null){

getView().getWeatherDataResult(response);

}

}

@Override

public void onFailed() {

if(getView() != null){

getView().getWeatherDataFailed();

}

}

});

}

//查询天气所有数据

void getWeatherDataResult(Response response);

//天气数据获取错误返回

void getWeatherDataFailed();

进入MainActivity.java中

在这里插入图片描述

/**

  • 所有天气数据返回

  • @param response

*/

@Override

public void getWeatherDataResult(Response response) {

dismissLoadingDialog();//关闭弹窗

if ((“ok”).equals(response.body().getHeWeather6().get(0).getStatus())) {

}else {

ToastUtils.showShortToast(context, CodeToStringUtils.WeatherCode(response.body().getHeWeather6().get(0).getStatus()));

}

}

首先改动适配器

七天天气预报适配器WeatherForecastAdapter.java

在这里插入图片描述

逐小时天气预报适配器WeatherHourlyAdapter.java

在这里插入图片描述

然后回到MainActivity.java

在这里插入图片描述

红框中为新增的,然后之前的注释掉,当所有的改动都完成之后,再删除掉注释的没有用的代码、

在这里插入图片描述

红框中的就是你要改动的

然后修改切换城市之后的方法请求

在这里插入图片描述

定位后

在这里插入图片描述

下拉刷新之后

在这里插入图片描述

onResume方法中

在这里插入图片描述

在这里插入图片描述

返回值里面的业务逻辑处理代码先注释掉,最后修改getWeatherDataResult。

修改代码如下:

/**

  • 所有天气数据返回

  • @param response

*/

@Override

public void getWeatherDataResult(Response response) {

refresh.finishRefresh();//关闭刷新

dismissLoadingDialog();//关闭弹窗

if (mLocationClient != null) {

mLocationClient.stop();//数据返回后关闭定位

}

if ((“ok”).equals(response.body().getHeWeather6().get(0).getStatus())) {

if (response.body().getHeWeather6().get(0).getBasic() != null) {//得到数据不为空则进行数据显示

/**

  • 当天天气

*/

tvTemperature.setText(response.body().getHeWeather6().get(0).getNow().getTmp());//温度

if (flag) {

ivLocation.setVisibility(View.VISIBLE);//显示定位图标

} else {

ivLocation.setVisibility(View.GONE);//显示定位图标

}

tvCity.setText(response.body().getHeWeather6().get(0).getBasic().getLocation());//城市

tvInfo.setText(response.body().getHeWeather6().get(0).getNow().getCond_txt());//天气状况

//修改上次更新时间的结果显示 -> 更加人性化

String datetime = response.body().getHeWeather6().get(0).getUpdate().getLoc();//赋值

String time = datetime.substring(11);//截去前面的字符,保留后面所有的字符,就剩下 22:00

tvOldTime.setText(“上次更新时间:” + WeatherUtil.showTimeInfo(time) + time);

tvWindDirection.setText("风向 " + response.body().getHeWeather6().get(0).getNow().getWind_dir());//风向

tvWindPower.setText("风力 " + response.body().getHeWeather6().get(0).getNow().getWind_sc() + “级”);//风力

wwBig.startRotate();//大风车开始转动

wwSmall.startRotate();//小风车开始转动

/**

  • 查询7天天气预报

*/

//最低温和最高温

tvLowHeight.setText(response.body().getHeWeather6().get(0).getDaily_forecast().get(0).getTmp_min() + " / " +

response.body().getHeWeather6().get(0).getDaily_forecast().get(0).getTmp_max() + “℃”);

if (response.body().getHeWeather6().get(0).getDaily_forecast() != null) {

List<WeatherResponse.HeWeather6Bean.DailyForecastBean> data

= response.body().getHeWeather6().get(0).getDaily_forecast();

mListDailyForecast.clear();//添加数据之前先清除

mListDailyForecast.addAll(data);//添加数据

mAdapter.notifyDataSetChanged();//刷新列表

} else {

ToastUtils.showShortToast(context, “天气预报数据为空”);

}

/**

  • 查询逐小时天气数据

*/

if (response.body().getHeWeather6().get(0).getHourly() != null) {

List<WeatherResponse.HeWeather6Bean.HourlyBean> data

= response.body().getHeWeather6().get(0).getHourly();

mListHourlyBean.clear();//添加数据之前先清除

mListHourlyBean.addAll(data);//添加数据

mAdapterHourly.notifyDataSetChanged();//刷新列表

} else {

ToastUtils.showShortToast(context, “逐小时预报数据为空”);

}

/**

  • 生活指数

*/

List<WeatherResponse.HeWeather6Bean.LifestyleBean> data = response.body().getHeWeather6().get(0).getLifestyle();

if (!ObjectUtils.isEmpty(data)) {

for (int i = 0; i < data.size(); i++) {

switch (data.get(i).getType()){

case “uv”:

tvUv.setText(“紫外线:” + data.get(i).getTxt());

break;

case “comf”:

tvComf.setText(“舒适度:” + data.get(i).getTxt());

break;

case “drsg”:

tvDrsg.setText(“穿衣指数:” + data.get(i).getTxt());

break;

case “flu”:

tvFlu.setText(“感冒指数:” + data.get(i).getTxt());

break;

case “sport”:

tvSport.setText(“运动指数:” + data.get(i).getTxt());

break;

case “trav”:

tvTrav.setText(“旅游指数:” + data.get(i).getTxt());

break;

case “cw”:

tvCw.setText(“洗车指数:” + data.get(i).getTxt());

break;

case “air”:

tvAir.setText(“空气指数:” + data.get(i).getTxt());

break;

}

}

} else {

ToastUtils.showShortToast(context, “生活指数数据为空”);

}

}else {

ToastUtils.showShortToast(context,“天气数据为空”);

}

}else {

ToastUtils.showShortToast(context, CodeToStringUtils.WeatherCode(response.body().getHeWeather6().get(0).getStatus()));

}

改到这里就是初步改完了,运行一下:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

很明显,和之前什么变化都没有,但是,里面的业务处理方式发生了改变,之前是一次性请求四次接口,现在是变成一个接口了,无疑在机制上有了优化,现在就可以注释掉其他的代码了。

打开ApiService

在这里插入图片描述

这里将之前的四个API注释掉。

再打开订阅器

在这里插入图片描述

在这里插入图片描述

然后再回到MainActivity

在这里插入图片描述

将这四个报错的返回方法注释掉,这是你的页面是不会有报错的,然后再运行一下,确保没有问题。好了,已经运行过没有问题了,那么最后就是删除掉注释的代码了。

在这里插入图片描述

这四个实体bean也可以删除了,删除之后再运行一下,OK,没有问题。也算是为应用做了一次瘦身了,下面就是优化一下用户的体验。

优化UI


增加逐小时天气预报详情和七天天气预报详情,

这个我打算用弹窗来做,首先是逐小时天气预报的详情,创建一个弹窗的背景样式

在项目的drawable下创建shape_white_5.xml文件

样式代码如下:

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

① 逐小时天气预报的详情UI

在项目的layout文件下创建window_hourly_detail.xml文件

布局代码如下:

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

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

android:orientation=“vertical”

android:background=“@drawable/shape_white_5”

android:padding=“@dimen/dp_12”

android:layout_width=“300dp”

android:layout_height=“400dp”>

<TextView

android:gravity=“center_vertical”

android:textColor=“@color/black”

android:textSize=“@dimen/sp_16”

android:textStyle=“bold”

android:id=“@+id/tv_time”

android:layout_width=“match_parent”

android:layout_height=“@dimen/dp_40”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“温度”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_tem”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“天气状况”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_cond_txt”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“风向360角度”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_wind_deg”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“风向”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_wind_dir”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“风力”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_wind_sc”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“风速”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_wind_spd”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“相对湿度”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_hum”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“大气压强”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_pres”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“降水概率”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_pop”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“露点温度”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_dew”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“云量”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_cloud”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

由于这一次的弹窗是屏幕中间出现,所以增加动画效果和代码,在mvplibrary模块下的anim文件夹中增加出现和隐藏的动画xml文件

在这里插入图片描述

window_hidden_anim.xml

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

<scale

android:duration=“300”

android:fromXScale=“1.0”

android:fromYScale=“1.0”

android:pivotX=“50%”

android:pivotY=“50%”

android:toXScale=“0.5”

android:toYScale=“0.5” />

<alpha

android:duration=“300”

android:fromAlpha=“1.0”

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

android:toAlpha=“0.0” />

window_show_anim.xml

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

<scale

android:duration=“300”

android:fromXScale=“0.6”

android:fromYScale=“0.6”

android:pivotX=“50%”

android:pivotY=“50%”

android:toXScale=“1.0”

android:toYScale=“1.0” />

<alpha

android:duration=“300”

android:fromAlpha=“0.0”

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

android:toAlpha=“1.0” />

然后在styles.xml文件中添加

在这里插入图片描述

这里还会用到一个测量的工具类SizeUtils.java

代码如下:

package com.llw.mvplibrary.utils;

import android.content.Context;

import android.util.DisplayMetrics;

import android.util.TypedValue;

import android.view.View;

import android.view.ViewGroup;

/**

  • dp sp px 转换 view 测量工个类

*/

public final class SizeUtils {

private SizeUtils() {

throw new UnsupportedOperationException(“u can’t instantiate me…”);

}

/**

  • Value of dp to value of px.

  • @param dpValue The value of dp.

  • @return value of px

*/

public static int dp2px(Context context, final float dpValue) {

final float scale = context.getApplicationContext().getResources().getDisplayMetrics().density;

return (int) (dpValue * scale + 0.5f);

}

/**

  • Value of px to value of dp.

  • @param pxValue The value of px.

  • @return value of dp

*/

public static int px2dp(Context context, final float pxValue) {

final float scale = context.getApplicationContext().getResources().getDisplayMetrics().density;

return (int) (pxValue / scale + 0.5f);

}

/**

  • Value of sp to value of px.

  • @param spValue The value of sp.

  • @return value of px

*/

public static int sp2px(Context context, final float spValue) {

final float fontScale = context.getApplicationContext().getResources().getDisplayMetrics().scaledDensity;

return (int) (spValue * fontScale + 0.5f);

}

/**

  • Value of px to value of sp.

  • @param pxValue The value of px.

  • @return value of sp

*/

public static int px2sp(Context context, final float pxValue) {

final float fontScale = context.getApplicationContext().getResources().getDisplayMetrics().scaledDensity;

return (int) (pxValue / fontScale + 0.5f);

}

/**

  • Converts an unpacked complex data value holding a dimension to its final floating

  • point value. The two parameters unit and value

  • are as in {@link TypedValue#TYPE_DIMENSION}.

  • @param value The value to apply the unit to.

  • @param unit The unit to convert from.

  • @return The complex floating point value multiplied by the appropriate

  • metrics depending on its unit.

*/

public static float applyDimension(Context context, final float value, final int unit) {

DisplayMetrics metrics = context.getApplicationContext().getResources().getDisplayMetrics();

switch (unit) {

case TypedValue.COMPLEX_UNIT_PX:

return value;

case TypedValue.COMPLEX_UNIT_DIP:

return value * metrics.density;

case TypedValue.COMPLEX_UNIT_SP:

return value * metrics.scaledDensity;

case TypedValue.COMPLEX_UNIT_PT:

return value * metrics.xdpi * (1.0f / 72);

case TypedValue.COMPLEX_UNIT_IN:

return value * metrics.xdpi;

case TypedValue.COMPLEX_UNIT_MM:

return value * metrics.xdpi * (1.0f / 25.4f);

}

return 0;

}

/**

  • Force get the size of view.

  • e.g.

  • SizeUtils.forceGetViewSize(view, new SizeUtils.onGetSizeListener() {

  • Override
    
  • public void onGetSize(final View view) {
    
  •     view.getWidth();
    
  • }
    
  • });

  • @param view The view.

  • @param listener The get size listener.

*/

public static void forceGetViewSize(final View view, final onGetSizeListener listener) {

view.post(new Runnable() {

@Override

public void run() {

if (listener != null) {

listener.onGetSize(view);

}

}

});

}

/**

  • Return the width of view.

  • @param view The view.

  • @return the width of view

*/

public static int getMeasuredWidth(final View view) {

return measureView(view)[0];

}

/**

  • Return the height of view.

  • @param view The view.

  • @return the height of view

*/

public static int getMeasuredHeight(final View view) {

return measureView(view)[1];

}

/**

  • Measure the view.

  • @param view The view.

  • @return arr[0]: view’s width, arr[1]: view’s height

*/

public static int[] measureView(final View view) {

ViewGroup.LayoutParams lp = view.getLayoutParams();

if (lp == null) {

lp = new ViewGroup.LayoutParams(

ViewGroup.LayoutParams.MATCH_PARENT,

ViewGroup.LayoutParams.WRAP_CONTENT

);

}

int widthSpec = ViewGroup.getChildMeasureSpec(0, 0, lp.width);

int lpHeight = lp.height;

int heightSpec;

if (lpHeight > 0) {

heightSpec = View.MeasureSpec.makeMeasureSpec(lpHeight, View.MeasureSpec.EXACTLY);

} else {

heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

}

view.measure(widthSpec, heightSpec);

return new int[]{view.getMeasuredWidth(), view.getMeasuredHeight()};

}

///

// interface

///

public interface onGetSizeListener {

void onGetSize(View view);

}

}

现在去LiWindow.java中增加中间显示的代码.

在这里插入图片描述

增加的代码我也贴一下:

/**

  • 中间显示

  • @param mView

*/

public void showCenterPopupWindow(View mView,int width,int height) {

mPopupWindow = new PopupWindow(mView,

width, height, true);

mPopupWindow.setContentView(mView);

mPopupWindow.setAnimationStyle(R.style.AnimationCenterFade); //设置动画

mPopupWindow.showAtLocation(mView, Gravity.CENTER, 0, 0);

mPopupWindow.update();

setBackgroundAlpha(0.5f,mContext);

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

nomal.alpha = 0.5f;

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

mPopupWindow.setOnDismissListener(closeDismiss);

}

现在修改item_weather_hourly_list.xml,增加了一个id和点击的水波纹效果,代码如下,复制粘贴即可:

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

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

android:orientation=“vertical”

android:gravity=“center_horizontal”

android:id=“@+id/item_hourly”

android:foreground=“@drawable/bg_white”

android:padding=“8dp”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”>

<TextView

android:id=“@+id/tv_time”

android:textColor=“#FFF”

android:text=“上午10:00”

android:textSize=“14sp”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”/>

<ImageView

android:layout_marginTop=“12dp”

android:layout_marginBottom=“8dp”

android:id=“@+id/iv_weather_state”

android:background=“@mipmap/icon_100”

android:layout_width=“30dp”

android:layout_height=“30dp”/>

<TextView

android:textSize=“20sp”

android:id=“@+id/tv_temperature”

android:textColor=“#FFF”

android:text=“25℃”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”/>

再修改WeatherHourlyAdapter.java适配器

在这里插入图片描述

添加点击事件的id。

进入MainActivity.java

在这里插入图片描述

这个代码就手写一下吧,也不多,然后是这个弹窗的方法

//显示逐三小时详情天气信息弹窗

private void showHourlyWindow(WeatherResponse.HeWeather6Bean.HourlyBean data) {

liWindow = new LiWindow(context);

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

TextView tv_time = view.findViewById(R.id.tv_time);//时间

TextView tv_tem = view.findViewById(R.id.tv_tem);//温度

TextView tv_cond_txt = view.findViewById(R.id.tv_cond_txt);//天气状态描述

TextView tv_wind_deg = view.findViewById(R.id.tv_wind_deg);//风向360角度

TextView tv_wind_dir = view.findViewById(R.id.tv_wind_dir);//风向

TextView tv_wind_sc = view.findViewById(R.id.tv_wind_sc);//风力

TextView tv_wind_spd = view.findViewById(R.id.tv_wind_spd);//风速

TextView tv_hum = view.findViewById(R.id.tv_hum);//相对湿度

TextView tv_pres = view.findViewById(R.id.tv_pres);//大气压强

TextView tv_pop = view.findViewById(R.id.tv_pop);//降水概率

TextView tv_dew = view.findViewById(R.id.tv_dew);//露点温度

TextView tv_cloud = view.findViewById(R.id.tv_cloud);//云量

String datetime = data.getTime();//赋值

String time = datetime.substring(11);//截去前面的字符,保留后面所有的字符,就剩下 22:00

tv_time.setText(WeatherUtil.showTimeInfo(time) + time);

tv_tem.setText(data.getTmp() + “℃”);

tv_cond_txt.setText(data.getCond_txt());

tv_wind_deg.setText(data.getWind_deg()+“°”);

tv_wind_dir.setText(data.getWind_dir());

tv_wind_sc.setText(data.getWind_sc()+“级”);

tv_wind_spd.setText(data.getWind_spd() + “公里/小时”);

tv_hum.setText(data.getHum());

tv_pres.setText(data.getPres());

tv_pop.setText(data.getPop() + “%”);

tv_dew.setText(data.getDew()+“℃”);

tv_cloud.setText(data.getCloud());

liWindow.showCenterPopupWindow(view, SizeUtils.dp2px(context, 300),SizeUtils.dp2px(context, 400));

}

OK,到目前为止,可以运行一下了

在这里插入图片描述

OK,还是蛮简单的吧(PS:由于没有UI,大家这个都知道开发的审美,就先将就一下,如果有好的建议可以给我提)。

② 未来七天天气的详情UI

其实这个和逐小时的比较类似,不过要比逐小时的数据要多一些,

在项目的layout文件下创建window_forecast_detail.xml文件

布局代码如下:

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

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

android:orientation=“vertical”

android:background=“@drawable/shape_white_5”

android:padding=“@dimen/dp_12”

android:layout_width=“300dp”

android:layout_height=“500dp”>

<TextView

android:gravity=“center_vertical”

android:textColor=“@color/black”

android:textSize=“@dimen/sp_16”

android:textStyle=“bold”

android:id=“@+id/tv_datetime”

android:layout_width=“match_parent”

android:layout_height=“@dimen/dp_40”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“最高温”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_tmp_max”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“最低温”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_tmp_min”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“紫外线强度”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_uv_index”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“白天天气状况”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_cond_txt_d”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“晚上天气状况”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_cond_txt_n”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“风向360角度”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_wind_deg”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“风向”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_wind_dir”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“风力”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_wind_sc”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“风速”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_wind_spd”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“相对湿度”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_hum”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“大气压强”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_pres”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“降水量”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_pcpn”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“降水概率”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_pop”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“能见度”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_vis”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

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

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

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

img

img

img

img

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

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

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

总结

这次面试问的还是还是有难度的,要求当场写代码并且运行,也是很考察面试者写代码
因为Android知识体系比较庞大和复杂的,涉及到计算机知识领域的方方面面。在这里我和身边一些朋友特意整理了一份快速进阶为Android高级工程师的系统且全面的学习资料。涵盖了Android初级——Android高级架构师进阶必备的一些学习技能。

附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…

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

"

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_pres”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“降水量”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_pcpn”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“降水概率”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_pop”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<LinearLayout

android:gravity=“center_vertical”

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<TextView

android:text=“能见度”

android:textColor=“@color/black”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:textColor=“@color/black”

android:id=“@+id/tv_vis”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

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

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

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

[外链图片转存中…(img-tUQa8MHv-1713751918280)]

[外链图片转存中…(img-8wZZBaGS-1713751918281)]

[外链图片转存中…(img-EL9KkbeD-1713751918282)]

[外链图片转存中…(img-dRaJRBwV-1713751918284)]

[外链图片转存中…(img-oiiGhRoJ-1713751918285)]

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

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

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

[外链图片转存中…(img-NsgcE23Z-1713751918286)]

总结

这次面试问的还是还是有难度的,要求当场写代码并且运行,也是很考察面试者写代码
因为Android知识体系比较庞大和复杂的,涉及到计算机知识领域的方方面面。在这里我和身边一些朋友特意整理了一份快速进阶为Android高级工程师的系统且全面的学习资料。涵盖了Android初级——Android高级架构师进阶必备的一些学习技能。

附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
[外链图片转存中…(img-RlHeWN82-1713751918287)]
里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…

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

  • 17
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值