a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統(tǒng) > Android > 正文

Android底部導航欄的動態(tài)替換方案

2019-10-21 21:33:08
字體:
供稿:網(wǎng)友

Android底部導航欄的動態(tài)替換方案,供大家參考,具體內(nèi)容如下

1、通常來說,一般情況下,我們的app的BottomTab會有下面幾種實現(xiàn)方式。

1)、自定義view,然后自己寫邏輯去實現(xiàn)互斥。

2)、使用RadioGroup+RadioButton去實現(xiàn)底部的Tab。
自由度比極高,如果想實現(xiàn)搞復雜度的話可以重寫 RadioButton。

3)、使用google design包里面的 TabLayout去實現(xiàn)。
可上、可下、可以滑動
偷懶的話可以根據(jù)已有api來設(shè)置一些資源,也可以 setCustomView()

4)、使用google design包里面的BottomNavigationView去實現(xiàn)。

(1)使用menu設(shè)置資源
(2)有默認的動畫效果

2.本篇介紹的是日常見到的京東,淘寶類似的根據(jù)后臺下發(fā)實現(xiàn)動態(tài)替換底部導航資源圖片的方法(基于TabLayout實現(xiàn))
既然提到了動態(tài)替換肯定意味著要下載資源,所以先講一下IntentService

  • IntentService也是一個service,只不過google幫我們在里面封裝并維護了一個HandlerThread,里面的操作都是異步的。
  • 當任務(wù)執(zhí)行完后,IntentService 會自動停止,不需要我們?nèi)ナ謩咏Y(jié)束。
  • 如果啟動 IntentService 多次,那么每一個耗時操作會以工作隊列的方式在 IntentService 的 onHandleIntent 回調(diào)方法中執(zhí)行,依次去執(zhí)行,使用串行的方式,執(zhí)行完自動結(jié)束。

onHandlerIntent(Intent intent) 是最重要的一個方法

@Override protected void onHandleIntent(Intent intent) {  if (intent != null) {   final String action = intent.getAction();   if (ACTION_FOO.equals(action)) {    // 在這里面處理耗時任務(wù),當所有的耗時任務(wù)都結(jié)束以后,IntentService會自動的finish掉,不需要開發(fā)者關(guān)心。   }  } }

選擇IntentService的原因是因為下面的這幾個操作都是耗時操作,所以我們干脆都封裝到這service里面,我們只需要在合適的時機去啟動這個Service就ok了

  • 需要下載資源壓縮包
  • 因為是動態(tài)替換,所以必然涉及到預下載,所以數(shù)據(jù)格式要先定好(下面是數(shù)據(jù)格式)。
{  "currentInfo":{//當前樣式   "id":"111",   "imageZipUrl":你的下載地址,   "tabNamesList":[     "首頁1","附近1","發(fā)現(xiàn)1","我的1"   ],   "tabColorNormal":"B0C4DE",   "tabColorHighlight":"F7B62D",   "startTime":開始時間,   "deadLineTime":結(jié)束時間  },  "nextInfo":{//下一次要展示的樣式   "id":"111",   "imageZipUrl":你的下載地址,   "tabNamesList":[     "首頁2","附近2","發(fā)現(xiàn)2","我的2"   ],   "tabColorNormal":"B0C4DE",   "tabColorHighlight":"FE6246",   "startTime":開始時間,   "deadLineTime":結(jié)束時間  } }
  • 需要存放資源壓縮包

下載和存放文件的代碼(這里使用的是Retrofit進行下載的) 

// 下載文件  Response<ResponseBody> zipFile = ServiceGenerator.createService(HomeService.class)   .downloadFileRetrofit(getFileDownLoadUrl(homeTabImageInfoBean, type))   .execute();   // 得到文件流   ResponseBody zipBody = zipFile.body();   LogUtils.d("DownLoad", "下載完成");   // 創(chuàng)建一個文件   File zipDirectory = new File(FilePathUtil.getHuaShengHomeTabZipDirectory(getApplicationContext())     + createZipFileName(homeTabImageInfoBean, type));   // 如果文件不存在,則創(chuàng)建文件夾   if (!zipDirectory.exists()) {    zipDirectory.createNewFile();   }   // 保存文件   FileUtils.writeFile2Disk(zipBody, zipDirectory);
  • 解壓資源并刪除文件(解壓方法由于過長所以寫在了文中底部)
// 解壓文件 并刪除文件   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", "解壓完成---");   }

其實最關(guān)鍵的就是如何創(chuàng)建并獲取我們的文件資源

重要的就是資源的兩種狀態(tài)切換(選中 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>

現(xiàn)在我們要根據(jù)下載下來的圖片(存放在sdcard中)去動態(tài)創(chuàng)建drawable這樣我們便能里面系統(tǒng)控件的互斥特性

下面的三個方法代碼很重要

// 構(gòu)建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; }
// 構(gòu)建顏色選擇器 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});
// 將文件轉(zhuǎn)換成Drawable // pathName就是圖片存放的絕對路徑 private Drawable getDrawableByFile(String pathName) {  return Drawable.createFromPath(pathName); }

最后就是在TabLayout的tab上設(shè)置資源

取出TabLayout的所有的Tab,遍歷,然后根據(jù)特定條件去設(shè)置相應的drawable就可以了

最后在本文結(jié)尾附上上文的壓縮相關(guān)工具類

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: 程龍 *  time : 2018/12/14 *  desc : 壓縮相關(guān)工具類 * </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();   // 如果是空文件夾那么創(chuàng)建它,我把'/'換為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 {  // 判斷是否存在這個路徑,沒有的話就創(chuàng)建這個路徑  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; } /**  * 解壓帶有關(guān)鍵字的文件  *  * @param zipFilePath 待解壓文件路徑  * @param destDirPath 目標目錄路徑  * @param keyword  關(guān)鍵字  * @return 返回帶有關(guān)鍵字的文件鏈表  * @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); } /**  * 解壓帶有關(guān)鍵字的文件  *  * @param zipFile 待解壓文件  * @param destDir 目標目錄  * @param keyword 關(guān)鍵字  * @return 返回帶有關(guān)鍵字的文件鏈表  * @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; }}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 欧美精品一区二区三区蜜桃视频 | 国产午夜精品久久久久久久 | 黄色网页大全 | 在线精品亚洲欧美日韩国产 | 天天操操 | 成人蜜桃视频 | 欧美成人在线影院 | 日韩h| 国产乱肥老妇国产一区二 | 欧洲精品一区 | 欧美一级毛片免费观看 | 狠狠夜夜 | 日韩欧美一区二区三区久久婷婷 | 精品一区二区三区在线观看 | 久久久久久久久久久免费 | 99精品一区二区三区 | 久久久久免费观看 | 99精品国产高清一区二区麻豆 | 久久亚洲天堂 | 欧美一区二区三区免费在线观看 | 日韩一区二区免费视频 | 欧美精品在线观看 | 久草热8精品视频在线观看 高清av一区 | 色噜噜在线观看 | 国产一区91| 亚洲第一国产精品 | 最新精品在线 | 逼逼av | 欧美高清一区二区 | 久久高清 | 色综合久久久久 | 一级黄色录像免费观看 | 久在线视频 | 日韩免费视频中文字幕 | 中文字幕第二十六页页 | 亚洲欧美中文字幕在线观看 | 精品一区二区三区在线观看 | 免费av中国 | 欧美日韩在线电影 | 国产美女在线精品免费观看 | 国产成人久久 |