Android仿微信聯(lián)系人字母排序效果
本文實例為大家分享了Android聯(lián)系人字母排序的具體代碼,供大家參考,具體內(nèi)容如下

實現(xiàn)思路:首先說下布局,整個是一個相對布局,最下面是一個listview,listview上面是一個自定義的view(右邊顯示字母),最上面是一個textview(屏幕中間的方塊)。
首先說一下右邊自定義view,字母是畫到view上面的,首先計算一下view的高度,然后除以存放字母數(shù)組的長的,得到每個字符的高度;每個字母的寬度都是一樣的,所以這里直接設(shè)置30sp;
listview顯示的是108個梁山好漢的名字;
項目里面使用了一個pinyin4j.jar把每個名字轉(zhuǎn)換為拼音,然后使用charAt(0)獲得拼音的第一個字母;
listview的每個item是一個線性布局包裹的兩個textview,上面的tv顯示的是拼音的第一個字母,下面的tv顯示的就是名字;在adapter判斷當(dāng)前條目和上一個條目的拼音首字母是否相同,如果相同隱藏當(dāng)前item上面的tv;
然后在自定義view設(shè)置一個自定義監(jiān)聽;當(dāng)點擊自定義view的時候根據(jù)高度判斷當(dāng)前點擊的是那個字母,然后根據(jù)字母設(shè)置listview要跳轉(zhuǎn)的位置;
首先看下布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@android:color/transparent" >
</ListView>
<com.wxcontacts.www.view.QuickIndexBar
android:id="@+id/quickindexbar"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
/>
<!-- 默認(rèn)隱藏,當(dāng)點擊自定義view的時候顯示 -->
<TextView
android:visibility="gone"
android:id="@+id/tv_hint"
android:gravity="center"
android:layout_centerInParent="true"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/bg_text"
android:text="A"
android:textSize="24sp"/>
</RelativeLayout>
先不著急看MainActivity代碼首先看下自定義view的代碼:
//自定義view
public class QuickIndexBar extends View{
//畫筆
private Paint paint;
String[] lettres={"↑","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S"
,"T","U","V","W","X","Y","Z","↓"};
//自定義view的寬高
private int viewHight;
private int viewWidth;
//每個文本的高度;(文本高度等于view高度除以文本個數(shù))
private int cellHeight;
//文本的高度
private float textHeight;
private int currentIndex=-1;
public OnLetterChangeListen onLetterChangeListen;
public OnLetterChangeListen getOnLetterChangeListen(){
return onLetterChangeListen;
}
public void setOnLetterChangeListener(OnLetterChangeListen onLetterChangeListen){
this.onLetterChangeListen=onLetterChangeListen;
}
//自定義一個字母改變的監(jiān)聽
public interface OnLetterChangeListen{
void onLetterChange(String letter);
void onResert();
}
public QuickIndexBar(Context context) {
this(context,null);
}
public QuickIndexBar(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public QuickIndexBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
paint=new Paint();
//設(shè)置字體顏色;默認(rèn)為黑色;我們這里使用黑色
//paint.setColor(Color.WHITE);
//設(shè)置字體大小
paint.setTextSize(20);
//抗鋸齒
paint.setAntiAlias(true);
//獲取字體寬高,因為每個字體寬度不一樣,所以獲得寬度必須放在循環(huán)中做
//首先獲取字體的屬性
FontMetrics fontMetrics = paint.getFontMetrics();
//下邊界 - 上邊界
textHeight = (float) Math.ceil(fontMetrics.descent-fontMetrics.ascent);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//獲取文本的寬高
// getTextWH();
//每個文本的高度;
cellHeight=viewHight/lettres.length;
//通過循環(huán)把字母畫出來
for(int x=0;x<lettres.length;x++){
String text=lettres[x];
//paint給我們提供了一個測量字體寬度的方法
float textWidth = paint.measureText(text);
if(currentIndex==x){
//當(dāng)點擊某個字母的時候變色
paint.setColor(Color.GRAY);
}else{
paint.setColor(Color.BLACK);
}
/*
* 參數(shù)1:要畫的內(nèi)容 參數(shù)23:要畫的位置 參數(shù)4:畫筆
* 參數(shù)23所畫的位置指的是字母左下角坐標(biāo)
*/
canvas.drawText(text,viewWidth/2-textWidth/2,cellHeight/2+textHeight/2+cellHeight*x,paint);
}
}
//測量view的寬高
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
viewHight =getMeasuredHeight();
viewWidth =getMeasuredWidth();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//計算當(dāng)前點擊的字母,寬度不用考慮,因為寬度都是一樣的,計算高度即可,根據(jù)你點擊或者移動的高度計算你當(dāng)前所點擊的是哪個字母
float downY=event.getY();
currentIndex = (int)downY/cellHeight;
if(currentIndex<0 || currentIndex>lettres.length-1){
}else{
if(onLetterChangeListen!=null){
onLetterChangeListen.onLetterChange(lettres[currentIndex]);
}
}
//重新繪制;相當(dāng)于重新調(diào)用onDraw()方法
invalidate();
break;
case MotionEvent.ACTION_MOVE:
float moveY=event.getY();
currentIndex = (int)moveY/cellHeight;
if(currentIndex<0 || currentIndex>lettres.length-1){
}else{
if(onLetterChangeListen!=null){
onLetterChangeListen.onLetterChange(lettres[currentIndex]);
}
}
//重新繪制
invalidate();
break;
case MotionEvent.ACTION_UP:
currentIndex=-1;
if(onLetterChangeListen!=null){
onLetterChangeListen.onResert();
}
break;
default:
break;
}
return true;
}
}
下面我為大家準(zhǔn)備了一張計算字母xy坐標(biāo)的圖片:

