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=" ...
随机推荐
- 数据切分——MySql表分区概述
定义: 表的分区指根据可以设置为任意大小的规则,跨文件系统分配单个表的多个部分.实际上,表的不同部分在不同的位置被存储为单独的表.用户所选择的.实现数据分割的规则被称为分区函数,这在M ...
- poj 3176 Cow Bowling(区间dp)
题目链接:http://poj.org/problem?id=3176 思路分析:基本的DP题目:将每个节点视为一个状态,记为B[i][j], 状态转移方程为 B[i][j] = A[i][j] + ...
- (step6.3.5)hdu 1281(棋盘游戏——二分图的完美匹配)
题目大意:本体是中文题.读者可以直接在OJ上看 解题思路: 1)完美匹配:所有的端点都是匹配点 2)对于二分图的完美匹配,我们需要用一个数组来存储匹配点.(而二分图的其他问题(我们则可以直接使用变量来 ...
- HDU 4741 Save Labman No.004 2013 ACM/ICPC 杭州网络赛
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4741 题意:给你两条异面直线,然你求着两条直线的最短距离,并求出这条中垂线与两直线的交点. 需要注意的是 ...
- Courses(最大匹配)
Courses Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- jvm Classload method介绍
1,jvm Classload默认几个重要方法介绍 findClass:Finds and loads the class with the specified name from the URL s ...
- codeblock快捷键
一款开源的C/C++ IDE(集成开发环境),基于wxWidgets GUI体系,跨平台支持. 从别处粘贴的,方便以后看,啦啦啦…… 编辑器 快捷键 功能 Ctrl+Z 恢复上一次操作 Ctrl+Sh ...
- 将vs屏幕上内容重定向到一个log文本中
在需要打印的屏幕内容前面加上一句话: freopen("debug.txt","w",stdout); 结束部分关掉他: fclose(stdout); 参考 ...
- [转]Windows7 64bit下配置Apache+PHP+MySQL
原文链接:http://blog.csdn.net/sbz0409/article/details/12946375 1.准备资料: 首先下载文件:Apache2.4.16 64bit,http:// ...
- 基于visual Studio2013解决C语言竞赛题之0416完数
题目 解决代码及点评 完数的解决方案依旧是遍历,然后写出判断完数的函数进行处理 /************************************************** ...