Android仿微信通讯录滑动快速定位功能-创新互联

先给大家展示下效果图:

在七星等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、成都做网站 网站设计制作按需网站策划,公司网站建设,企业网站建设,品牌网站设计,营销型网站,成都外贸网站建设,七星网站建设费用合理。

实现代码如下:

下面简单说下实现原理。

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);
  }
}

当前文章:Android仿微信通讯录滑动快速定位功能-创新互联
本文链接:https://www.cdcxhl.com/article26/jogcg.html

成都网站建设公司_创新互联,为您提供云服务器响应式网站电子商务企业建站域名注册网站制作

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

手机网站建设