在看Oplayer的时候看见滑动字母来实现listView的内容搜索,所以就把里面的核心的函数扣除来做了一个demo,分为两部分一个是布局,另一个就是代码了,具体的如下:

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:orientation="horizontal" > <FrameLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:orientation="vertical" > <ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="@android:color/transparent"
android:divider="@drawable/ic_line"
android:drawSelectorOnTop="false"
android:fadingEdge="none"
android:listSelector="@android:color/transparent"
android:scrollbars="none" /> <TextView
android:id="@+id/first_letter_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="50.0dip"
android:layout_marginTop="49.0dip"
android:background="@drawable/fast_scroller_overlay"
android:gravity="center"
android:paddingBottom="5.0dip"
android:scaleType="center"
android:textSize="43.0dip"
android:visibility="gone" />
</FrameLayout> <ImageView
android:id="@+id/alphabet_scroller"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:paddingRight="0dip"
android:scaleType="fitXY"
android:src="@drawable/alphabet_scroller_bg" /> </LinearLayout>

里面涉及到了一个ImageView的图片

<?xml version="1.0" encoding="UTF-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/contact_list_scroll_pressed" />
<item android:drawable="@drawable/contact_list_scroll_normal" />
</selector>

具体的代码如下:

package com.example.test;

import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView; public class Main6 extends Activity{ ListView iListView;
TextView showChar;
ImageView myChar;
List<String> iList;
MAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main5);
iListView = (ListView)findViewById(R.id.list);
showChar = (TextView)findViewById(R.id.first_letter_overlay);
myChar = (ImageView)findViewById(R.id.alphabet_scroller);
iList = initChar();
Log.e("t", " "+iList.size());
mAdapter = new MAdapter();
iListView.setAdapter(mAdapter);
myChar.setClickable(true);
myChar.setOnTouchListener(asOnTouch);
} /**
* A-Z
*/
private OnTouchListener asOnTouch = new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:// 0
myChar.setPressed(true);
showChar.setVisibility(View.VISIBLE);
mathScrollerPosition(event.getY());
break;
case MotionEvent.ACTION_UP:// 1
myChar.setPressed(false);
showChar.setVisibility(View.GONE);
break;
case MotionEvent.ACTION_MOVE:
mathScrollerPosition(event.getY());
break;
}
return false;
}
};
/**
* 显示字符
*
* @param y
*/
private void mathScrollerPosition(float y) {
int height = myChar.getHeight();
float charHeight = height / 28.0f;
char c = 'A';
if (y < 0)
y = 0;
else if (y > height)
y = height; int index = (int) (y / charHeight) - 1;
if (index < 0)
index = 0;
else if (index > 25)
index = 25; String key = String.valueOf((char) (c + index));
showChar.setText(key); int position = 0;
if (index == 0)
iListView.setSelection(0);
else if (index == 25)
iListView.setSelection(mAdapter.getCount() - 1);
else {
if (mAdapter != null) {
for (int i = 0; i < iList.size(); i++) {
if (iList.get(i).startsWith(key)) {
iListView.setSelection(position);
break;
}
position++;
}
}
}
}
/**
* ListView的数据
* @return
*/
public List<String> initChar(){
List<String> list = new ArrayList<String>();
char c = 'A';
for (int i = 0; i < 26; i++) {
list.add(String.valueOf((char) (c + i)));
list.add(String.valueOf((char) (c + i)));
list.add(String.valueOf((char) (c + i)));
}
return list;
}
class MAdapter extends BaseAdapter{ @Override
public int getCount() {
return iList.size();
} @Override
public Object getItem(int position) {
return iList.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text = new TextView(getApplicationContext());
text.setText(iList.get(position));
return text;
} }
}

为了方便我就用了字母来填充listView,其实原理是很简单的。具体的显示如下:

拖动后的显示效果:

里面使用的那两张图片也发出来

