admin 管理员组

文章数量: 887007

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

上一篇:Android 天气APP(七)城市切换 之 城市数据源

添加数据库、城市切换

  • 新版-------------------
    • 一、添加依赖和城市数据
    • 二、添加启动页
    • 三、城市数据操作
    • 四、切换城市
    • 五、切换城市处理
    • 六、文章源码
  • 旧版-------------------
    • 自定义弹窗

新版-------------------

  在上一篇文章中,完成了风力风向的显示,文章最后我添加了一个菜单,菜单里面只有一个选项,切换城市,本篇文章就是完成切换城市之后查询城市天气的功能,说起来是不是很简单,那么我们看一下实现这个功能要怎么做。

一、添加依赖和城市数据

  想一下,我们的城市数据怎么来,怎么保存和获取,在我之前的版本中,有一个txt文件,读取这个文件,然后显示在UI上,乍一看似乎可以使用,但是会有问题,性能损耗太大,每一次都需要重新进行文件读取,不优雅,这一块我们就用数据库来处理,只做一次文件读取,然后写入到数据库中,用的时候从数据库中读取即可,怎么保证只做一次文件读取呢,可以利用缓存值,在程序第一次运行时进行读取,后面就不再读取,现在想一下是不是比之前要复杂一些了,下面我们先添加依赖,在app的build.gralde的dependencies{}闭包下添加如下所示代码:

	//Room数据库implementation 'androidx.room:room-runtime:2.4.2'annotationProcessor 'androidx.room:room-compiler:2.4.2'//Room 支持RxJava2implementation 'androidx.room:room-rxjava2:2.4.2'//腾讯MMKVimplementation 'com.tencent:mmkv:1.2.11'//Gsonimplementation 'com.google.code.gson:gson:2.9.0'

添加位置如下图所示:

  然后Sync Now,下面准备城市数据源,在app模块的main文件夹下创建一个assets文件夹,里面放入一个city.txt,这里我就不贴里面的内容了,你可以去我的源码里面直接获取,如下图所示:

二、添加启动页

  一般程序会在启动页做一些事情,比如初始数据获取等一些操作,我们现在只有一个MainActivity,这明显是不够的,下面我们在com.llw.goodweather包下新建ui,ui包下新建一个SplashActivity,创建Activity的方式你应该会吧,为了方便管理,我们将MainActivity也移动到ui包下,移动之后检查一下MainActivity里面有没有报错,有的话就是ViewBinding的问题,你把错误的那一条语句删掉,重新导包就行了,然后就需要将SplashActivity作为启动页面了,修改AndroidManifest.xml中的代码,如下图所示:

  要注意细节,在Android 12中有一个android:exported属性,启动Activity必须为true,其他的就默认为false就行了,下面简单修改一下activity_splash.xml中的内容,代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/splash_bg"android:fitsSystemWindows="true"tools:context=".ui.SplashActivity"><ImageViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:fitsSystemWindows="true"android:scaleType="centerCrop"android:src="@drawable/splash_bg" />
</androidx.constraintlayout.widget.ConstraintLayout>

这里用到的背景图你可以去源码里面去找,那么你现在运行一下,应该就是显示SplashActivity了。

三、城市数据操作

  下面我们进行城市数据的操作,也就是从txt中读取、通过数据库保存、查询,这里我们用到的是Room数据库,首先我们在bean包下新建一个Province类,里面的代码如下:

@Entity
public class Province {@PrimaryKey(autoGenerate = true)private int uid;private String provinceName;private List<City> cityList;public int getUid() {return uid;}public void setUid(int uid) {this.uid = uid;}public String getProvinceName() {return provinceName;}public void setProvinceName(String provinceName) {this.provinceName = provinceName;}public List<City> getCityList() {return cityList;}public void setCityList(List<City> cityList) {this.cityList = cityList;}public Province() {}@Ignorepublic Province(String provinceName, List<City> cityList) {this.provinceName = provinceName;this.cityList = cityList;}public static class City {private String cityName;private List<Area> areaList;public String getCityName() {return cityName;}public void setCityName(String cityName) {this.cityName = cityName;}public List<Area> getAreaList() {return areaList;}public void setAreaList(List<Area> areaList) {this.areaList = areaList;}public City() {}public static class Area {private String areaName;public String getAreaName() {return areaName;}public void setAreaName(String areaName) {this.areaName = areaName;}public Area(String countyName) {this.areaName = countyName;}}}
}

  这里面用到的注解你记得导包,都是room下的,然后我们创建一个转换器,在bean包下新建一个CityConverter类,代码如下:

public class CityConverter {@TypeConverterpublic List<Province.City> stringToObject(String value) {Type userListType = new TypeToken<ArrayList<Province.City>>() {}.getType();return new Gson().fromJson(value, userListType);}@TypeConverterpublic String objectToString(List<Province.City> list) {return new Gson().toJson(list);}
}

  这里的注解导包同样是room下的,为什么要有这个转换器呢?因为我们是在一张表里面又插入了一张表,不用这个转换器就会报错。

然后我们再回到Province,在@Entity注解上面再添加一行注解,代码如下:

@TypeConverters(CityConverter.class)

添加位置如下图所示:

  下面我们在com.llw.goodweather包下新建一个db包,然后在db包下创建一个AppDatabase类,稍微我们会用到它,下面将bean包移到db包下,然后在db包下新建一个dao包,dao包中就是对于数据库操作的接口方法包,在dao包下新建一个ProvinceDao接口,代码如下所示:

