快速索引 (对View的自定义,黑马程序员)
来源:互联网 发布:思想深度 知乎 编辑:程序博客网 时间:2024/05/28 11:29
* 功能实现:
* 1. A-Z索引的绘制.
* 2. 处理Touch事件.
* 3. 提供使用监听\回调.
* 4. 汉字转换成拼音.
* 5. 进行排序展示.
* 6. 进行分组.
* 7. 将自定义控件和ListView合体.
项目结构:
MainActivity:
package com.itheima.quickindex;
import java.util.ArrayList;
import java.util.Collections;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import com.itheima.quickindex.adapter.HaoHanAdapter;
import com.itheima.quickindex.bean.Person;
import com.itheima.quickindex.ui.QuickIndexBar;
import com.itheima.quickindex.ui.QuickIndexBar.OnLetterUpdateListener;
import com.itheima.quickindex.util.Cheeses;
public class MainActivity extends Activity {
private ListView mMainList;
private ArrayList<Person> persons;
private TextView tv_center;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
QuickIndexBar bar = (QuickIndexBar) findViewById(R.id.bar);
// 设置监听
bar.setListener(new OnLetterUpdateListener() {
@Override
public void onLetterUpdate(String letter) {
// Utils.showToast(getApplicationContext(), letter);
showLetter(letter);
// 根据字母定位ListView, 找到集合中第一个以letter为拼音首字母的对象,得到索引
for (int i = 0; i < persons.size(); i++) {
Person person = persons.get(i);
String l = person.getPinyin().charAt(0) + "";
if(TextUtils.equals(letter, l)){
// 匹配成功
mMainList.setSelection(i);
break;
}
}
}
});
mMainList = (ListView) findViewById(R.id.lv_main);
persons = new ArrayList<Person>();
// 填充数据 , 排序
fillAndSortData(persons);
mMainList.setAdapter(new HaoHanAdapter(MainActivity.this , persons));
tv_center = (TextView) findViewById(R.id.tv_center);
}
private Handler mHandler = new Handler();
/**
* 显示字母
* @param letter
*/
protected void showLetter(String letter) {
tv_center.setVisibility(View.VISIBLE);
tv_center.setText(letter);
mHandler.removeCallbacksAndMessages(null);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
tv_center.setVisibility(View.GONE);
}
}, 2000);
}
private void fillAndSortData(ArrayList<Person> persons) {
// 填充数据
for (int i = 0; i < Cheeses.NAMES.length; i++) {
String name = Cheeses.NAMES[i];
persons.add(new Person(name));
}
// 进行排序
Collections.sort(persons);
}
}
HaoHanAdapter:
package com.itheima.quickindex.adapter;
import java.util.ArrayList;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.itheima.quickindex.R;
import com.itheima.quickindex.bean.Person;
public class HaoHanAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Person> persons;
public HaoHanAdapter(Context mContext, ArrayList<Person> persons) {
this.mContext = mContext;
this.persons = persons;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return persons.size();
}
@Override
public Object getItem(int position) {
return persons.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(convertView == null){
view = view.inflate(mContext, R.layout.item_list, null);
}
ViewHolder mViewHolder = ViewHolder.getHolder(view);
Person p = persons.get(position);
String str = null;
String currentLetter = p.getPinyin().charAt(0) + "";
// 根据上一个首字母,决定当前是否显示字母
if(position == 0){
str = currentLetter;
}else {
// 上一个人的拼音的首字母
String preLetter = persons.get(position - 1).getPinyin().charAt(0) + "";
if(!TextUtils.equals(preLetter, currentLetter)){
str = currentLetter;
}
}
// 根据str是否为空,决定是否显示索引栏
mViewHolder.mIndex.setVisibility(str == null ? View.GONE : View.VISIBLE);
mViewHolder.mIndex.setText(currentLetter);
mViewHolder.mName.setText(p.getName());
return view;
}
static class ViewHolder {
TextView mIndex;
TextView mName;
public static ViewHolder getHolder(View view) {
Object tag = view.getTag();
if(tag != null){
return (ViewHolder)tag;
}else {
ViewHolder viewHolder = new ViewHolder();
viewHolder.mIndex = (TextView) view.findViewById(R.id.tv_index);
viewHolder.mName = (TextView) view.findViewById(R.id.tv_name);
view.setTag(viewHolder);
return viewHolder;
}
}
}
}
Person:
package com.itheima.quickindex.bean;
import com.itheima.quickindex.util.PinyinUtils;
public class Person implements Comparable<Person>{
private String name;
private String pinyin;
public Person(String name) {
super();
this.name = name;
this.pinyin = PinyinUtils.getPinyin(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPinyin() {
return pinyin;
}
public void setPinyin(String pinyin) {
this.pinyin = pinyin;
}
@Override
public int compareTo(Person another) {
return this.pinyin.compareTo(another.getPinyin());
}
}
以上是主要代码,代码地址:http://download.csdn.net/detail/u013453973/9215001
- 快速索引 (对View的自定义,黑马程序员)
- 快速索引 (对View的自定义)
- 自定义View:快速索引实现
- 自定义View之快速索引栏的实现
- Android 自定义View-实现快速索引
- 自定义view实战笔记--快速索引
- 自定义View 实现字母快速索引控件
- 【Android】自定义View快速建立字母索引列表
- android自定义View----通讯录导航栏(快速查找索引)
- Android开发-自定义View-AndroidStudio(十四)快速索引(1)
- Android开发-自定义View-AndroidStudio(十五)快速索引(2)
- 自定义索引view
- 自定义字母索引View
- 自定义索引View
- 自定义的微信快速索引电话本
- 自定义view:快速实现柱状图的绘制
- 自定义控件:快速索引
- android 自定义View对Title的封装
- 洛谷1993小k的农场(差分约束系统)
- 【NGUI】Unity使用Resources加载NGUI的图片
- leetcode contains duplicate III
- UIImage剪切图片为自定义大小
- WebSocket形象理解
- 快速索引 (对View的自定义,黑马程序员)
- C# DataGridView内容合并相同的行
- R绘图lattice包
- Codeforces Round #322 (Div. 2)
- Oracle基本用法
- light oj 1100 - Again Array Queries
- Android获取控件大小的方法
- 万维网WWW简介
- C语言使用正则表达式