国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android仿微信聯(lián)系人字母排序效果

2019-12-12 04:27:46
字體:
供稿:網(wǎng)友

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

實(shí)現(xiàn)思路:首先說下布局,整個(gè)是一個(gè)相對(duì)布局,最下面是一個(gè)listview,listview上面是一個(gè)自定義的view(右邊顯示字母),最上面是一個(gè)textview(屏幕中間的方塊)。

首先說一下右邊自定義view,字母是畫到view上面的,首先計(jì)算一下view的高度,然后除以存放字母數(shù)組的長的,得到每個(gè)字符的高度;每個(gè)字母的寬度都是一樣的,所以這里直接設(shè)置30sp;

listview顯示的是108個(gè)梁山好漢的名字;

項(xiàng)目里面使用了一個(gè)pinyin4j.jar把每個(gè)名字轉(zhuǎn)換為拼音,然后使用charAt(0)獲得拼音的第一個(gè)字母;

listview的每個(gè)item是一個(gè)線性布局包裹的兩個(gè)textview,上面的tv顯示的是拼音的第一個(gè)字母,下面的tv顯示的就是名字;在adapter判斷當(dāng)前條目和上一個(gè)條目的拼音首字母是否相同,如果相同隱藏當(dāng)前item上面的tv;

然后在自定義view設(shè)置一個(gè)自定義監(jiān)聽;當(dāng)點(diǎn)擊自定義view的時(shí)候根據(jù)高度判斷當(dāng)前點(diǎn)擊的是那個(gè)字母,然后根據(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)點(diǎn)擊自定義view的時(shí)候顯示 -->   <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;   //每個(gè)文本的高度;(文本高度等于view高度除以文本個(gè)數(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;   }   //自定義一個(gè)字母改變的監(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);     //獲取字體寬高,因?yàn)槊總€(gè)字體寬度不一樣,所以獲得寬度必須放在循環(huán)中做     //首先獲取字體的屬性     FontMetrics fontMetrics = paint.getFontMetrics();     //下邊界 - 上邊界     textHeight = (float) Math.ceil(fontMetrics.descent-fontMetrics.ascent);   }      @Override   protected void onDraw(Canvas canvas) {     super.onDraw(canvas);     //獲取文本的寬高 //   getTextWH();     //每個(gè)文本的高度;     cellHeight=viewHight/lettres.length;          //通過循環(huán)把字母畫出來     for(int x=0;x<lettres.length;x++){       String text=lettres[x];              //paint給我們提供了一個(gè)測(cè)量字體寬度的方法       float textWidth = paint.measureText(text);       if(currentIndex==x){         //當(dāng)點(diǎn)擊某個(gè)字母的時(shí)候變色         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);     }        }      //測(cè)量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:       //計(jì)算當(dāng)前點(diǎn)擊的字母,寬度不用考慮,因?yàn)閷挾榷际且粯拥?,?jì)算高度即可,根據(jù)你點(diǎn)擊或者移動(dòng)的高度計(jì)算你當(dāng)前所點(diǎn)擊的是哪個(gè)字母       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)備了一張計(jì)算字母xy坐標(biāo)的圖片:


最后是MainActivity的代碼:

public class MainActivity extends Activity {    private com.wxcontacts.www.view.QuickIndexBar quickIndexBar;   private ListView mListView;   //當(dāng)點(diǎn)擊自定義view的時(shí)候屏幕中間顯示一個(gè)方塊,顯示當(dāng)前點(diǎn)擊的字母,默認(rèn)是隱藏的,當(dāng)點(diǎn)擊的時(shí)候才顯示   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)點(diǎn)擊到某個(gè)字母的時(shí)候彈出吐司顯示這個(gè)字母;     //當(dāng)點(diǎn)擊字母的時(shí)候遍歷集合,找到首字母為點(diǎn)擊字母的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);             //找到第一個(gè)字母相同的直接結(jié)束,不再向下找;             break;           }         }       }        @Override       //當(dāng)手指彈起的時(shí)候此方法執(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.VeVB.COm/201611/yuanma/AndroidWXcontact(VeVB.COm).rar

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 克拉玛依市| 周口市| 汝南县| 泰宁县| 华蓥市| 定结县| 工布江达县| 阜新| 长寿区| 会东县| 汉川市| 昌黎县| 固始县| 安徽省| 太和县| 齐齐哈尔市| 赤水市| 龙门县| 治多县| 丹棱县| 和顺县| 三穗县| 广昌县| 来安县| 琼海市| 新昌县| 温泉县| 宁远县| 库尔勒市| 高州市| 丽江市| 共和县| 高邑县| 宜兴市| 孟津县| 望奎县| 闽清县| 盈江县| 积石山| 铁力市| 冷水江市|