android 按字母搜索
在看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 按字母搜索的更多相关文章
- 【随笔】Android应用市场搜索优化(ASO)
参考了几篇网上的关于Android应用市场搜索优化(ASO)的分析,总结几点如下: 优化关键字:举例目前美团酒店在各Android市场上的关键字有“美团酒店.钟点房.团购.7天”等等,而酒店类竞对在“ ...
- 详细解读Android中的搜索框—— SearchView
以前总是自己写的 今天看看别人做的 本篇讲的是如何用searchView实现搜索框,其实原理和之前的没啥差别,也算是个复习吧. 一.Manifest.xml 这里我用一个activity进行信息的输入 ...
- 【Solr】 solr对拼音搜索和拼音首字母搜索的支持
问:对于拼音和拼音首字母的支持,当你在搜商品的时候,如果想输入拼音和拼音首字母就给出商品的信息,怎么办呢? 实现方式有2种,但是他们其实是对应的. 用lucene实现 1.建索引, 多建一个索引字段 ...
- 做个简单的Android列表字母索引控件
相信大家在许多App中都见到过带字母索引的界面,比如我最近看到的这个开源控件: WaveSideBar 很酷是不是?!!!如果加在例如联系人列表界面上,大大提升了用户体验. 那么这个索引控件要怎么做呢 ...
- Android 联系人字母排序(仿微信)
现在很多APP只要涉及到联系人的界面,几乎都会采取字母排序以及导航的方式.作为程序猿,这种已经普及的需求还是需要学习的,于是小生开始了在网上默默的学习之路,网上学习的资料质量参差不齐,不过也有很不错的 ...
- Android 根据EditText搜索框ListView动态显示数据
根据EditText搜索框ListView动态显示数据是根据需求来的,觉得这之中涉及的东西可能比较的有意思,所以动手来写一写,希望对大家有点帮助. 首先,我们来分析下整个过程: 1.建立一个layou ...
- iOS拼音搜索,拼音首字母搜索
扩展了一下 搜索框,能够实现拼音和首字母模糊搜索 基本搜索 上一篇文章 #import "NSString+utility.h" @interface WJWPinyinSearc ...
- 详细解读Android中的搜索框(三)—— SearchView
本篇讲的是如何用searchView实现搜索框,其实原理和之前的没啥差别,也算是个复习吧. 一.Manifest.xml 这里我用一个activity进行信息的输入和展示,配置方式还是老样子,写一个输 ...
- 详细解读Android中的搜索框(四)—— Searchable配置文件
<?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android=" ...
随机推荐
- Solr4.7从文件创建索引
索引数据源并不会一定来自于数据库.XML.JSON.CSV这类结构化数据,很多时候也来自于PDF.word.html.word.MP3等这类非结构化数据,从这类非结构化数据创建索引,solr也给我们提 ...
- linux shell 执行多个命令的方法
(1)在每个命令之间用:(分号)隔开. (2)在每个命令之间用&&隔开. &&表示:若前一个命令执行成功,才会执行下一个.这样,可确保所有的命令执行完毕后,其执行过程都 ...
- 转:.Net程序员学习Linux最简单的方法
有很多关于Linux的书籍,博客.大多数都会比较“粗暴“的将一大堆的命令塞给读者,从而使很多.NET程序员望而却步.未入其门就路过了. 所以我设想用一种更为平滑的学习方式, 就是在学习命令时,先用纯语 ...
- IT第七天 - 类及其属性、方法的理解,断点调试初识,代码优化总结,编程逻辑培养
IT第七天 上午 类 1.对象:是多个实体抽象出来的共同点集合,对象包括:属性(即实体的特征).方法(即尸体的功能作用) 2.程序中,用类来模拟对象 3.类是抽象的,是对象的类型,是将多个拥有相同属性 ...
- [每日一题] 11gOCP 1z0-052 :2013-09-14 repeated parsing activity.................................A70
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/11699299 正确答案:D SQL语句的执行过程: 1.客户端输入sql语句update ...
- 手把手教你在Windows端搭建Redmine项目管理软件
1.Redmine介绍 Redmine是用Ruby开发的基于web的项目管理软件,是用ROR框架开发的一套跨平台项目管理系统,据说是源于Basecamp的ror版而来,支持多种数据库,有不少自己独特的 ...
- SQL常用语句集合(不断更新)
1.多条件 查询 上下级 所有数据 select * from OrgUnit where (ParentId = '3' or OrgId='3' or ParentId in (select Or ...
- if语句求三个数中最大的
Console.WriteLine("请输入第一个数:"); int a = Convert.ToInt32( Console.ReadLine()); Console.Write ...
- 转化为用欧几里得算法判断互质的问题D - Wolf and Rabbit
Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbit must h ...
- C#学习之在辅助线程中修改UI控件----invoke方法
Invoke and BeginInvoke 转载地址:http://www.cnblogs.com/worldreason/archive/2008/06/09/1216127.html 在Invo ...