本篇內(nèi)容主要講解“ListView的側(cè)邊字母滑動(dòng)索引怎么實(shí)現(xiàn)”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“ListView的側(cè)邊字母滑動(dòng)索引怎么實(shí)現(xiàn)”吧!
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到上饒網(wǎng)站設(shè)計(jì)與上饒網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名申請(qǐng)、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋上饒地區(qū)。
一、最終效果
二、功能分析與實(shí)現(xiàn)
1.LisetView布局
分析:同樣的字母開(kāi)頭,第一個(gè)上方有該字母的標(biāo)志
實(shí)現(xiàn):在item布局中除了TextView再在其上方加一個(gè)TextView等布局(用于顯示數(shù)據(jù)的首字母),然后在適配器的getView中判斷該布局是否顯示。默認(rèn)設(shè)置該布局的Visibility為VISIBLE,當(dāng)該布局的的文字內(nèi)容首字母與上一個(gè)不同時(shí)說(shuō)明該item是新的首字母開(kāi)頭,設(shè)置為VISIBLE,否則設(shè)置為GONE。
部分代碼:
// 默認(rèn)顯示 為了顯示第一個(gè)布局以及布局復(fù)用出現(xiàn)的問(wèn)題
vh.cd_lable.setVisibility(View.VISIBLE);
String now_city = Cheeses.sCheeseStrings[position];
vh.tv_city.setText(now_city);
String now_letter = now_city.substring(0, 1).toUpperCase();
vh.tv_label.setText(now_letter);
if (position != 0) {
// 與上一個(gè)比較 如果不同說(shuō)明為該字母段的第一個(gè)
String pre_letter = Cheeses.sCheeseStrings[position - 1]
.substring(0, 1).toUpperCase();
if (!pre_letter.equals(now_letter)) {
vh.cd_lable.setVisibility(View.VISIBLE);
} else {
vh.cd_lable.setVisibility(View.GONE);
}
}
2.自定義控件字母索引
1.新建一個(gè)類繼承View,重寫(xiě)兩個(gè)參數(shù)的構(gòu)造方法,為了獲取AttributeSet。重寫(xiě)onDraw方法。
2.畫(huà)文字A-Z
1)調(diào)用canvas的drawText(String text, float x, float y, Paint paint)方法用來(lái)畫(huà)A-Z,這里的參數(shù)分別代表需要畫(huà)的文字,起始位置坐標(biāo)(x,y),畫(huà)筆。我們先在構(gòu)造方法里new Paint(Paint.ANTI_ALIAS_FLAG),這里的參數(shù)是為了抗鋸齒。
2)畫(huà)筆有了還需要坐標(biāo)和文字大小,構(gòu)造方法里的AttributeSet參數(shù)就有作用了。我們?cè)趓es/values目錄下建一個(gè)attrs.xml文件,為了放一些我們自定義控件所需要的屬性。如下:
<resources>
<declare-styleable name="LetterSlideView">
<attr name="textsize" format = "dimension|reference" />
<attr name="backcolor" format = "color|reference" />
<attr name="frontcolor" format = "color|reference" />
</declare-styleable>
</resources>
name一般為你自定義控件的類名,下面屬性意思就是名字不多說(shuō)了,format是為了限定你輸入的內(nèi)容格式,dimension|reference就是只能sp dp或者資源文件之類的。然后再自定義控件類的構(gòu)造方法中獲取這些屬性的值:
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.LetterSlideView);
text_size = typedArray.getDimension(
R.styleable.LetterSlideView_textsize, 26);
back_color = typedArray.getColor(R.styleable.LetterSlideView_backcolor,
Color.BLACK);
front_color = typedArray.getColor(
R.styleable.LetterSlideView_frontcolor, Color.RED);
typedArray.recycle();
這時(shí)有了文字的大小我們就可以計(jì)算我們要畫(huà)的坐標(biāo)了,使用paint.measureText(letter);方法可以計(jì)算傳入文字letter的寬度,x軸的坐標(biāo)即為
float letter_w = paint.measureText(letter);
letter_x = (int) ((getWidth() - letter_w) / 2);
getWidth()方法獲得控件的總寬度,(總寬度-文字寬度)/2作為起點(diǎn)的x坐標(biāo)可以讓該文字畫(huà)在控件的正中間。因?yàn)槲覀冃枰獜腁畫(huà)到Z所以寫(xiě)個(gè)26大小的for循環(huán),每次y軸+一個(gè)文字的高度就可以了,為了讓26個(gè)字母平均在整個(gè)空間的高度則用控件總高度getHeight()/26作為文字的高度。代碼如下:
String letter = (char) ('A' + i) + "";
letter_h = getHeight() / 26;
float letter_w = paint.measureText(letter);
letter_x = (int) ((getWidth() - letter_w) / 2);
canvas.drawText(letter, letter_x, letter_h * (i + 1), paint);
3.動(dòng)畫(huà)效果,按下控件有半透明背景以及點(diǎn)擊的文字變色。
1)重寫(xiě)onTouchEvent方法獲得觸摸事件,按下的時(shí)候,文字變色,出現(xiàn)背景。松開(kāi)的時(shí)候回歸原狀。
2)文字變色實(shí)現(xiàn):調(diào)用event.getY()方法獲取觸摸事件的高度除以文字的高度這可以獲得是第幾個(gè)字母為成員變量index賦值,松開(kāi)的時(shí)候設(shè)為-1。然后再畫(huà)字的循環(huán)中判斷是否是該字母并調(diào)用paint的setColor方法。
3)出現(xiàn)背景:當(dāng)按下的時(shí)候?yàn)槌蓡T變量isPressed賦值原理同上,調(diào)用canvas.drawColor方法設(shè)置背景。
4)觸摸事件結(jié)束時(shí)調(diào)用invalidate()方法,系統(tǒng)為執(zhí)行onDraw()方法,注意最后要return ture;表示該事件由我們處理了。
4.自定義控件控制ListvView的顯示。
1)自定義接口為了實(shí)現(xiàn)回調(diào),類似于其他控件的setOnclick機(jī)制,寫(xiě)一個(gè)public的方法用來(lái)獲取接口,在觸摸事件中調(diào)用接口的方法(判斷非空)暴露該空間的index值。
接口:
public interface OnLSVTouchListener {
public void getState(int index);
}
方法:
public void setOnLSVTouchListener(OnLSVTouchListener listener) {
this.listener = listener;
}
暴露控件的index:
if (listener != null) {
listener.getState(index);
}
2)在activity中find該控件并實(shí)現(xiàn)該接口,這時(shí)候我們就獲取到了index也就是第幾個(gè)英文字母,這時(shí)候就需要知道我們的數(shù)據(jù)中那些數(shù)據(jù)是同樣首字母的第一個(gè)。
letter_list.add(0);
for (int i = 0; i < Cheeses.sCheeseStrings.length; i++) {
if (i + 1 < Cheeses.sCheeseStrings.length) {
if (!Cheeses.sCheeseStrings[i].substring(0,1)
.equals(Cheeses.sCheeseStrings[i + 1].substring(0,1))) {
letter_list.add(i + 1);
}
}
}
第一個(gè)數(shù)據(jù)肯定符合要求,然后就是把當(dāng)前的數(shù)據(jù)首字母與后一個(gè)比較不一樣則保存后一個(gè)數(shù)據(jù)進(jìn)我們的new的容器中(代碼沒(méi)優(yōu)化)。
3)實(shí)現(xiàn)控制Listview,調(diào)用listview的setSelection()方法,傳來(lái)的index相當(dāng)于就是我們保存的容器的index,獲取到position設(shè)置就好了。中間顯示的TextView同理。
三、布局使用自定義控件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res/com.example.customview"
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ui_day2_city_customview.MainActivity" >
<ListView
android:id="@+id/lv_city"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<TextView
android:id="@+id/tv_city_letter"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:background="#80000000"
android:textSize="80sp"
android:textColor="#FFFFFF"
android:gravity="center"
android:visibility="gone" />
<com.example.customview.LetterSlideView
android:id="@+id/lsv_city"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_gravity="right" >
</com.example.customview.LetterSlideView>
</FrameLayout>
注意自定義命名空間、幀布局的位置用android:layout_gravity調(diào)整,自定義控件需要完整的包名
到此,相信大家對(duì)“ListView的側(cè)邊字母滑動(dòng)索引怎么實(shí)現(xiàn)”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
網(wǎng)頁(yè)題目:ListView的側(cè)邊字母滑動(dòng)索引怎么實(shí)現(xiàn)
文章路徑:http://www.rwnh.cn/article26/jicgjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、動(dòng)態(tài)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、云服務(wù)器、企業(yè)建站
聲明:本網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)
網(wǎng)頁(yè)設(shè)計(jì)公司知識(shí)