本文實(shí)例講述了Android編程實(shí)現(xiàn)的簡(jiǎn)易路徑導(dǎo)航條功能。分享給大家供大家參考,具體如下:
為柳北等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及柳北網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、柳北網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
這里要實(shí)現(xiàn)的是如圖所示的路徑導(dǎo)航條, 類似于文件管理器的效果。

該導(dǎo)航條包含三個(gè)功能:
1. 支持追加任意個(gè)子路徑(文字一行寫不下時(shí)可左右滑動(dòng));
2. 支持返回到上一個(gè)路徑;
3. 支持點(diǎn)擊中間的某個(gè)路徑回到指定位置。
代碼很簡(jiǎn)單,已封裝成自定義View, 如下:
PathTextView.Java
/**
* 顯示路徑的View,支持返回上一級(jí),支持點(diǎn)擊某個(gè)位置回到指定層級(jí)。
*/
public class PathTextView extends LinearLayout {
private TextView mTextView;
private HorizontalScrollView hsView;
private OnItemClickListener mListener;
//保存每一個(gè)路徑的id和名稱
private LinkedList<PathItem> pathItemList;
//可點(diǎn)擊部門文本顏色
private static final int TEXT_COLOR = Color.parseColor("#48a0c7");
//分隔符
private static final String DIV_STR = " - ";
public PathTextView(Context context) {
super(context);
}
public PathTextView(Context context, AttributeSet attrs) {
super(context, attrs);
View root = LayoutInflater.from(context).inflate(R.layout.simple_tv, this, true);
hsView = (HorizontalScrollView) root.findViewById(R.id.path_hs);
mTextView = (TextView) root.findViewById(R.id.path_tv);
mTextView.setMovementMethod(LinkMovementMethod.getInstance());
mTextView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return true;
}
});
pathItemList = new LinkedList<>();
}
/**
* 初始化根路徑名稱。
*/
public void initRoot(String text) {
mTextView.append(createSpannableString(-1, text));
pathItemList.addLast(new PathItem(-1, text));
}
/**
* 繼續(xù)拼接一個(gè)路徑。
*/
public void append(long id, String text) {
mTextView.append(DIV_STR);
mTextView.append(createSpannableString(id, text));
pathItemList.addLast(new PathItem(id, text));
//HorizontalScrollView滑動(dòng)到最右邊
hsView.postDelayed(new Runnable() {
@Override
public void run() {
hsView.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
}, 100);
}
/**
* 返回父級(jí)路徑,一般用戶點(diǎn)擊“返回”時(shí)調(diào)用。
*/
public void backParent() {
int lastItemLength = pathItemList.removeLast().text.length();
CharSequence oldCs = mTextView.getText();
mTextView.setText(oldCs.subSequence(0, oldCs.length() - lastItemLength - DIV_STR.length()));
}
private SpannableString createSpannableString(long id, String text) {
SpannableString spStr = new SpannableString(text);
ClickableSpan clickSpan = new MyClickableSpan(id);
spStr.setSpan(clickSpan, 0, text.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spStr.setSpan(new ForegroundColorSpan(TEXT_COLOR), 0, text.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
return spStr;
}
private class MyClickableSpan extends ClickableSpan {
private long id;
MyClickableSpan(long id) {
super();
this.id = id;
}
@Override
public void onClick(View widget) {
//更新當(dāng)前路徑
int backCount = 0;
while (pathItemList.getLast().id != id) {
backParent();
backCount++;
}
//回調(diào)
if (mListener != null && backCount > 0) {
mListener.onClick(id, backCount);
}
}
}
private class PathItem {
private long id;
private String text;
private PathItem(long id, String text) {
this.id = id;
this.text = text;
}
}
public interface OnItemClickListener {
/**
* @param currentId 返回后目錄的id.
* @param backCount 返回層級(jí)的數(shù)量.
*/
void onClick(long currentId, int backCount);
}
/**
* 設(shè)置點(diǎn)擊某個(gè)中間路徑時(shí)的回調(diào)。
*/
public void setOnItemClickListener(OnItemClickListener listener) {
this.mListener = listener;
}
}
布局文件,其實(shí)就是一個(gè)TextView:
simple_tv.xml
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/path_hs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/light_gray2"
android:scrollbars="none">
<TextView
android:id="@+id/path_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/light_gray2"
android:paddingBottom="12dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="12dp"
android:textSize="15sp" />
</HorizontalScrollView>
使用方法也很簡(jiǎn)單:
1. 初始化時(shí),調(diào)用 initRoot(String text) 方法,傳入根路徑名稱;
2. 點(diǎn)擊列表項(xiàng)進(jìn)入下一級(jí)時(shí),調(diào)用 append(long id, String text) 方法,傳遞當(dāng)前項(xiàng)的id(用于唯一性區(qū)分)和名稱;
3. 返回上一級(jí)時(shí),調(diào)用 backParent() 方法;
4. 點(diǎn)擊某個(gè)中間路徑時(shí), OnItemClickListener.onClick(long currentId, int backCount) 方法將會(huì)被回調(diào),并返回點(diǎn)擊項(xiàng)的id和返回的層級(jí)數(shù)量。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
網(wǎng)站名稱:Android編程實(shí)現(xiàn)的簡(jiǎn)易路徑導(dǎo)航條功能示例
分享路徑:http://chinadenli.net/article14/jgghde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、ChatGPT、App開發(fā)、微信公眾號(hào)、網(wǎng)站排名、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)