Android 索引的实现
生活随笔
收集整理的這篇文章主要介紹了
Android 索引的实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在剛學習Android的時候就想自己手寫一個索引,但是當時還是彩筆,就一直沒做,昨天寫了一個索引,今天貼出來,以備后用,先上圖:
由于列表是我自己封裝的,可能必看到展示頁面的時候會有陌生感,但是并不復雜,很好理解,好了不廢話了,操起鍵盤就是干。
自定義索引欄:
注釋加的已經(jīng)很清楚了,還是不理解的@我
package app.project.view;import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View;import app.project.R;/*** @author qly*/ public class MySlideBar extends View {// 是否點擊private boolean showBkg = false;// 監(jiān)聽面板是否點擊接口OnTouchingLetterChangedListener onTouchingLetterChangedListener;// 26個字母public static String[] b = { "#", "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" };// 選擇的值int choose = -1;private Context context;// 畫筆Paint paint = new Paint();public MySlideBar(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);this.context = context;}public MySlideBar(Context context, AttributeSet attrs) {super(context, attrs);this.context = context;}public MySlideBar(Context context) {super(context);this.context = context;}/*** 重寫這個方法*/protected void onDraw(Canvas canvas) {super.onDraw(canvas);//背景色繪制為灰色if (showBkg) {canvas.drawColor(Color.parseColor("#40000000"));}int height = getHeight();int width = getWidth();int singleHeight = height / b.length;for (int i = 0; i < b.length; i++) {paint.setAntiAlias(true);paint.setTextSize(context.getResources().getDimensionPixelSize(R.dimen.sidebar_textsize));// 點擊的字體和26個字母中的任意一個相等就if (i == choose) {paint.setColor(Color.parseColor("#3399ff"));paint.setFakeBoldText(true);}// 字體的X坐標float xPos = width / 2 - paint.measureText(b[i]) / 2;// 字體的Y坐標float yPos = singleHeight * i + singleHeight;canvas.drawText(b[i], xPos, yPos, paint);// 還原畫布paint.reset();}}/*** 點擊事件*/@Overridepublic boolean dispatchTouchEvent(MotionEvent event) {// 點擊的Y坐標final float y = event.getY();final int oldChoose = choose;// 得到當前的值(當前點擊坐標/控件高度*27)final int c = (int) (y / getHeight() * b.length);// 根據(jù)點擊的狀態(tài)不同做出不同的處理switch (event.getAction()) {// 按下已經(jīng)開始case MotionEvent.ACTION_DOWN:// 將開關設置為trueshowBkg = true;if (oldChoose != c && onTouchingLetterChangedListener != null) {if (c >= 0 && c < b.length) {// 當當前點擊的值綁定監(jiān)聽// 這個監(jiān)聽在本頁面中做的是接口。實際調用是在MainActiv中。也就是說我們調用這個接口會執(zhí)行MainActivtiy的方法onTouchingLetterChangedListener.onTouchingLetterChanged(b[c]);choose = c;// 刷新界面invalidate();}}break;// 松開為完成點擊case MotionEvent.ACTION_MOVE:if (oldChoose != c && onTouchingLetterChangedListener != null) {if (c >= 0 && c < b.length) {onTouchingLetterChangedListener.onTouchingLetterChanged(b[c]);choose = c;invalidate();}}break;// 完成松開 還原數(shù)據(jù) 并刷新界面case MotionEvent.ACTION_UP:showBkg = false;choose = -1;invalidate();break;}return true;}/*** 向外公開的方法*/public void setOnTouchingLetterChangedListener(OnTouchingLetterChangedListener onTouchingLetterChangedListener) {this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;}/*** 接口*/public interface OnTouchingLetterChangedListener {void onTouchingLetterChanged(String s);}}列表展示頁面
package app.project;import android.graphics.Color; import android.os.Bundle; import android.support.annotation.LayoutRes; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map;import app.project.recycler.DevBaseRecyclerViewActivity; import app.project.recycler.ListBaseRecyclerAdapter; import app.project.utils.BaseStickyBean; import app.project.model.ContactHeader; import app.project.model.ItemsEntity; import app.project.view.MySlideBar;public class IndexActivity extends DevBaseRecyclerViewActivity<BaseStickyBean> {List<ItemsEntity> list=new ArrayList<>();@Overridepublic void setContentView(@LayoutRes int layoutResID) {super.setContentView(R.layout.activity_index);}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);MySlideBar mySideBar= (MySlideBar) findViewById(R.id.my_side_bar);mySideBar.setOnTouchingLetterChangedListener(new MySlideBar.OnTouchingLetterChangedListener() {@Overridepublic void onTouchingLetterChanged(String s) {for (int i = 1; i < mList.size(); i++) {if (mList.get(i).getStickItem().equalsIgnoreCase(s)) {mRecyclerView.scrollToPosition(i);}}}});}@Overridepublic void loadData() {enableSwipeRefresh(false);list.add(new ItemsEntity("阿文"));list.add(new ItemsEntity("Andy"));list.add(new ItemsEntity("梁朝偉"));list.add(new ItemsEntity("周杰倫"));list.add(new ItemsEntity("蔡依林"));list.add(new ItemsEntity("陳慧琳"));list.add(new ItemsEntity("房祖名"));list.add(new ItemsEntity("羅啟新"));list.add(new ItemsEntity("陳浩民"));list.add(new ItemsEntity("蘇友朋"));list.add(new ItemsEntity("胡彥斌"));list.add(new ItemsEntity("葉德嫻"));list.add(new ItemsEntity("孫燕姿"));list.add(new ItemsEntity("歐陽震華"));list.add(new ItemsEntity("郭富城"));list.add(new ItemsEntity("麥兆輝"));list.add(new ItemsEntity("向海嵐 "));sortList(list);}public void sortList(List<ItemsEntity> list) {Map<String, List<ItemsEntity>> map = new HashMap<>();for (int i = 0; i < list.size(); i++) {ItemsEntity myContact = list.get(i);List<ItemsEntity> temp = map.get(myContact.getStickItem());if (temp == null) {temp = new ArrayList<>();map.put(myContact.getStickItem(), temp);}temp.add(myContact);}List<BaseStickyBean> finalList = new ArrayList<>();List<String> keys = new ArrayList<>(map.keySet());Collections.sort(keys);for (int i = 0; i < keys.size(); i++) {finalList.add(new ContactHeader(keys.get(i)));finalList.addAll(map.get(keys.get(i)));}mList.addAll(finalList);setListAdapter();}@Overridepublic void onItemClick(ListBaseRecyclerAdapter.YFViewHolder holder, BaseStickyBean o, int position, long id) {}@Overridepublic View getView(ViewGroup parent, int viewType) {View view=LayoutInflater.from(this).inflate(R.layout.item,parent,false);if(viewType==0){return view;}else if(viewType==1){view.setBackgroundColor(Color.parseColor("#CDD7E2"));return view;}return null;}@Overridepublic void convertObject2View(ListBaseRecyclerAdapter.YFViewHolder holder, int position) {if(holder.getItemViewType()==1){holder.setText(R.id.tv, mList.get(position).getStickItem());}else if(holder.getItemViewType()==0){ItemsEntity item= (ItemsEntity) mList.get(position);holder.setText(R.id.tv,item.getName() );}}@Overridepublic int getItemViewType(int position) {BaseStickyBean bean = mList.get(position);if (bean instanceof ContactHeader) {return 1;}return 0;}}數(shù)據(jù)排序的實現(xiàn)
怎么獲取的姓名首字母的呢?
我們使用了一個jar包,pinyin4j-2.5.0.jar就是它。有興趣的可以看下。 需要源碼@我
總結
以上是生活随笔為你收集整理的Android 索引的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Centos 8 安装 Openbrav
- 下一篇: Elasticsearch02