【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android中如何動(dòng)態(tài)替換底部導(dǎo)航欄_第1頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android中如何動(dòng)態(tài)替換底部導(dǎo)航欄_第2頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android中如何動(dòng)態(tài)替換底部導(dǎo)航欄_第3頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android中如何動(dòng)態(tài)替換底部導(dǎo)航欄_第4頁(yè)
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android中如何動(dòng)態(tài)替換底部導(dǎo)航欄_第5頁(yè)
已閱讀5頁(yè),還剩15頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】Android中如何動(dòng)態(tài)替換底部導(dǎo)航欄

Android中如何動(dòng)態(tài)替換底部導(dǎo)航欄?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。具體內(nèi)容如下1、通常來(lái)說(shuō),一般情況下,我們的app的BottomTab會(huì)有下面幾種實(shí)現(xiàn)方式。1)、自定義view,然后自己寫邏輯去實(shí)現(xiàn)互斥。2)、使用RadioGroup+RadioButton去實(shí)現(xiàn)底部的Tab。自由度比極高,如果想實(shí)現(xiàn)搞復(fù)雜度的話可以重寫RadioButton。3)、使用googledesign包里面的TabLayout去實(shí)現(xiàn)??缮?、可下、可以滑動(dòng)偷懶的話可以根據(jù)已有api來(lái)設(shè)置一些資源,也可以setCustomView()4)、使用googledesign包里面的BottomNavigationView去實(shí)現(xiàn)。(1)使用menu設(shè)置資源(2)有默認(rèn)的動(dòng)畫效果(1)使用menu設(shè)置資源(2)有默認(rèn)的動(dòng)畫效果2.本篇介紹的是日常見(jiàn)到的京東,淘寶類似的根據(jù)后臺(tái)下發(fā)實(shí)現(xiàn)動(dòng)態(tài)替換底部導(dǎo)航資源圖片的方法(基于TabLayout實(shí)現(xiàn))既然提到了動(dòng)態(tài)替換肯定意味著要下載資源,所以先講一下IntentServiceIntentService也是一個(gè)service,只不過(guò)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(Intentintent)是最重要的一個(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掉,不需要開(kāi)發(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":[

"首頁(yè)1","附近1","發(fā)現(xiàn)1","我的1"

],

"tabColorNormal":"B0C4DE",

"tabColorHighlight":"F7B62D",

"startTime":開(kāi)始時(shí)間,

"deadLineTime":結(jié)束時(shí)間

},

"nextInfo":{//下一次要展示的樣式

"id":"111",

"imageZipUrl":你的下載地址,

"tabNamesList":[

"首頁(yè)2","附近2","發(fā)現(xiàn)2","我的2"

],

"tabColorNormal":"B0C4DE",

"tabColorHighlight":"FE6246",

"startTime":開(kāi)始時(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);解壓資源并刪除文件(解壓方法由于過(guò)長(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來(lái)寫的<?xml

version="1.0"

encoding="utf-8"?>

<selector

xmlns:android="/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ù)下載下來(lái)的圖片(存放在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è)路徑,沒(méi)有的話就創(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()

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論