android 按字母搜索的更多相关文章

  1. 【随笔】Android应用市场搜索优化(ASO)

    参考了几篇网上的关于Android应用市场搜索优化(ASO)的分析,总结几点如下: 优化关键字:举例目前美团酒店在各Android市场上的关键字有“美团酒店.钟点房.团购.7天”等等,而酒店类竞对在“ ...

  2. 详细解读Android中的搜索框—— SearchView

    以前总是自己写的 今天看看别人做的 本篇讲的是如何用searchView实现搜索框,其实原理和之前的没啥差别,也算是个复习吧. 一.Manifest.xml 这里我用一个activity进行信息的输入 ...

  3. 【Solr】 solr对拼音搜索和拼音首字母搜索的支持

    问:对于拼音和拼音首字母的支持,当你在搜商品的时候,如果想输入拼音和拼音首字母就给出商品的信息,怎么办呢? 实现方式有2种,但是他们其实是对应的.  用lucene实现 1.建索引, 多建一个索引字段 ...

  4. 做个简单的Android列表字母索引控件

    相信大家在许多App中都见到过带字母索引的界面,比如我最近看到的这个开源控件: WaveSideBar 很酷是不是?!!!如果加在例如联系人列表界面上,大大提升了用户体验. 那么这个索引控件要怎么做呢 ...

  5. Android 联系人字母排序(仿微信)

    现在很多APP只要涉及到联系人的界面,几乎都会采取字母排序以及导航的方式.作为程序猿,这种已经普及的需求还是需要学习的,于是小生开始了在网上默默的学习之路,网上学习的资料质量参差不齐,不过也有很不错的 ...

  6. Android 根据EditText搜索框ListView动态显示数据

    根据EditText搜索框ListView动态显示数据是根据需求来的,觉得这之中涉及的东西可能比较的有意思,所以动手来写一写,希望对大家有点帮助. 首先,我们来分析下整个过程: 1.建立一个layou ...

  7. iOS拼音搜索,拼音首字母搜索

    扩展了一下 搜索框,能够实现拼音和首字母模糊搜索 基本搜索 上一篇文章 #import "NSString+utility.h" @interface WJWPinyinSearc ...

  8. 详细解读Android中的搜索框(三)—— SearchView

    本篇讲的是如何用searchView实现搜索框,其实原理和之前的没啥差别,也算是个复习吧. 一.Manifest.xml 这里我用一个activity进行信息的输入和展示,配置方式还是老样子,写一个输 ...

  9. 详细解读Android中的搜索框(四)—— Searchable配置文件

    <?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android=" ...

随机推荐

  1. The reference to entity "characterEncoding" must end with the ';' delimiter

    数据源配置时加上编码转换格式后出问题了: The reference to entity "characterEncoding" must end with the ';' del ...

  2. Qt下如何修改文件的时间(全平台修改)

    提供一个全平台修改文件的时间的方法,希望大家喜欢 /* UTIME.C: This program uses _utime to set the * file-modification time to ...

  3. hdu 4679 (树形DP)

    题意:给一棵树,边的权值都是1,摧毁每条边是有代价的,选择摧毁一条边,把一棵树分成两部分,求出两部分中距离最大的两点的距离,求出距离*代价最小的边,多条的话输出序号最小的. 刚开始理解错题意了,wro ...

  4. JavaScript 高级程序设计(第3版)笔记——chapter4:变量、作用域和内存问题

    Chapter4 变量.作用域和内存问题 l  理解基本类型和引用类型的值 l  理解执行环境 l  理解垃圾收集 4.1基本类型和引用类型的值 l  ECMAScript变量包含两种不同数据类型的值 ...

  5. 【转载】Java重构示例【1】

    序言 本文通过Java示例代码片段展示了常用重构原则和技巧,供初级开发人员参考.精致的代码能够清楚传达作者的意图,精致的代码是最好的注释,精致的代码非常容易维护和扩展.程序员阅读精致的代码如同大众欣赏 ...

  6. Android中的TextView实现多行显示省略号

    今天遇到一个问题就是TextView显示内容的时候,多行显示的时候,显示省略号的问题,刚开始没有找到一个好的办法,只找到一个自定义TextView组件的方法,然而今天在贴吧中找到一个更好,更简便的方法 ...

  7. Android保存之SharedPreferences

    Android中一共有四种存储方式: SharedPreferences 为其中的一种,具体还是看代码: package com.wyl.preferencetest; import android. ...

  8. Python之路Day9

    摘要: 协程 Select\Poll\Epoll异步IO与事件驱动 Python连接MySQL数据库操作 RabbitMQ队列 Redis\Memcached缓存 Paramiko Twsited网络 ...

  9. 使用zabbix监控nginx

    在zabbix agentd客户端上,查看nginx是否加载了--with-http_stub_status_module.因为zabbix监控nginx是根据 nginx的Stub Status模块 ...

  10. 将时间显示为“刚刚”“n分钟/小时前”等

    在很多场合为了显示出信息的及时性,一般会将时间显示成“刚刚”,“5分钟前”,“3小时前”等,而不是直接将时间打印出来.比如微博,SNS类应用就最长用到这个功能.而一般存储在数据库中的时间格式为 Uni ...