国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

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

Android底部導(dǎo)航欄的動(dòng)態(tài)替換方案

2019-12-12 00:24:10
字體:
供稿:網(wǎng)友

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

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

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

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

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

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

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

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

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

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

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

選擇IntentService的原因是因?yàn)橄旅娴倪@幾個(gè)操作都是耗時(shí)操作,所以我們干脆都封裝到這service里面,我們只需要在合適的時(shí)機(jī)去啟動(dòng)這個(gè)Service就ok了

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

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

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

其實(shí)最關(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中)去動(dòng)態(tài)創(chuàng)建drawable這樣我們便能里面系統(tǒng)控件的互斥特性

下面的三個(gè)方法代碼很重要

// 構(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就是圖片存放的絕對(duì)路徑 private Drawable getDrawableByFile(String pathName) {  return Drawable.createFromPath(pathName); }

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

取出TabLayout的所有的Tab,遍歷,然后根據(jù)特定條件去設(shè)置相應(yīng)的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錯(cuò)誤時(shí)拋出  */ 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錯(cuò)誤時(shí)拋出  */ 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錯(cuò)誤時(shí)拋出  */ 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錯(cuò)誤時(shí)拋出  */ 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錯(cuò)誤時(shí)拋出  */ 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錯(cuò)誤時(shí)拋出  */ 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錯(cuò)誤時(shí)拋出  */ 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錯(cuò)誤時(shí)拋出  */ 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 相對(duì)于壓縮文件的路徑  * @param zos  壓縮文件輸出流  * @param comment 壓縮文件的注釋  * @return {@code true}: 壓縮成功<br>{@code false}: 壓縮失敗  * @throws IOException IO錯(cuò)誤時(shí)拋出  */ 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測(cè)試就不成功,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 目標(biāo)目錄路徑  * @return {@code true}: 解壓成功<br>{@code false}: 解壓失敗  * @throws IOException IO錯(cuò)誤時(shí)拋出  */ public static boolean unzipFiles(Collection<File> zipFiles, String destDirPath)   throws IOException {  return unzipFiles(zipFiles, FileUtils.getFileByPath(destDirPath)); } /**  * 批量解壓文件  *  * @param zipFiles 壓縮文件集合  * @param destDir 目標(biāo)目錄  * @return {@code true}: 解壓成功<br>{@code false}: 解壓失敗  * @throws IOException IO錯(cuò)誤時(shí)拋出  */ 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 目標(biāo)目錄路徑  * @return {@code true}: 解壓成功<br>{@code false}: 解壓失敗  * @throws IOException IO錯(cuò)誤時(shí)拋出  */ public static boolean unzipFile(String zipFilePath, String destDirPath) throws IOException {  // 判斷是否存在這個(gè)路徑,沒有的話就創(chuàng)建這個(gè)路徑  File tempDir = new File(destDirPath);  if (!tempDir.exists()) {   tempDir.mkdirs();  }  return unzipFile(FileUtils.getFileByPath(zipFilePath), FileUtils.getFileByPath(destDirPath)); } /**  * 解壓文件  *  * @param zipFile 待解壓文件  * @param destDir 目標(biāo)目錄  * @return {@code true}: 解壓成功<br>{@code false}: 解壓失敗  * @throws IOException IO錯(cuò)誤時(shí)拋出  */ public static boolean unzipFile(File zipFile, File destDir)   throws IOException {  return unzipFileByKeyword(zipFile, destDir, null) != null; } /**  * 解壓帶有關(guān)鍵字的文件  *  * @param zipFilePath 待解壓文件路徑  * @param destDirPath 目標(biāo)目錄路徑  * @param keyword  關(guān)鍵字  * @return 返回帶有關(guān)鍵字的文件鏈表  * @throws IOException IO錯(cuò)誤時(shí)拋出  */ 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 目標(biāo)目錄  * @param keyword 關(guān)鍵字  * @return 返回帶有關(guān)鍵字的文件鏈表  * @throws IOException IO錯(cuò)誤時(shí)拋出  */ 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錯(cuò)誤時(shí)拋出  */ public static List<String> getFilesPath(String zipFilePath)   throws IOException {  return getFilesPath(FileUtils.getFileByPath(zipFilePath)); } /**  * 獲取壓縮文件中的文件路徑鏈表  *  * @param zipFile 壓縮文件  * @return 壓縮文件中的文件路徑鏈表  * @throws IOException IO錯(cuò)誤時(shí)拋出  */ 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錯(cuò)誤時(shí)拋出  */ public static List<String> getComments(String zipFilePath)   throws IOException {  return getComments(FileUtils.getFileByPath(zipFilePath)); } /**  * 獲取壓縮文件中的注釋鏈表  *  * @param zipFile 壓縮文件  * @return 壓縮文件中的注釋鏈表  * @throws IOException IO錯(cuò)誤時(shí)拋出  */ 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; } /**  * 獲取壓縮文件中的文件對(duì)象  *  * @param zipFilePath 壓縮文件路徑  * @return 壓縮文件中的文件對(duì)象  * @throws IOException IO錯(cuò)誤時(shí)拋出  */ public static Enumeration<?> getEntries(String zipFilePath)   throws IOException {  return getEntries(FileUtils.getFileByPath(zipFilePath)); } /**  * 獲取壓縮文件中的文件對(duì)象  *  * @param zipFile 壓縮文件  * @return 壓縮文件中的文件對(duì)象  * @throws IOException IO錯(cuò)誤時(shí)拋出  */ 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; }}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 卢氏县| 陆川县| 普兰县| 许昌县| 晋州市| 刚察县| 清水河县| 伊金霍洛旗| 天镇县| 鸡东县| 迁安市| 炉霍县| 类乌齐县| 伊春市| 虹口区| 永胜县| 海林市| 密云县| 奉化市| 陆川县| 竹溪县| 湖南省| 米林县| 石城县| 厦门市| 龙泉市| 沧州市| 商水县| 磐石市| 甘肃省| 湘潭县| 绥芬河市| 休宁县| 任丘市| 宣城市| 大关县| 北辰区| 越西县| 四会市| 百色市| 虞城县|