
先给大家展示下效果图:
实现代码如下:
下面简单说下实现原理。
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仿微信通讯录滑动快速定位功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)