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, 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通讯录怎么实现定位,Android仿微信通讯录滑动快速定位功能相关推荐
- 【uniapp前端组件】仿微信通讯录列表组件
仿微信通讯录列表组件 示例图 前言 仿微信通讯录列表组件,可实现通讯列表以及选择多个联系人功能. 组件介绍 本组件有三个自定义组件构成,都已经集成在bugking7-contact-list中,该组件 ...
- Android仿微信通讯录
Android仿微信通讯录 分3部: 1.listview实现显示头像.名字(太简单,这里就不写了) 通讯录页面xml布局代码: <LinearLayout xmlns:android=&quo ...
- 【Android 仿微信通讯录 导航分组列表-上】使用ItemDecoration为RecyclerView打造带悬停头部的分组列表
*本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 转载请标明出处: http://blog.csdn.net/zxt0601/article/details/52355199 本文 ...
- android 字母搜索栏,android仿微信通讯录搜索示例(匹配拼音,字母,索引位置)
前言: 仿微信通讯录搜索功能,通过汉字或拼音首字母找到匹配的联系人并显示匹配的位置 一:先看效果图 字母索引 搜索匹配 二:功能分析 1:汉字转拼音 通讯录汉字转拼音(首个字符当考虑姓氏多音字), 现 ...
- android 通讯录 首字母索引,android仿微信通讯录搜索(匹配拼音,字母,索引位置标记颜色)...
前言: 仿微信通讯录搜索功能,通过汉字或拼音首字母找到匹配的联系人并显示匹配的位置 一:先看效果图 字母索引 搜索匹配 二:功能分析 1:汉字转拼音 通讯录汉字转拼音(首个字符当考虑姓氏多音字), 现 ...
- android仿微信语音聊天功能,Android仿微信发送语音消息的功能及示例代码
微信的发送语音是有一个向上取消的,我们使用ontouchlistener来监听手势,然后做出相应的操作就行了. 直接上代码: //语音操作对象 private mediaplayer mplayer ...
- RecyclerView+index索引实现仿微信通讯录
感觉之前写的有点乱,所以有重新整理了一下这个博客: demo下载地址:http://download.csdn.net/detail/qq_34501274/9799175 最近跟朋友聊天,说道博客相 ...
- 仿微信通讯录的Demo----PinnedHeaderListView
仿微信通讯录的Demo--PinnedHeaderListView 侧边栏首字母匹配 + header分组 本示例代码来自网上 这里只贴出效果图,附件Demo源码,仅供学习和以后参考时用 附件 Dem ...
- uni-app 写小程序 索引列表,仿微信通讯录
心里认定了一个女孩 就要好好的珍惜对待她,人生不容辜负,你必须要更加努力 .加油 骚年 uni-app 写小程序 索引列表,仿微信通讯录 去uni-app官网 下载插件 indexlist <m ...
- 仿 微信/QQ 实现小程序功能 -IOS
仿 微信/QQ 实现小程序功能 -IOS 1. 需求 首先,来大致看一下 微信/QQ 小程序的功能. Android端: 点击图标进入小程序,会新开一个任务栈,每个小程序一个任务栈(有的机型所有小程序 ...
最新文章
- visual MySQL 教程_MySql轻松入门系列——第二站 使用visual studio 对mysql进行源码级调试...
- pyspark导出文件
- 046_Unicode对照表十二
- #define va_arg(AP, TYPE)
- 1.4编程基础之逻辑表达式与条件分支 09 判断能否被3,5,7整除
- 依赖 netty spring_面试官:如何写好一个 Spring 组件?懵圈!
- 使用Docker部署Gitlab
- java servlet,action,struts,struts2输出流中文编码问题
- 红旗linux桌面版_瑞星ESM杀毒软件For Linux获红旗兼容性认证
- 算法基础-顺序统计量
- apt-get update 更新失败----如何清除破损而且陈旧的PPA仓库 (W: Failed to fetch http://ppa.launchpad.net/jonathonf/pyt)
- alertmanager配置详解
- C++计算四则表达式的模板
- 超文本标记语言是指Java_超文本标记语言(HTML)
- linux iptable配置
- Skype for Business 中的键盘快捷方式
- 湿指纹识别技术 – 机遇与挑战并存
- 原生 hadoop安装
- 论文阅读笔记:ProjectionNet: Learning Efficient On-Device Deep Networks Using Neural Projections
- 疯子的匣子_人工智能扮演``疯子''来像孩子一样学习语法