在看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. 通过Qt样式表定制程序外观(比较通俗易懂)

    1. 何为Qt样式表[喝小酒的网摘]http://blog.hehehehehe.cn/a/10270.htm2. 样式表语法基础3. 方箱模型4. 前景与背景5. 创建可缩放样式6. 控制大小7. ...

  2. iOS SDK:预览和打开文档

    iOS中的沙盒可以让平台更加的安全,这也是沙盒给用户带来的最主要好处.不过由于沙盒的严格限制,导致程序之间共享数据比较麻烦.一般在程序间共享文档可以通过UIDocumentInteractionCon ...

  3. Windows XP硬盘安装Ubuntu 12.04双系统图文详解

    Windows XP硬盘安装Ubuntu 12.04双系统图文详解 Ubuntu 12.04 LTS版本于2012年4月26日发布,趁着五一放假,赶紧在自己的Windows XP的电脑上安装下Ubun ...

  4. 在web page中使鼠标右击失效的几种方法

    这里主要介绍两种方法,一种是使用js来处理,还有一种是在html属性中设置. 方法一:js 1: <script language="javascript"> docu ...

  5. Week2(9月19日):增加一个CodeFirst的例子来说明

    Part I:提问  =========================== 1.上堂课中我们使用了()数据库,它是()可部署的,只需要将相应的()文件添加到应用程序的()文件夹,就可以使用了,该数据 ...

  6. 数据挖掘算法学习(三)NaiveBayes算法

    算法简单介绍 NBC是应用最广的分类算法之中的一个.朴素贝叶斯模型发源于古典数学理论,有着坚实的数学基础,以及稳定的分类效率.同一时候,NBC模型所需预计的參数非常少,对缺失数据不太敏感,算法也比較简 ...

  7. POJ 3422 Kaka&#39;s Matrix Travels (最小费用最大流)

    POJ 3422 Kaka's Matrix Travels 链接:http://poj.org/problem? id=3422 题意:有一个N*N的方格,每一个方格里面有一个数字.如今卡卡要从左上 ...

  8. Android模拟器的文件目录介绍

    文件存放在 .avd文件夹下 .ini为对应的配置文件     打开.avd文件夹 *.lock文件夹保存的是模拟器的一下数据,当模拟器正常关闭时这些文件夹都会被自动删除. 当模拟器无法开启的时候可以 ...

  9. 在C#中使用C++编写的类1

    转载地址:http://blog.csdn.net/starlee/article/details/2864588 现在在Windows下的应用程序开发,VS.Net占据了绝大多数的份额.因此很多以前 ...

  10. Cocos Studio和Cocos2d-x版本对应关系

    链接地址:http://www.cocoachina.com/bbs/read.php?tid=182077 可以在cocos2d.cpp中查看2d-x的版本信息.   版本对应列表: Studio2 ...