@Dao
public interface ProvinceDao {/*** 查询所有*/@Query("SELECT * FROM Province")Flowable<List<Province>> getAll();/*** 插入所有* @param provinces 所有行政区数据*/@Insert(onConflict = OnConflictStrategy.REPLACE)Completable insertAll(Province... provinces);
}

  这里的Flowable和Completable 都是RxJava中的内容,背压,你可以去了解一下,下面回到AppDatabase类,修改代码如下所示:

@Database(entities = {Province.class},version = 1,exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {private static final String DATABASE_NAME = "GoodWeatherNew";private static volatile AppDatabase mInstance;public abstract ProvinceDao provinceDao();/*** 单例模式*/public static AppDatabase getInstance(Context context) {if (mInstance == null) {synchronized (AppDatabase.class) {if (mInstance == null) {mInstance = Room.databaseBuilder(context.getApplicationContext(),AppDatabase.class, DATABASE_NAME).build();}}}return mInstance;}
}

  注意看这是一个抽象类,我们通过注解会生成一个编译时类,然后将之前创建的Province当成一个表放进数据库,数据库版本为1,里面有一个抽象接口方法,还有一个单例,单例中做数据库的构建,下面关于数据库的操作就基本上完成了,看一下db包下的内容,如下图所示:

  那么我们在哪里调用呢?在Repository中,下面在repository包下新建一个CustomDisposable类,这里面是对数据的背压操作所创建的工具类,里面的代码如下所示:

public class CustomDisposable {private static final CompositeDisposable compositeDisposable = new CompositeDisposable();/*** Flowable* @param flowable* @param consumer* @param <T>*/public static <T> void addDisposable(Flowable<T> flowable, Consumer<T> consumer) {compositeDisposable.add(flowable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(consumer));}/*** Completable* @param completable* @param action* @param <T>*/public static <T> void addDisposable(Completable completable, Action action) {compositeDisposable.add(completable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(action));}
}

然后在repository包下新建一个CityRepository类,代码如下所示:

public class CityRepository {private static final String TAG = CityRepository.class.getSimpleName();private static final class CityRepositoryHolder {private static final CityRepository mInstance = new CityRepository();}public static CityRepository getInstance() {return CityRepository.CityRepositoryHolder.mInstance;}/*** 添加城市数据*/public void addCityData(List<Province> cityList) {Province[] provinceArray = cityList.toArray(new Province[0]);Completable insertAll = WeatherApp.getDb().provinceDao().insertAll(provinceArray);CustomDisposable.addDisposable(insertAll, () -> Log.d(TAG, "addCityData: 插入数据成功。"));}/*** 获取城市数据*/public void getCityData(MutableLiveData<List<Province>> listMutableLiveData) {Flowable<List<Province>> listFlowable = WeatherApp.getDb().provinceDao().getAll();CustomDisposable.addDisposable(listFlowable, listMutableLiveData::postValue);}
}

  然后就是调用这些方法的地方,根据MVVM的框架模式,我们可以在viewmodel包下新建一个SplashViewModel类,代码如下:

public class SplashViewModel extends BaseViewModel {public MutableLiveData<List<Province>> listMutableLiveData = new MutableLiveData<>();/*** 添加城市数据*/public void addCityData(List<Province> provinceList) {CityRepository.getInstance().addCityData(provinceList);}/*** 获取所有城市数据*/public void getAllCityData() {CityRepository.getInstance().getCityData(listMutableLiveData);}
}

下面在Constant中新增两个常量,代码如下:

	/*** 程序第一次运行*/public static final String FIRST_RUN = "firstRun";/*** 今天第一次启动时间*/public static final String FIRST_STARTUP_TIME_TODAY = "firstStartupTimeToday";

下面我们添加MMKV的使用,在utils包下新建一个MVUtils 类,代码如下所示:

public class MVUtils {private static MVUtils mInstance;private static MMKV mmkv;public MVUtils() {mmkv = MMKV.defaultMMKV();}public static MVUtils getInstance() {if (mInstance == null) {synchronized (MVUtils.class) {if (mInstance == null) {mInstance = new MVUtils();}}}return mInstance;}/*** 写入基本数据类型缓存** @param key    键* @param object 值*/public static void put(String key, Object object) {if (object instanceof String) {mmkv.encode(key, (String) object);} else if (object instanceof Integer) {mmkv.encode(key, (Integer) object);} else if (object instanceof Boolean) {mmkv.encode(key, (Boolean) object);} else if (object instanceof Float) {mmkv.encode(key, (Float) object);} else if (object instanceof Long) {mmkv.encode(key, (Long) object);} else if (object instanceof Double) {mmkv.encode(key, (Double) object);} else if (object instanceof byte[]) {mmkv.encode(key, (byte[]) object);} else {mmkv.encode(key, object.toString());}}public static void putSet(String key, Set<String> sets) {mmkv.encode(key, sets);}public static void putParcelable(String key, Parcelable obj) {mmkv.encode(key, obj);}public static Integer getInt(String key) {return mmkv.decodeInt(key, 0);}public static Integer getInt(String key, int defaultValue) {return mmkv.decodeInt(key, defaultValue);}public static Double getDouble(String key) {return mmkv.decodeDouble(key, 0.00);}public static Double getDouble(String key, double defaultValue) {return mmkv.decodeDouble(key, defaultValue);}public static Long getLong(String key) {return mmkv.decodeLong(key, 0L);}public static Long getLong(String key, long defaultValue) {return mmkv.decodeLong(key, defaultValue);}public static Boolean getBoolean(String key) {return mmkv.decodeBool(key, false);}public static Boolean getBoolean(String key, boolean defaultValue) {return mmkv.decodeBool(key, defaultValue);}public static Float getFloat(String key) {return mmkv.decodeFloat(key, 0F);}public static Float getFloat(String key, float defaultValue) {return mmkv.decodeFloat(key, defaultValue);}public static byte[] getBytes(String key) {return mmkv.decodeBytes(key);}public static byte[] getBytes(String key, byte[] defaultValue) {return mmkv.decodeBytes(key, defaultValue);}public static String getString(String key) {return mmkv.decodeString(key, "");}public static String getString(String key, String defaultValue) {return mmkv.decodeString(key, defaultValue);}public static Set<String> getStringSet(String key) {return mmkv.decodeStringSet(key, Collections.emptySet());}public static Parcelable getParcelable(String key) {return mmkv.decodeParcelable(key, null);}/*** 移除某个key对** @param key*/public static void removeKey(String key) {mmkv.removeValueForKey(key);}/*** 清除所有key*/public static void clearAll() {mmkv.clearAll();}
}

然后也要记得初始化,在WeatherApp中进行,代码如下:

public class WeatherApp extends BaseApplication {//数据库private static AppDatabase db;@Overridepublic void onCreate() {super.onCreate();//使用定位需要同意隐私合规政策LocationClient.setAgreePrivacy(true);//初始化网络框架NetworkApi.init(new NetworkRequiredInfo(this));//MMKV初始化MMKV.initialize(this);//工具类初始化MVUtils.getInstance();//初始化Room数据库db = AppDatabase.getInstance(this);}public static AppDatabase getDb() {return db;}
}

这里我同时也将数据库进行了初始化,爆红的地方导包就好了。最后我们修改一下SplashActivity中的代码如下所示:

@SuppressLint("CustomSplashScreen")
public class SplashActivity extends NetworkActivity<ActivitySplashBinding> {private SplashViewModel viewModel;private final String TAG = SplashActivity.class.getSimpleName();@Overrideprotected void onCreate() {setFullScreenImmersion();viewModel = new ViewModelProvider(this).get(SplashViewModel.class);//检查启动checkingStartup();//checkFirstRunToday();new Handler().postDelayed(() -> jumpActivityFinish(MainActivity.class), 1000);}/*** 检查启动*/private void checkingStartup() {if (MVUtils.getBoolean(Constant.FIRST_RUN, false)) return;//第一次运行,获取城市数据,没有就会去加载到数据库中viewModel.getAllCityData();MVUtils.put(Constant.FIRST_RUN, true);}/*** 检查今天第一次运行*/private void checkFirstRunToday() {long todayFirstRunTime = MVUtils.getLong(Constant.FIRST_STARTUP_TIME_TODAY);long currentTimeMillis = System.currentTimeMillis();long todayTwelveTimestamp = EasyDate.getTodayTwelveTimestamp();//满足更新启动时间的条件,1.为0表示没有保存过时间,2. 当前时间if (todayFirstRunTime == 0 || currentTimeMillis > todayFirstRunTime - (1000 * 60 * 10)) {MVUtils.put(Constant.FIRST_STARTUP_TIME_TODAY, currentTimeMillis);//今天第一次启动要做的事情}}@Overrideprotected void onObserveData() {//城市数据返回viewModel.listMutableLiveData.observe(this, provinceList -> {if (provinceList.size() == 0) {Log.d(TAG, "onObserveData: 第一次添加数据");//没有保存过数据,只需要保存一次即可。List<Province> provinces = loadCityData();if (provinces != null) viewModel.addCityData(provinces);} else {Log.d(TAG, "onObserveData: 有数据了");}});}/*** 加载本地的城市数据*/private List<Province> loadCityData() {List<Province> provinceList = new ArrayList<>();//读取城市数据InputStream inputStream;try {inputStream = getResources().getAssets().open("city.txt");BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));StringBuffer stringBuffer = new StringBuffer();String lines = bufferedReader.readLine();while (lines != null) {stringBuffer.append(lines);lines = bufferedReader.readLine();}final JSONArray jsonArray = new JSONArray(stringBuffer.toString());for (int i = 0; i < jsonArray.length(); i++) {Province province = new Province();List<Province.City> cityList = new ArrayList<>();//得到省份对象JSONObject provinceJsonObject = jsonArray.getJSONObject(i);province.setProvinceName(provinceJsonObject.getString("name"));//得到省份下的市数组JSONArray cityJsonArray = provinceJsonObject.getJSONArray("city");for (int j = 0; j < cityJsonArray.length(); j++) {Province.City city = new Province.City();List<Province.City.Area> areaList = new ArrayList<>();//得到市对象JSONObject cityJsonObject = cityJsonArray.getJSONObject(j);city.setCityName(cityJsonObject.getString("name"));//得到市下的区/县数组JSONArray areaJsonArray = cityJsonObject.getJSONArray("area");for (int k = 0; k < areaJsonArray.length(); k++) {areaList.add(new Province.City.Area(areaJsonArray.getString(k)));}cityList.add(city);city.setAreaList(areaList);}provinceList.add(province);province.setCityList(cityList);}return provinceList;} catch (IOException | JSONException e) {e.printStackTrace();}return null;}
}

  首先从上面开始,沉浸式、然后viewModel绑定,检查是否第一次启动,是就读取本地城市数据,保存到数据库中,不是就跳过,最后延迟1秒进入MainActivity,里面有一个关于今天第一次启动的方法,目前还用不到,后面会用到,EasyDate中没有getTodayTwelveTimestamp()方法,给它添加一个,代码如下所示:

	public static long getTodayTwelveTimestamp() {long zero = getTimestamp() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数long twelve = zero + 24 * 60 * 60 * 1000 - 1;//今天23点59分59秒的毫秒数return new Timestamp(twelve).getTime();}

现在你的这个SplashActivity应该没有报错的地方了,有的话,检查一下有没有导包,下面我们运行看看控制台。


OK,这说明我们的数据库保存和查询都没有问题,那么下面就是显示数据。

四、切换城市

  在之前的版本中,我将切换城市的代码也写在MainActivity,这样并不好,所以这一次我将代码抽离了出来,然后提供接口给MainActivity实现,来看看是怎么做的。

这里城市的切换我们分为三个级别,省/直辖市 市 区/县,所在我在utils包下新建了一个AdministrativeType 类,代码如下所示:

public enum AdministrativeType {PROVINCE,CITY,AREA
}

  然后将adapter包移到ui包下,报错的适配器重新导一下包,然后在adapter包下新建一个AdministrativeClickCallback接口,代码如下:

public interface AdministrativeClickCallback {/*** 行政区 点击事件** @param view     点击视图* @param position 点击位置* @param type     行政区类型*/void onAdministrativeItemClick(View view, int position, AdministrativeType type);
}

  因为涉及到三个行政区的数据列表点击事件,所以我单独写了一个接口来处理,通过type来做区分具体市哪一个,下面就是列表item的布局创建,在layout下新建一个item_text_rv.xml,代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android=""android:id="@+id/tv_text"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="@dimen/dp_1"android:background="@color/white"android:ellipsize="middle"android:foreground="?attr/selectableItemBackground"android:gravity="center"android:padding="@dimen/dp_16"android:singleLine="true"android:text="城市"android:textColor="@color/unselect_tx_color" />

  这里三个列表的适配器进行复用,可以显示行政区名字,下面我们创建适配器,在adapter包下新建一个ProvinceAdapter类,代码如下所示:

public class ProvinceAdapter extends RecyclerView.Adapter<ProvinceAdapter.ViewHolder> {private final List<Province> provinces;private AdministrativeClickCallback administrativeClickCallback;//视图点击public ProvinceAdapter(List<Province> provinces) {this.provinces = provinces;}public void setAdministrativeClickCallback(AdministrativeClickCallback administrativeClickCallback) {this.administrativeClickCallback = administrativeClickCallback;}@NonNull@Overridepublic ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {ItemTextRvBinding binding = ItemTextRvBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);ViewHolder viewHolder = new ViewHolder(binding);//添加视图点击事件binding.getRoot().setOnClickListener(v -> {if (administrativeClickCallback != null) {administrativeClickCallback.onAdministrativeItemClick(v, viewHolder.getAdapterPosition(), AdministrativeType.PROVINCE);}});return viewHolder;}@Overridepublic void onBindViewHolder(@NonNull ViewHolder holder, int position) {holder.binding.tvText.setText(provinces.get(position).getProvinceName());}@Overridepublic int getItemCount() {return provinces.size();}public static class ViewHolder extends RecyclerView.ViewHolder {public ItemTextRvBinding binding;public ViewHolder(@NonNull ItemTextRvBinding itemTextRvBinding) {super(itemTextRvBinding.getRoot());binding = itemTextRvBinding;}}
}

  这就是RecyclerView的常规用法,加上了ViewBinding和点击事件而已,没有什么好说明的,下面在adapter包下创建CityAdapter类,代码如下:

public class CityAdapter extends RecyclerView.Adapter<CityAdapter.ViewHolder> {private final List<Province.City> cities;private AdministrativeClickCallback administrativeClickCallback;//视图点击public CityAdapter(List<Province.City> cities) {this.cities = cities;}public void setAdministrativeClickCallback(AdministrativeClickCallback administrativeClickCallback) {this.administrativeClickCallback = administrativeClickCallback;}@NonNull@Overridepublic ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {ItemTextRvBinding binding = ItemTextRvBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);ViewHolder viewHolder = new ViewHolder(binding);//添加视图点击事件binding.getRoot().setOnClickListener(v -> {if (administrativeClickCallback != null) {administrativeClickCallback.onAdministrativeItemClick(v, viewHolder.getAdapterPosition(), AdministrativeType.CITY);}});return viewHolder;}@Overridepublic void onBindViewHolder(@NonNull ViewHolder holder, int position) {holder.binding.tvText.setText(cities.get(position).getCityName());}@Overridepublic int getItemCount() {return cities.size();}public static class ViewHolder extends RecyclerView.ViewHolder {public ItemTextRvBinding binding;public ViewHolder(@NonNull ItemTextRvBinding itemTextRvBinding) {super(itemTextRvBinding.getRoot());binding = itemTextRvBinding;}}
}

最后在adapter包下创建AreaAdapter类,代码如下所示:

public class AreaAdapter extends RecyclerView.Adapter<AreaAdapter.ViewHolder> {private final List<Province.City.Area> areas;private AdministrativeClickCallback administrativeClickCallback;//视图点击public AreaAdapter(List<Province.City.Area> areas) {this.areas = areas;}public void setAdministrativeClickCallback(AdministrativeClickCallback administrativeClickCallback) {this.administrativeClickCallback = administrativeClickCallback;}@NonNull@Overridepublic ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {ItemTextRvBinding binding = ItemTextRvBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);ViewHolder viewHolder = new ViewHolder(binding);//添加视图点击事件binding.getRoot().setOnClickListener(v -> {if (administrativeClickCallback != null) {administrativeClickCallback.onAdministrativeItemClick(v, viewHolder.getAdapterPosition(), AdministrativeType.AREA);}});return viewHolder;}@Overridepublic void onBindViewHolder(@NonNull ViewHolder holder, int position) {holder.binding.tvText.setText(areas.get(position).getAreaName());}@Overridepublic int getItemCount() {return areas.size();}public static class ViewHolder extends RecyclerView.ViewHolder {public ItemTextRvBinding binding;public ViewHolder(@NonNull ItemTextRvBinding itemTextRvBinding) {super(itemTextRvBinding.getRoot());binding = itemTextRvBinding;}}
}

  现在适配器有了,就需要一个使用它们的地方,那当然是RecyclerView了,在layout下创建一个dialog_city.xml布局,代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""xmlns:app=""android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/line_color"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="52dp"android:background="@color/white"android:gravity="center_vertical"><!--关闭--><ImageButtonandroid:id="@+id/iv_close"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@null"android:padding="@dimen/dp_16"android:src="@drawable/ic_round_close_24" /><TextViewandroid:id="@+id/tv_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:ellipsize="middle"android:gravity="center"android:padding="@dimen/dp_16"android:singleLine="true"android:text="请选择"android:textColor="@color/black"android:textSize="@dimen/sp_16"android:textStyle="bold" /><!--确定--><ImageButtonandroid:id="@+id/iv_submit"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@null"android:padding="@dimen/dp_16"android:src="@drawable/ic_round_check_24"android:visibility="invisible" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginTop="1dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintTop_toBottomOf="@+id/materialToolbar"><!--省/直辖市列表--><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/rv_province"android:layout_width="@dimen/dp_0"android:layout_height="match_parent"android:layout_weight="1" /><!--市列表--><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/rv_city"android:layout_width="@dimen/dp_0"android:layout_height="match_parent"android:layout_marginStart="@dimen/dp_1"android:layout_marginEnd="@dimen/dp_1"android:layout_weight="1"android:visibility="gone" /><!--区/县列表--><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/rv_area"android:layout_width="@dimen/dp_0"android:layout_height="match_parent"android:layout_weight="1"android:visibility="gone" /></LinearLayout>
</LinearLayout>

这里面有两个小图标,在drawable下创建ic_round_close_24.xml,代码如下:

<vector xmlns:android=""android:width="24dp"android:height="24dp"android:tint="#000000"android:viewportWidth="24"android:viewportHeight="24"><pathandroid:fillColor="@android:color/white"android:pathData="M18.3,5.71c-0.39,-0.39 -1.02,-0.39 -1.41,0L12,10.59 7.11,5.7c-0.39,-0.39 -1.02,-0.39 -1.41,0 -0.39,0.39 -0.39,1.02 0,1.41L10.59,12 5.7,16.89c-0.39,0.39 -0.39,1.02 0,1.41 0.39,0.39 1.02,0.39 1.41,0L12,13.41l4.89,4.89c0.39,0.39 1.02,0.39 1.41,0 0.39,-0.39 0.39,-1.02 0,-1.41L13.41,12l4.89,-4.89c0.38,-0.38 0.38,-1.02 0,-1.4z" />
</vector>

再创建ic_round_check_24.xml,代码如下:

<vector xmlns:android=""android:width="24dp"android:height="24dp"android:tint="#000000"android:viewportWidth="24"android:viewportHeight="24"><pathandroid:fillColor="@android:color/white"android:pathData="M9,16.17L5.53,12.7c-0.39,-0.39 -1.02,-0.39 -1.41,0 -0.39,0.39 -0.39,1.02 0,1.41l4.18,4.18c0.39,0.39 1.02,0.39 1.41,0L20.29,7.71c0.39,-0.39 0.39,-1.02 0,-1.41 -0.39,-0.39 -1.02,-0.39 -1.41,0L9,16.17z" />
</vector>

对了,还有两个颜色值,在colors.xml中增加如下代码:

	<color name="line_color">#EEEEEE</color><color name="tx_color">#201E1E</color>

现在xml应该不会报错了,下面在utils包下新建一个CityDialog类,代码如下所示:

public class CityDialog implements AdministrativeClickCallback {@SuppressLint("StaticFieldLeak")private static volatile CityDialog mInstance;@SuppressLint("StaticFieldLeak")private static Context mContext;private BottomSheetDialog dialog;private DialogCityBinding binding;private final List<Province> provinceList = new ArrayList<>();private final List<Province.City> cityList = new ArrayList<>();private final List<Province.City.Area> areaList = new ArrayList<>();private final CityAdapter cityAdapter = new CityAdapter(cityList);private final AreaAdapter areaAdapter = new AreaAdapter(areaList);private SelectedCityCallback selectedCityCallback;private String provinceName, cityName, areaName;public CityDialog(Context context, List<Province> provinces) {mContext = context;provinceList.clear();provinceList.addAll(provinces);}public static CityDialog getInstance(Context context, List<Province> provinces) {if (mInstance == null) {synchronized (CityDialog.class) {if (mInstance == null) {mInstance = new CityDialog(context, provinces);}}}return mInstance;}/*** 设置选中城市回调*/public void setSelectedCityCallback(SelectedCityCallback selectedCityCallback) {this.selectedCityCallback = selectedCityCallback;}/*** 显示弹窗*/public void show() {dialog = new BottomSheetDialog(mContext);binding = DialogCityBinding.inflate(LayoutInflater.from(mContext), null, false);ProvinceAdapter provinceAdapter = new ProvinceAdapter(provinceList);provinceAdapter.setAdministrativeClickCallback(this);cityAdapter.setAdministrativeClickCallback(this);areaAdapter.setAdministrativeClickCallback(this);binding.rvProvince.setLayoutManager(new LinearLayoutManager(mContext));binding.rvProvince.setAdapter(provinceAdapter);binding.rvCity.setLayoutManager(new LinearLayoutManager(mContext));binding.rvCity.setAdapter(cityAdapter);binding.rvArea.setLayoutManager(new LinearLayoutManager(mContext));binding.rvArea.setAdapter(areaAdapter);binding.ivClose.setOnClickListener(v -> dialog.dismiss());binding.ivSubmit.setOnClickListener(v -> {if (selectedCityCallback != null) {selectedCityCallback.selectedCity(areaName);dialog.dismiss();}});dialog.setContentView(binding.getRoot());dialog.show();}public void dismiss() {if (dialog != null) {dialog.dismiss();}}@Overridepublic void onAdministrativeItemClick(View view, int position, AdministrativeType type) {switch (type) {case PROVINCE:cityList.clear();cityList.addAll(provinceList.get(position).getCityList());cityAdapter.notifyDataSetChanged();binding.rvCity.setVisibility(View.VISIBLE);binding.rvArea.setVisibility(View.GONE);provinceName = provinceList.get(position).getProvinceName();cityName = null;areaName = null;break;case CITY:areaList.clear();areaList.addAll(cityList.get(position).getAreaList());areaAdapter.notifyDataSetChanged();binding.rvArea.setVisibility(View.VISIBLE);cityName = cityList.get(position).getCityName();areaName = null;break;case AREA:areaName = areaList.get(position).getAreaName();break;}binding.ivSubmit.setVisibility(areaName == null ? View.INVISIBLE : View.VISIBLE);binding.tvTitle.setText(provinceName);if (cityName == null) return;binding.tvTitle.setText(provinceName + " > " + cityName);if (areaName == null) return;binding.tvTitle.setText(provinceName + " > " + cityName + " > " + areaName);}public interface SelectedCityCallback {void selectedCity(String cityName);}
}

  这里的代码就是加载获取到的城市数据,然后显示出来,再点击Item是根据区域类型做不同的处理,结合UI效果去进行操作就可以了。

五、切换城市处理

因为我们需要在MainActivity中获取城市数据,那么就需要修改一下MainViewModel,添加代码如下:

	public MutableLiveData<List<Province>> cityMutableLiveData = new MutableLiveData<>();public void getAllCity() {CityRepository.getInstance().getCityData(cityMutableLiveData);}

下面我们回到MainActivity中,声明变量:

	//城市弹窗private CityDialog cityDialog;

然后就在onCreate()中获取城市数据,如下图所示:

在onObserveData()方法中增加如下代码:

			viewModel.cityMutableLiveData.observe(this, provinces -> {//城市弹窗初始化cityDialog = CityDialog.getInstance(MainActivity.this, provinces);cityDialog.setSelectedCityCallback(this);});

这里我们就对弹窗进行了初始化,然后实现接口

在MainActivity中重写selectedCity()方法,代码如下:

	@Overridepublic void selectedCity(String cityName) {//搜索城市viewModel.searchCity(cityName);//显示所选城市binding.tvCity.setText(cityName);}

下面我们运行一下:

六、文章源码

欢迎 StarFork

第八篇文章源码地址:GoodWeather-New-8

旧版-------------------

自定义弹窗

既然是弹窗,那就不能让它平白无奇的出现,给一个动画效果,闪亮登场,完美谢幕。

in_bottom_to_top.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android=""android:duration="500"android:fromYDelta="100%p"android:interpolator="@android:anim/accelerate_interpolator"android:toYDelta="0%p" ></translate>

in_right_to_left.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android=""android:duration="500"android:fromXDelta="100%p"android:interpolator="@android:anim/accelerate_interpolator"android:toXDelta="0%p" ></translate>

out_left_to_right.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android=""android:duration="500"android:fromXDelta="0%p"android:toXDelta="100%p"android:interpolator="@android:anim/accelerate_interpolator" ></translate>

out_top_to_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android=""android:duration="500"android:fromYDelta="0%p"android:toYDelta="100%p"android:interpolator="@android:anim/accelerate_interpolator" ></translate>

然后在模块的styles.xml中增加样式

<!--弹窗样式--><style name="AnimationRightFade"><!--右侧--><item name="android:windowEnterAnimation">@anim/in_right_to_left</item>  <!--打开动画--><item name="android:windowExitAnimation">@anim/out_left_to_right</item>  <!--关闭动画--></style><style name="AnimationBottomFade"><!--底部--><item name="android:windowEnterAnimation">@anim/in_bottom_to_top</item><item name="android:windowExitAnimation">@anim/out_top_to_bottom</item></style>

然后在模块的utils包中新建一个LiWindow类

代码如下:

package com.llw.mvplibrary.utils;import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import com.llw.mvplibrary.R;
import java.util.HashMap;
import java.util.Map;/*** 自定义弹窗*/
public class LiWindow {private LiWindow mLiWindow;private PopupWindow mPopupWindow;private LayoutInflater inflater;private View mView;private Context mContext;private WindowManager show;WindowManager.LayoutParams context;private Map<String,Object> mMap = new HashMap<>();public Map<String, Object> getmMap() {return mMap;}public LiWindow(Context context){this.mContext = context;inflater = LayoutInflater.from(context);mLiWindow = this;}public LiWindow(Context context, Map<String,Object> map){this.mContext = context;this.mMap = map;inflater = LayoutInflater.from(context);}/*** 右侧显示  自适应大小* @param mView*/public void showRightPopupWindow(View mView) {mPopupWindow = new PopupWindow(mView,ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT , true);mPopupWindow.setContentView(mView);mPopupWindow.setOutsideTouchable(true);//点击空白处不关闭弹窗  true为关闭mPopupWindow.setFocusable(true);mPopupWindow.setAnimationStyle(R.style.AnimationRightFade); //设置动画mPopupWindow.showAtLocation(mView, Gravity.RIGHT,0 ,0);setBackgroundAlpha(0.5f,mContext);WindowManager.LayoutParams nomal = ((Activity) mContext).getWindow().getAttributes();nomal.alpha = 0.5f;((Activity) mContext).getWindow().setAttributes(nomal);mPopupWindow.setOnDismissListener(closeDismiss);}/*** 右侧显示  高度占满父布局* @param mView*/public void showRightPopupWindowMatchParent(View mView) {mPopupWindow = new PopupWindow(mView,ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT , true);mPopupWindow.setContentView(mView);mPopupWindow.setOutsideTouchable(true);//点击空白处不关闭弹窗  true为关闭mPopupWindow.setFocusable(true);mPopupWindow.setAnimationStyle(R.style.AnimationRightFade); //设置动画mPopupWindow.showAtLocation(mView, Gravity.RIGHT,0 ,0);setBackgroundAlpha(0.5f,mContext);WindowManager.LayoutParams nomal = ((Activity) mContext).getWindow().getAttributes();nomal.alpha = 0.5f;((Activity) mContext).getWindow().setAttributes(nomal);mPopupWindow.setOnDismissListener(closeDismiss);}/*** 底部显示* @param mView*/public void showBottomPopupWindow(View mView) {mPopupWindow = new PopupWindow(mView,ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);mPopupWindow.setContentView(mView);mPopupWindow.setOutsideTouchable(true);//点击空白处不关闭弹窗  true为关闭mPopupWindow.setFocusable(true);mPopupWindow.setAnimationStyle(R.style.AnimationBottomFade); //设置动画mPopupWindow.showAtLocation(mView, Gravity.BOTTOM, 0, 0);setBackgroundAlpha(0.5f,mContext);WindowManager.LayoutParams nomal = ((Activity) mContext).getWindow().getAttributes();nomal.alpha = 0.5f;((Activity) mContext).getWindow().setAttributes(nomal);mPopupWindow.setOnDismissListener(closeDismiss);}public static void setBackgroundAlpha(float bgAlpha,Context mContext){WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();lp.alpha = bgAlpha;((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);((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() {@Overridepublic 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=""xmlns:app=""android:orientation="vertical"android:fitsSystemWindows="true"android:layout_width="wrap_content"android:layout_height="wrap_content"><LinearLayoutandroid:orientation="vertical"android:background="#FFF"android:layout_width="240dp"android:layout_height="match_parent"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><androidx.appcompat.widget.Toolbarandroid:layout_width="match_parent"android:layout_height="?attr/actionBarSize"app:contentInsetLeft="16dp"app:popupTheme="@style/AppTheme.PopupOverlay"><TextViewandroid: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><!--城市列表的返回--><ImageViewandroid: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"/><!--区/县列表的返回--><ImageViewandroid: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"/></RelativeLayout><Viewandroid:layout_width="match_parent"android:layout_height="0.5dp"android:background="#EEEEEE"/><!--数据展示--><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/rv"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout>
</RelativeLayout>

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

代码如下

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android=""><item android:state_pressed="true"><shape android:shape="rectangle"><solid android:color="#18ffc400"/></shape></item><item android:state_focused="true" android:state_enabled="true"><shape android:shape="rectangle"><solid android:color="#0f000000"/></shape></item>
</selector>

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

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns: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=""android:id="@+id/item_city"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#FFF"android:orientation="vertical"><!--显示省 、 市 、 区/县--><TextViewandroid: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" /><!--分隔线--><Viewandroid:layout_width="match_parent"android:layout_height="0.5dp"android:background="#EEEEEE"/>
</LinearLayout>

接下来就是要创建一个实体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<CityBean> city;public String getName() {return name;}public void setName(String name) {this.name = name;}public List<CityBean> getCity() {return city;}public void setCity(List<CityBean> city) {this.city = city;}public static class CityBean {/*** name : 北京市* area : ["东城区","西城区","崇文区","宣武区","朝阳区","丰台区","石景山区","海淀区","门头沟区","房山区","通州区","顺义区","昌平区","大兴区","平谷区","怀柔区","密云县","延庆县"]*/private String name;private List<AreaBean> 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<CityResponse> data) {super(layoutResId, data);}@Overrideprotected 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);}@Overrideprotected 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);}@Overrideprotected 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<String> list;//字符串列表private List<CityResponse> 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=""android:duration="500"><translateandroid:interpolator="@android:anim/accelerate_decelerate_interpolator"android:fromYDelta="50%p"android:toYDelta="0"/><alphaandroid:fromAlpha="0"android:toAlpha="1"android:interpolator="@android:anim/accelerate_decelerate_interpolator" /></set>

item_animation_from_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android=""android:duration="500"><translateandroid:interpolator="@android:anim/decelerate_interpolator"android:fromXDelta="100%p"android:toXDelta="0"/><alphaandroid:fromAlpha="0.5"android:toAlpha="1"android:interpolator="@android:anim/accelerate_decelerate_interpolator"/></set>

layout_animation_from_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimationxmlns: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"?>
<layoutAnimationxmlns: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);//加载城市列表数据
	/*** 省市县数据渲染* @param recyclerView  列表* @param areaBack 区县返回* @param cityBack 市返回* @param windowTitle  窗口标题*/private void initCityData(RecyclerView recyclerView,ImageView areaBack,ImageView cityBack,TextView windowTitle) {//初始化省数据 读取省数据并显示到列表中try {InputStream inputStream = getResources().getAssets().open("City.txt");//读取数据BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));StringBuffer stringBuffer = new StringBuffer();String lines = bufferedReader.readLine();while (lines != null) {stringBuffer.append(lines);lines = bufferedReader.readLine();}final JSONArray Data = new JSONArray(stringBuffer.toString());//循环这个文件数组、获取数组中每个省对象的名字for (int i = 0; i < Data.length(); i++) {JSONObject provinceJsonObject = Data.getJSONObject(i);String provinceName = provinceJsonObject.getString("name");CityResponse response = new CityResponse();response.setName(provinceName);provinceList.add(response);}//定义省份显示适配器provinceAdapter = new ProvinceAdapter(R.layout.item_city_list, provinceList);LinearLayoutManager manager = new LinearLayoutManager(context);recyclerView.setLayoutManager(manager);recyclerView.setAdapter(provinceAdapter);provinceAdapter.notifyDataSetChanged();runLayoutAnimationRight(recyclerView);//动画展示provinceAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {@Overridepublic void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {try {//返回上一级数据cityBack.setVisibility(View.VISIBLE);cityBack.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {recyclerView.setAdapter(provinceAdapter);provinceAdapter.notifyDataSetChanged();cityBack.setVisibility(View.GONE);windowTitle.setText("中国");}});//根据当前位置的省份所在的数组位置、获取城市的数组JSONObject provinceObject = Data.getJSONObject(position);windowTitle.setText(provinceList.get(position).getName());provinceTitle = provinceList.get(position).getName();final JSONArray cityArray = provinceObject.getJSONArray("city");//更新列表数据if (citylist != null) {citylist.clear();}for (int i = 0; i < cityArray.length(); i++) {JSONObject cityObj = cityArray.getJSONObject(i);String cityName = cityObj.getString("name");CityResponse.CityBean response = new CityResponse.CityBean();response.setName(cityName);citylist.add(response);}cityAdapter = new CityAdapter(R.layout.item_city_list, citylist);LinearLayoutManager manager1 = new LinearLayoutManager(context);recyclerView.setLayoutManager(manager1);recyclerView.setAdapter(cityAdapter);cityAdapter.notifyDataSetChanged();runLayoutAnimationRight(recyclerView);cityAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {@Overridepublic void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {try {//返回上一级数据areaBack.setVisibility(View.VISIBLE);areaBack.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {recyclerView.setAdapter(cityAdapter);cityAdapter.notifyDataSetChanged();areaBack.setVisibility(View.GONE);windowTitle.setText(provinceTitle);arealist.clear();}});//根据当前城市数组位置 获取地区数据windowTitle.setText(citylist.get(position).getName());JSONObject cityJsonObj = cityArray.getJSONObject(position);JSONArray areaJsonArray = cityJsonObj.getJSONArray("area");if (arealist != null) {arealist.clear();}if(list != null){list.clear();}for (int i = 0; i < areaJsonArray.length(); i++) {list.add(areaJsonArray.getString(i));}Log.i("list", list.toString());for (int j = 0; j < list.size(); j++) {CityResponse.CityBean.AreaBean response = new CityResponse.CityBean.AreaBean();response.setName(list.get(j).toString());arealist.add(response);}areaAdapter = new AreaAdapter(R.layout.item_city_list, arealist);LinearLayoutManager manager2 = new LinearLayoutManager(context);recyclerView.setLayoutManager(manager2);recyclerView.setAdapter(areaAdapter);areaAdapter.notifyDataSetChanged();runLayoutAnimationRight(recyclerView);areaAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {@Overridepublic void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {mPresent.todayWeather(context,arealist.get(position).getName());//今日天气mPresent.weatherForecast(context, arealist.get(position).getName());//天气预报mPresent.lifeStyle(context, arealist.get(position).getName());//生活指数liWindow.closePopupWindow();}});} catch (JSONException e) {e.printStackTrace();}}});} catch (JSONException e) {e.printStackTrace();}}});} catch (IOException e) {e.printStackTrace();} catch (JSONException e) {e.printStackTrace();}}

运行一下:




可以看到城市已经切换好了,数据也拿到了。

源码地址:GoodWeather
欢迎 StarFork

下一篇:Android 天气APP(九)细节优化、必应每日一图

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