1、通常来说,一般情况下,我们的app的BottomTab会有集中实现方式。

  1. 自定义view,然后自己写逻辑去实现互斥。

    • 自由度最高,因为啥都是自己写的。
  2. 使用RadioGroup+RadioButton去实现底部的Tab。
    • 自由度比极高,如果想实现搞复杂度的话可以重写RadioButton。
  3. 使用google design包里面的 TabLayout去实现。
    • 可上、可下、可以滑动
    • 偷懒的话可以根据已有api来设置一些资源,也可以setCustomView()
  4. 使用google design包里面的BottomNavigationView去实现。
    • 使用menu设置资源
    • 有默认的动画效果。

2、今天来讲一下基于TabLayout来实现的根据后台下发实现动态替换底部导航资源图片的方法。

  • 因为动态替换肯定意味着下载资源,所以先讲一下IntentService

    • IntentService也是一个service,只不过google帮我们在里面封装并维护了一个HandlerThread,里面的操作都是异步的。
    • 当任务执行完后,IntentService 会自动停止,不需要我们去手动结束。
    • 如果启动 IntentService 多次,那么每一个耗时操作会以工作队列的方式在 IntentService 的 onHandleIntent 回调方法中执行,依次去执行,使用串行的方式,执行完自动结束。
  • 这里面最重要的方法 onHandlerIntent(Intent intent)
    •     @Override
      protected void onHandleIntent(Intent intent) {
      if (intent != null) {
      final String action = intent.getAction();
      if (ACTION_FOO.equals(action)) {
      // 在这里面处理耗时任务,当所有的耗时任务都结束以后,IntentService会自动的finish掉,不需要开发者关心。
      }
      }
      }

3、选择IntentService的原因(下面的这几个操作都是耗时操作,所以我们干脆都封装到这service里面,我们只需要在合适的时机去启动这个Service就ok了)

    • 需要下载资源压缩包

      • 因为是动态替换,所以必然涉及到预下载,所以数据格式要先定好(下面是数据格式)。
      • {
        "currentInfo":{//当前样式
        "id":"111",
        "imageZipUrl":"http://oc8ql3urp.bkt.clouddn.com/currentInfo.zip",
        "tabNamesList":[
        "首页1","产品1","发现1","我的1"
        ],
        "tabColorNormal":"B0C4DE",
        "tabColorHighlight":"F7B62D",
        "startTime":"1517846400000",
        "deadLineTime":"1518710400000"
        },
        "nextInfo":{//下一次要展示的样式
        "id":"111",
        "imageZipUrl":"http://oc8ql3urp.bkt.clouddn.com/nextInfo.zip",
        "tabNamesList":[
        "首页2","产品2","发现2","我的2"
        ],
        "tabColorNormal":"B0C4DE",
        "tabColorHighlight":"FE6246",
        "startTime":"1417846400000",
        "deadLineTime":"1518710400000"
        }
        }
    • 需要存放资源压缩包

      • 下载和存放文件的代码(我这里使用的是Retrofit进行下载的)
      • // 下载文件
        Response<ResponseBody> zipFile = ServiceGenerator.createService(HomeService.class)
        .downloadFileRetrofit(getFileDownLoadUrl(homeTabImageInfoBean, type))
        .execute(); // 得到文件流
        ResponseBody zipBody = zipFile.body(); LogUtils.d("HomeTabImageDownLoadInt", "下载完成---"); // 创建一个文件
        File zipDirectory = new File(FilePathUtil.getHuaShengHomeTabZipDirectory(getApplicationContext())
        + createZipFileName(homeTabImageInfoBean, type)); // 如果文件不存在,则创建文件夹
        if (!zipDirectory.exists()) {
        zipDirectory.createNewFile();
        } // 保存文件
        FileUtils.writeFile2Disk(zipBody, zipDirectory);
    • 需要解压资源压缩方
      • 解压的话就是使用java里面的那几个类进行解压的操作
      • // 解压文件 并删除文件
        if (ZipUtils.unzipFile(zipDirectory.getAbsolutePath(),
        CURRENT.equals(type) ? FilePathUtil.getHuaShengHomeTabImgCurrentDirectory(getApplicationContext())
        : FilePathUtil.getHuaShengHomeTabImgNextDirectory(getApplicationContext()))) { // 保存文件解压地址
        saveFileDirPath(homeTabImageInfoBean, type,
        CURRENT.equals(type) ? FilePathUtil.getHuaShengHomeTabImgCurrentDirectory(getApplicationContext())
        : FilePathUtil.getHuaShengHomeTabImgNextDirectory(getApplicationContext())); LogUtils.d("HomeTabImageDownLoadInt", "解压完成---"); }

  4、其实最关键的就是如何创建并获取我们的文件资源

    • 最重要的就是资源的两种状态切换(选中 or 不选中),通常我们都是使用drawable来写的
    • <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:drawable="@mipmap/home_tab_financing_selected" android:state_selected="true" />
      <item android:drawable="@mipmap/home_tab_financing_normal" />
      </selector>
    • 现在我们要根据下载下来的图片(存放在sdcard中)去动态创建drawable,这样我们便能里面系统控件的互斥特性,下面的三个方法代码很重要。
    •     // 构建Drawable选择器
      private StateListDrawable createDrawableSelector(Drawable checked, Drawable unchecked) {
      StateListDrawable stateList = new StateListDrawable();
      int state_selected = android.R.attr.state_selected;
      stateList.addState(new int[]{state_selected}, checked);
      stateList.addState(new int[]{-state_selected}, unchecked);
      return stateList;
      }
          // 构建颜色选择器
      private ColorStateList createColorSelector(int checkedColor, int uncheckedColor) { return new ColorStateList(
      new int[][]{new int[]{android.R.attr.state_selected},
      new int[]{-android.R.attr.state_selected}},
      new int[]{checkedColor, uncheckedColor});
      }
          // 将文件转换成Drawable
      // pathName就是图片存放的绝对路径
      private Drawable getDrawableByFile(String pathName) {
      return Drawable.createFromPath(pathName);
      }

  5、如何给TabLayout设置上资源我觉得就不需要我来说了吧

    • 取出TabLayout的所有的Tab,遍历,然后根据特定条件去设置相应的drawable

