重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Android仿微信通讯录滑动快速定位功能

先给大家展示下效果图:

十年的海曙网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网整合营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整海曙建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联公司从事“海曙网站设计”,“海曙网站推广”以来,每个客户项目都认真落实执行。

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

使用

public class CompanyActivity extends BaseActivity implements IndexBar.OnIndexChangedListener {
  @Bind(R.id.lv_company)
  ListView lvCompany;
  @Bind(R.id.ib_indicator)
  IndexBar ibIndicator;
  @Bind(R.id.tv_indicator)
  TextView tvIndicator;
  private List mCompanyList = new ArrayList<>();
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_company);
    // ...
  }
  @Override
  public void onIndexChanged(String index, boolean showIndicator) {
    int position = -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坐标计算出手指所在的索引位置,要注意临界值。

以上所述是小编给大家介绍的Android仿微信通讯录滑动快速定位功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!


本文标题:Android仿微信通讯录滑动快速定位功能
网站链接:http://cqcxhl.com/article/jscigh.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP