Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android仿微信通讯录滑动快速定位功能大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

先给大家展示下效果图:

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,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,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<CompanyEntity> 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仿微信通讯录滑动快速定位功能全部内容,希望文章能够帮你解决Android仿微信通讯录滑动快速定位功能所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。