好了,这样就可以实现底部Tab动态替换了,最后奉上一个解压的工具类

 import com.blankj.utilcode.util.CloseUtils;
import com.blankj.utilcode.util.StringUtils; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream; /**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2016/08/27
* desc : 压缩相关工具类
* </pre>
*/
public final class ZipUtils { private static final int KB = 1024; private ZipUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
} /**
* 批量压缩文件
*
* @param resFiles 待压缩文件集合
* @param zipFilePath 压缩文件路径
* @return {@code true}: 压缩成功<br>{@code false}: 压缩失败
* @throws IOException IO错误时抛出
*/
public static boolean zipFiles(Collection<File> resFiles, String zipFilePath)
throws IOException {
return zipFiles(resFiles, zipFilePath, null);
} /**
* 批量压缩文件
*
* @param resFiles 待压缩文件集合
* @param zipFilePath 压缩文件路径
* @param comment 压缩文件的注释
* @return {@code true}: 压缩成功<br>{@code false}: 压缩失败
* @throws IOException IO错误时抛出
*/
public static boolean zipFiles(Collection<File> resFiles, String zipFilePath, String comment)
throws IOException {
return zipFiles(resFiles, FileUtils.getFileByPath(zipFilePath), comment);
} /**
* 批量压缩文件
*
* @param resFiles 待压缩文件集合
* @param zipFile 压缩文件
* @return {@code true}: 压缩成功<br>{@code false}: 压缩失败
* @throws IOException IO错误时抛出
*/
public static boolean zipFiles(Collection<File> resFiles, File zipFile)
throws IOException {
return zipFiles(resFiles, zipFile, null);
} /**
* 批量压缩文件
*
* @param resFiles 待压缩文件集合
* @param zipFile 压缩文件
* @param comment 压缩文件的注释
* @return {@code true}: 压缩成功<br>{@code false}: 压缩失败
* @throws IOException IO错误时抛出
*/
public static boolean zipFiles(Collection<File> resFiles, File zipFile, String comment)
throws IOException {
if (resFiles == null || zipFile == null) return false;
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new FileOutputStream(zipFile));
for (File resFile : resFiles) {
if (!zipFile(resFile, "", zos, comment)) return false;
}
return true;
} finally {
if (zos != null) {
zos.finish();
CloseUtils.closeIO(zos);
}
}
} /**
* 压缩文件
*
* @param resFilePath 待压缩文件路径
* @param zipFilePath 压缩文件路径
* @return {@code true}: 压缩成功<br>{@code false}: 压缩失败
* @throws IOException IO错误时抛出
*/
public static boolean zipFile(String resFilePath, String zipFilePath)
throws IOException {
return zipFile(resFilePath, zipFilePath, null);
} /**
* 压缩文件
*
* @param resFilePath 待压缩文件路径
* @param zipFilePath 压缩文件路径
* @param comment 压缩文件的注释
* @return {@code true}: 压缩成功<br>{@code false}: 压缩失败
* @throws IOException IO错误时抛出
*/
public static boolean zipFile(String resFilePath, String zipFilePath, String comment)
throws IOException {
return zipFile(FileUtils.getFileByPath(resFilePath), FileUtils.getFileByPath(zipFilePath), comment);
} /**
* 压缩文件
*
* @param resFile 待压缩文件
* @param zipFile 压缩文件
* @return {@code true}: 压缩成功<br>{@code false}: 压缩失败
* @throws IOException IO错误时抛出
*/
public static boolean zipFile(File resFile, File zipFile)
throws IOException {
return zipFile(resFile, zipFile, null);
} /**
* 压缩文件
*
* @param resFile 待压缩文件
* @param zipFile 压缩文件
* @param comment 压缩文件的注释
* @return {@code true}: 压缩成功<br>{@code false}: 压缩失败
* @throws IOException IO错误时抛出
*/
public static boolean zipFile(File resFile, File zipFile, String comment)
throws IOException {
if (resFile == null || zipFile == null) return false;
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new FileOutputStream(zipFile));
return zipFile(resFile, "", zos, comment);
} finally {
if (zos != null) {
CloseUtils.closeIO(zos);
}
}
} /**
* 压缩文件
*
* @param resFile 待压缩文件
* @param rootPath 相对于压缩文件的路径
* @param zos 压缩文件输出流
* @param comment 压缩文件的注释
* @return {@code true}: 压缩成功<br>{@code false}: 压缩失败
* @throws IOException IO错误时抛出
*/
private static boolean zipFile(File resFile, String rootPath, ZipOutputStream zos, String comment)
throws IOException {
rootPath = rootPath + (isSpace(rootPath) ? "" : File.separator) + resFile.getName();
if (resFile.isDirectory()) {
File[] fileList = resFile.listFiles();
// 如果是空文件夹那么创建它,我把'/'换为File.separator测试就不成功,eggPain
if (fileList == null || fileList.length <= 0) {
ZipEntry entry = new ZipEntry(rootPath + '/');
if (!StringUtils.isEmpty(comment)) entry.setComment(comment);
zos.putNextEntry(entry);
zos.closeEntry();
} else {
for (File file : fileList) {
// 如果递归返回false则返回false
if (!zipFile(file, rootPath, zos, comment)) return false;
}
}
} else {
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(resFile));
ZipEntry entry = new ZipEntry(rootPath);
if (!StringUtils.isEmpty(comment)) entry.setComment(comment);
zos.putNextEntry(entry);
byte buffer[] = new byte[KB];
int len;
while ((len = is.read(buffer, 0, KB)) != -1) {
zos.write(buffer, 0, len);
}
zos.closeEntry();
} finally {
CloseUtils.closeIO(is);
}
}
return true;
} /**
* 批量解压文件
*
* @param zipFiles 压缩文件集合
* @param destDirPath 目标目录路径
* @return {@code true}: 解压成功<br>{@code false}: 解压失败
* @throws IOException IO错误时抛出
*/
public static boolean unzipFiles(Collection<File> zipFiles, String destDirPath)
throws IOException {
return unzipFiles(zipFiles, FileUtils.getFileByPath(destDirPath));
} /**
* 批量解压文件
*
* @param zipFiles 压缩文件集合
* @param destDir 目标目录
* @return {@code true}: 解压成功<br>{@code false}: 解压失败
* @throws IOException IO错误时抛出
*/
public static boolean unzipFiles(Collection<File> zipFiles, File destDir)
throws IOException {
if (zipFiles == null || destDir == null) return false;
for (File zipFile : zipFiles) {
if (!unzipFile(zipFile, destDir)) return false;
}
return true;
} /**
* 解压文件
*
* @param zipFilePath 待解压文件路径
* @param destDirPath 目标目录路径
* @return {@code true}: 解压成功<br>{@code false}: 解压失败
* @throws IOException IO错误时抛出
*/
public static boolean unzipFile(String zipFilePath, String destDirPath) throws IOException {
// 判断是否存在这个路径,没有的话就创建这个路径
File tempDir = new File(destDirPath);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
return unzipFile(FileUtils.getFileByPath(zipFilePath), FileUtils.getFileByPath(destDirPath));
} /**
* 解压文件
*
* @param zipFile 待解压文件
* @param destDir 目标目录
* @return {@code true}: 解压成功<br>{@code false}: 解压失败
* @throws IOException IO错误时抛出
*/
public static boolean unzipFile(File zipFile, File destDir)
throws IOException {
return unzipFileByKeyword(zipFile, destDir, null) != null;
} /**
* 解压带有关键字的文件
*
* @param zipFilePath 待解压文件路径
* @param destDirPath 目标目录路径
* @param keyword 关键字
* @return 返回带有关键字的文件链表
* @throws IOException IO错误时抛出
*/
public static List<File> unzipFileByKeyword(String zipFilePath, String destDirPath, String keyword)
throws IOException {
return unzipFileByKeyword(FileUtils.getFileByPath(zipFilePath),
FileUtils.getFileByPath(destDirPath), keyword);
} /**
* 解压带有关键字的文件
*
* @param zipFile 待解压文件
* @param destDir 目标目录
* @param keyword 关键字
* @return 返回带有关键字的文件链表
* @throws IOException IO错误时抛出
*/
public static List<File> unzipFileByKeyword(File zipFile, File destDir, String keyword)
throws IOException {
if (zipFile == null || destDir == null) return null;
List<File> files = new ArrayList<>();
ZipFile zf = new ZipFile(zipFile);
Enumeration<?> entries = zf.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
String entryName = entry.getName();
if (StringUtils.isEmpty(keyword) || FileUtils.getFileName(entryName).toLowerCase().contains(keyword.toLowerCase())) {
String filePath = destDir + File.separator + entryName;
File file = new File(filePath);
files.add(file);
if (entry.isDirectory()) {
if (!FileUtils.createOrExistsDir(file)) return null;
} else {
if (!FileUtils.createOrExistsFile(file)) return null;
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(zf.getInputStream(entry));
out = new BufferedOutputStream(new FileOutputStream(file));
byte buffer[] = new byte[KB];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
} finally {
CloseUtils.closeIO(in, out);
}
}
}
}
return files;
} /**
* 获取压缩文件中的文件路径链表
*
* @param zipFilePath 压缩文件路径
* @return 压缩文件中的文件路径链表
* @throws IOException IO错误时抛出
*/
public static List<String> getFilesPath(String zipFilePath)
throws IOException {
return getFilesPath(FileUtils.getFileByPath(zipFilePath));
} /**
* 获取压缩文件中的文件路径链表
*
* @param zipFile 压缩文件
* @return 压缩文件中的文件路径链表
* @throws IOException IO错误时抛出
*/
public static List<String> getFilesPath(File zipFile)
throws IOException {
if (zipFile == null) return null;
List<String> paths = new ArrayList<>();
Enumeration<?> entries = getEntries(zipFile);
while (entries.hasMoreElements()) {
paths.add(((ZipEntry) entries.nextElement()).getName());
}
return paths;
} /**
* 获取压缩文件中的注释链表
*
* @param zipFilePath 压缩文件路径
* @return 压缩文件中的注释链表
* @throws IOException IO错误时抛出
*/
public static List<String> getComments(String zipFilePath)
throws IOException {
return getComments(FileUtils.getFileByPath(zipFilePath));
} /**
* 获取压缩文件中的注释链表
*
* @param zipFile 压缩文件
* @return 压缩文件中的注释链表
* @throws IOException IO错误时抛出
*/
public static List<String> getComments(File zipFile)
throws IOException {
if (zipFile == null) return null;
List<String> comments = new ArrayList<>();
Enumeration<?> entries = getEntries(zipFile);
while (entries.hasMoreElements()) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
comments.add(entry.getComment());
}
return comments;
} /**
* 获取压缩文件中的文件对象
*
* @param zipFilePath 压缩文件路径
* @return 压缩文件中的文件对象
* @throws IOException IO错误时抛出
*/
public static Enumeration<?> getEntries(String zipFilePath)
throws IOException {
return getEntries(FileUtils.getFileByPath(zipFilePath));
} /**
* 获取压缩文件中的文件对象
*
* @param zipFile 压缩文件
* @return 压缩文件中的文件对象
* @throws IOException IO错误时抛出
*/
public static Enumeration<?> getEntries(File zipFile)
throws IOException {
if (zipFile == null) return null;
return new ZipFile(zipFile).entries();
} private static boolean isSpace(String s) {
if (s == null) return true;
for (int i = 0, len = s.length(); i < len; ++i) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}
return true;
}
}

工具类

Android--底部导航栏的动态替换方案的更多相关文章

  1. Android底部导航栏——FrameLayout + RadioGroup

    原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6285881.html Android底部导航栏有多种实现方式,本文详细介绍FrameLayout ...

  2. Android底部导航栏创建——ViewPager + RadioGroup

    原创文章,引用请注明出处:http://www.cnblogs.com/baipengzhan/p/6270201.html Android底部导航栏有多种实现方式,本文详解其中的ViewPager ...

  3. Android底部导航栏

    Android底部导航栏 今天简单写了一个底部导航栏,封装了一个库,用法比较简单 效果图 Github地址:https://github.com/kongqw/KqwBottomNavigation ...

  4. Android底部导航栏(可滑动)----TabLayout+viewPager

    [TabLayout] ①TabLayout是选项卡,在屏幕空间有限的情况下,对不同的空间进行分组.属于android support design,更多的用于新闻上,如果放在底部也可做底部导航栏 ② ...

  5. Android 底部导航栏实现一 Fragment-replace

    [效果](这里下载的软件收费的试用有水印) [推荐]这里推荐一个图标网http://iconfont.cn/.以上图标来自此图标网 [项目结构] [步骤] ①创建布局文件,写底部导航栏 <?xm ...

  6. android底部导航栏实现

    第一种用radiobutton实现 https://wizardforcel.gitbooks.io/w3school-android/content/75.html 布局文件,使用radiogrou ...

  7. android底部导航栏小结

    android自带的有TabHost,但好像无法满足要求, 本文只记录使用 TabLayout + Fragment  和 android 自带的 BottomNavigationView + Fra ...

  8. Android 底部导航栏的xml

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  9. Android 五种方式实现Android底部导航栏

    https://segmentfault.com/a/1190000007697941

