先给大家展示下效果图:
实现代码如下:
下面简单说下实现原理。
代码如下 | 复制代码 |
publicclassIndexBarextendsLinearLayoutimplementsView.OnTouchListener { privatestaticfinalString[] INDEXES =newString[]{"#","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"}; privatestaticfinalintTOUCHED_BACKGROUND_COLOR =0x40000000; privateOnIndexChangedListener mListener; publicvoidsetOnIndexChangedListener(OnIndexChangedListener listener) { mListener = listener; } publicIndexBar(Context context) { this(context,null); } publicIndexBar(Context context, AttributeSet attrs) { this(context, attrs,0); } publicIndexBar(Context context, AttributeSet attrs,intdefStyleAttr) { super(context, attrs, defStyleAttr); init(attrs); } privatevoidinit(AttributeSet attrs) { TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.IndexBar); floatindexTextSize = ta.getDimension(R.styleable.IndexBar_indexTextSize, Utils.sp2px(getContext(),12)); intindexTextColor = ta.getColor(R.styleable.IndexBar_indexTextColor,0xFF616161); ta.recycle(); setOrientation(VERTICAL); setOnTouchListener(this); for(String index : INDEXES) { TextView text =newTextView(getContext()); text.setText(index); text.setTextSize(TypedValue.COMPLEX_UNIT_PX, indexTextSize); text.setTextColor(indexTextColor); text.setGravity(Gravity.CENTER); LinearLayout.LayoutParams params =newLinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,0,1); text.setLayoutParams(params); addView(text); } } @Override publicbooleanonTouch(View v, MotionEvent event) { switch(event.getAction()) { caseMotionEvent.ACTION_DOWN: setBackgroundColor(TOUCHED_BACKGROUND_COLOR); handle(v, event); returntrue; caseMotionEvent.ACTION_MOVE: handle(v, event); returntrue; caseMotionEvent.ACTION_UP: setBackgroundColor(Color.TRANSPARENT); handle(v, event); returntrue; } returnsuper.onTouchEvent(event); } privatevoidhandle(View v, MotionEvent event) { inty = (int) event.getY(); intheight = v.getHeight(); intposition = INDEXES.length * y / height; if(position<0) { position =0; }elseif(position >= INDEXES.length) { position = INDEXES.length -1; } String index = INDEXES[position]; booleanshowIndicator = event.getAction() != MotionEvent.ACTION_UP; if(mListener !=null) { mListener.onIndexChanged(index, showIndicator); } } publicinterfaceOnIndexChangedListener { voidonIndexChanged(String index,booleanshowIndicator); } } |
使用
代码如下 | 复制代码 |
publicclassCompanyActivityextendsBaseActivityimplementsIndexBar.OnIndexChangedListener { @Bind(R.id.lv_company) ListView lvCompany; @Bind(R.id.ib_indicator) IndexBar ibIndicator; @Bind(R.id.tv_indicator) TextView tvIndicator; privateList @Override protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_company); // ... } @Override publicvoidonIndexChanged(String index,booleanshowIndicator) { intposition = -1; for(CompanyEntity company : mCompanyList) { if(TextUtils.equals(company.getName(), index)) { position = mCompanyList.indexOf(company); break; } } if(position != -1) { lvCompany.setSelection(position); } tvIndicator.setText(index); tvIndicator.setVisibility(showIndicator ? View.VISIBLE : View.GONE); } } |
继承自LinearLayout,添加了26个字母索引TextView,当手指滑动时通知Activity更新界面。
核心是OnTouchListener,手指滑动的时候根据当前Y坐标计算出手指所在的索引位置,要注意临界值。
敢达决战官方正版 安卓版v6.7.9
下载敢达决战 安卓版v6.7.9
下载像素火影骨架佐助 (Perseverance Fire Shadow)手机版v1.16
下载要塞英雄 安卓版v33.20.0-39082670-Android
下载梦想城镇vivo最新版本 安卓版v12.0.1
梦想城镇vivo版是这款卡通风模拟经营类手游的渠道服版本,玩
怦然心动的瞬间 安卓版v1.0
怦然心动的瞬间是一款真人向的恋爱互动游戏,在游戏中玩家将扮演
曼尼汉堡店游戏 安卓版v1.0.3
曼尼汉堡店是一款非常好玩的精品恐怖类型冒险游戏,在这款游戏中
现代总统模拟器去广告版 安卓版v1.0.46
现代总统模拟器是一款休闲养成类游戏,可能对于不少的玩家来说都
现代总统模拟器付费完整版 安卓版v1.0.46
现代总统模拟器高级版在商店是需要付费的,相对于普通版本,高级