最后是MainActivity的代碼:
public class MainActivity extends Activity {
private com.wxcontacts.www.view.QuickIndexBar quickIndexBar;
private ListView mListView;
//當(dāng)點擊自定義view的時候屏幕中間顯示一個方塊,顯示當(dāng)前點擊的字母,默認(rèn)是隱藏的,當(dāng)點擊的時候才顯示
private TextView tvHint;
private List<HaoHan> hhList=new ArrayList<HaoHan>();
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
tvHint=(TextView) findViewById(R.id.tv_hint);
context=this;
//listview 和 自定義view
mListView=(ListView) findViewById(R.id.listview);
quickIndexBar=(QuickIndexBar) findViewById(R.id.quickindexbar);
initHHListData();
mListView.setAdapter(new MainListViewAdapter(context,hhList));
//給自定義view設(shè)置自定義監(jiān)聽,當(dāng)點擊到某個字母的時候彈出吐司顯示這個字母;
//當(dāng)點擊字母的時候遍歷集合,找到首字母為點擊字母的HaoHan的下標(biāo),讓listview跳轉(zhuǎn)到響應(yīng)位置
quickIndexBar.setOnLetterChangeListener(new QuickIndexBar.OnLetterChangeListen() {
@Override
public void onLetterChange(String letter) {
quickIndexBar.setBackgroundResource(R.drawable.bg_text);
tvHint.setVisibility(View.VISIBLE);
tvHint.setText(letter);
for(int x=0;x<hhList.size();x++){
if((hhList.get(x).pinyin.charAt(0)+"").equals(letter)){
mListView.setSelection(x);
//找到第一個字母相同的直接結(jié)束,不再向下找;
break;
}
}
}
@Override
//當(dāng)手指彈起的時候此方法執(zhí)行
public void onResert() {
tvHint.setVisibility(View.GONE);
quickIndexBar.setBackgroundResource(Color.TRANSPARENT);
}
});
}
//初始化集合數(shù)據(jù)
private void initHHListData() {
HaoHan hh;
for(int x=0;x<Cheeses.NAMES.length;x++){
hh=new HaoHan(Cheeses.NAMES[x]);
hhList.add(hh);
}
//給集合排序
Collections.sort(hhList);
}
}
源碼下載:http://xiazai.jb51.net/201611/yuanma/AndroidWXcontact(jb51.net).rar
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android5.0以上版本錄屏實現(xiàn)代碼(完整代碼)
這篇文章主要介紹了Android5.0以上版本錄屏實現(xiàn)代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-01-01
Android使用SlidingPaneLayout 實現(xiàn)仿微信的滑動返回
這篇文章主要介紹了Android使用SlidingPaneLayout 實現(xiàn)仿微信的滑動返回,需要的朋友可以參考下2018-04-04
OpenHarmony實現(xiàn)類Android短信驗證碼及倒計時流程詳解
這篇文章主要介紹了OpenHarmony實現(xiàn)類Android短信驗證碼及倒計時流程,發(fā)送短信驗證碼后,一般在界面上都會有一個倒計時的顯示.在安卓中,實現(xiàn)類似的倒計時有多種方式,當(dāng)然背后的基本原理都是設(shè)定一個初始值,然后每過一定的間隔時間執(zhí)行操作2022-11-11
Android調(diào)用系統(tǒng)的發(fā)郵件功能的小例子
這篇文章介紹了Android調(diào)用系統(tǒng)的發(fā)郵件功能的小例子,有需要的朋友可以參考一下2013-08-08

