Android底部導航欄的動態替換方案

這篇文章主要爲大家詳細介紹了Android底部導航欄的動態替換方案,具有一定的參考價值,感興趣的小夥伴們可以參考一下

Android底部導航欄的動態替換方案,供大家參考,具體內容如下

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

1)、自定義view,然後自己寫邏輯去實現互斥。

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

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

4)、使用google design包裏面的BottomNavigationView去實現。

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

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掉,不需要開發者關心。
   }
  }
 }

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

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

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

// 下載文件
  Response<ResponseBody> zipFile = ServiceGenerator.createService(HomeService.class)
   .downloadFileRetrofit(getFileDownLoadUrl(homeTabImageInfoBean, type))
   .execute();

   // 得到文件流
   ResponseBody zipBody = zipFile.body();

   LogUtils.d("DownLoad", "下載完成");

   // 創建一個文件
   File zipDirectory = new File(FilePathUtil.getHuaShengHomeTabZipDirectory(getApplicationContext())
     + createZipFileName(homeTabImageInfoBean, type));

   // 如果文件不存在,則創建文件夾
   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", "解壓完成---");

   }

其實最關鍵的就是如何創建並獲取我們的文件資源

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

最後就是在TabLayout的tab上設置資源

取出TabLayout的所有的Tab,遍歷,然後根據特定條件去設置相應的drawable就可以了

最後在本文結尾附上上文的壓縮相關工具類

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 : 壓縮相關工具類
 * </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;
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章