随机推荐

  1. 搭建 MobileNet-SSD 开发环境并使用 VOC 数据集训练 TensorFlow 模型

    原文地址:搭建 MobileNet-SSD 开发环境并使用 VOC 数据集训练 TensorFlow 模型 0x00 环境 OS: Ubuntu 1810 x64 Anaconda: 4.6.12 P ...

  2. RocketMQ入门案例

    学习RocketMQ,先写一个Demo演示一下看看效果. 一.服务端部署 因为只是简单的为了演示效果,服务端仅部署单Master模式 —— 一个Name Server节点,一个Broker节点.主要有 ...

  3. leetcode — trapping-rain-water

    /** * Source : https://oj.leetcode.com/problems/trapping-rain-water/ * * Created by lverpeng on 2017 ...

  4. Vc数据库编程基础MySql数据库的表查询功能

    Vc数据库编程基础MySql数据库的表查询功能 一丶简介 不管是任何数据库.都会有查询功能.而且是很重要的功能.上一讲知识简单的讲解了表的查询所有. 那么这次我们需要掌握的则是. 1.使用select ...

  5. JAVA课程设计——一个简单的教务人事管理系统

    大三上学期期末总结,没错,上学期,写在下学期新学期开始,哈哈哈. 上学期学习了面向对象程序设计,课程设计的题目使用JAVA语言完成一个简单的教务人事管理系统,能够实现访问数据库的登录验证,分别按部门和 ...

  6. Linux命令-基本操作和软件安装

    配置JDK环境:JAVA_HOME:D:\software\JDK1.7Path:%JAVA_HOME%\bin;%JAVA_HOME%\lib; ls(查看当前目录下的文件及文件夹)sh initN ...

  7. webpack 配置文件相关解说

    博客地址:https://ainyi.com/10 webpack - 什么是webpack: WebPack可以看做是模块打包机:它做的事情是,分析你的项目结构,找到JavaScript模块以及其它 ...

  8. RNN入门(4)利用LSTM实现整数加法运算

      本文将介绍LSTM模型在实现整数加法方面的应用.   我们以0-255之间的整数加法为例,生成的结果在0到510之间.为了能利用深度学习模型模拟整数的加法运算,我们需要将输入的两个加数和输出的结果 ...

  9. IdentityServer4 中文文档 -12- (快速入门)添加外部认证支持

    IdentityServer4 中文文档 -12- (快速入门)添加外部认证支持 原文:http://docs.identityserver.io/en/release/quickstarts/4_e ...

  10. 【微服务No.3】AOP组件ASPectCore简单使用

    介绍: AspectCore是.NET标准的基于AOP的跨平台框架[github解释].主要支持:对方面拦截器,依赖注入集成,Web应用程序,数据验证等的核心支持. 使用实例: 首先安装dll: In ...