十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
先给大家展示下效果图:
在七星等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、成都做网站 网站设计制作按需网站策划,公司网站建设,企业网站建设,品牌网站设计,营销型网站,成都外贸网站建设,七星网站建设费用合理。实现代码如下:
下面简单说下实现原理。
public class IndexBar extends LinearLayout implements View.OnTouchListener { private static final String[] INDEXES = new String[]{"#", "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"}; private static final int TOUCHED_BACKGROUND_COLOR = 0x40000000; private OnIndexChangedListener mListener; public void setOnIndexChangedListener(OnIndexChangedListener listener) { mListener = listener; } public IndexBar(Context context) { this(context, null); } public IndexBar(Context context, AttributeSet attrs) { this(context, attrs, 0); } public IndexBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs); } private void init(AttributeSet attrs) { TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.IndexBar); float indexTextSize = ta.getDimension(R.styleable.IndexBar_indexTextSize, Utils.sp2px(getContext(), 12)); int indexTextColor = ta.getColor(R.styleable.IndexBar_indexTextColor, 0xFF616161); ta.recycle(); setOrientation(VERTICAL); setOnTouchListener(this); for (String index : INDEXES) { TextView text = new TextView(getContext()); text.setText(index); text.setTextSize(TypedValue.COMPLEX_UNIT_PX, indexTextSize); text.setTextColor(indexTextColor); text.setGravity(Gravity.CENTER); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1); text.setLayoutParams(params); addView(text); } } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: setBackgroundColor(TOUCHED_BACKGROUND_COLOR); handle(v, event); return true; case MotionEvent.ACTION_MOVE: handle(v, event); return true; case MotionEvent.ACTION_UP: setBackgroundColor(Color.TRANSPARENT); handle(v, event); return true; } return super.onTouchEvent(event); } private void handle(View v, MotionEvent event) { int y = (int) event.getY(); int height = v.getHeight(); int position = INDEXES.length * y / height; if (position < 0) { position = 0; } else if (position >= INDEXES.length) { position = INDEXES.length - 1; } String index = INDEXES[position]; boolean showIndicator = event.getAction() != MotionEvent.ACTION_UP; if (mListener != null) { mListener.onIndexChanged(index, showIndicator); } } public interface OnIndexChangedListener { void onIndexChanged(String index, boolean showIndicator); } }