上一篇android通讯录实例(一)地址:http://blog.youkuaiyun.com/specialshoot/article/details/50651080
这一此我们把UI上最重要的部分IndexableListView说明,这个项目可以在github上找到。https://github.com/woozzu/IndexableListView
源码下载下来我们发现核心代码有两片,IndexableListView和IndexScroller这两个文件。其中IndexScroller是右侧字母滚动条的代码,IndexableListView是继承自ListView将滚动条整合到ListView的代码。
IndexScroller
IndexScroller.java源代码:
/*
* Copyright 2011 woozzu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.woozzu.android.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.widget.Adapter;
import android.widget.ListView;
import android.widget.SectionIndexer;
public class IndexScroller {
private float mIndexbarWidth;//索引条宽度
private float mIndexbarMargin; //索引条外边距
private float mPreviewPadding; //索引条内边距
private float mDensity; //颜色密度(用于控制颜色透明度)
private float mScaledDensity; //缩放密度
private float mAlphaRate;//透明度
private int mState = STATE_HIDDEN; //索引条状态,默认为隐藏
private int mListViewWidth; //listview宽度
private int mListViewHeight;//listview高度
private int mCurrentSection = -1; //当前索引
private boolean mIsIndexing = false;//是否正在索引
private ListView mListView = null;//listview实例
private SectionIndexer mIndexer = null;//列表适配器
private String[] mSections = null;//存放索引字符的数组
private RectF mIndexbarRect;//索引条背景图形
//索引条状态
private static final int STATE_HIDDEN = 0; //隐藏
private static final int STATE_SHOWING = 1; //正在显示
private static final int STATE_SHOWN = 2; //已显示
private static final int STATE_HIDING = 3; //正在隐藏
public IndexScroller(Context context, ListView lv) {
mDensity = context.getResources().getDisplayMetrics().density;//获取密度值
mScaledDensity = context.getResources().getDisplayMetrics().scaledDensity;//获得缩放密度值
mListView = lv;//listview实例
setAdapter(mListView.getAdapter()); //设置适配器
mIndexbarWidth = 20 * mDensity;
mIndexbarMargin = 10 * mDensity;
mPreviewPadding = 5 * mDensity;
}
public void draw(Canvas canvas) {
if (mState == STATE_HIDDEN)
return;
// mAlphaRate determines the rate of opacity
Paint indexbarPaint = new Paint();//用于显示当前滑动到字母的背景画笔(屏幕中间大字)
indexbarPaint.setColor(Color.BLACK);
indexbarPaint.setAlpha((int) (64 * mAlphaRate));
indexbarPaint.setAntiAlias(true);//抗锯齿
canvas.drawRoundRect(mIndexbarRect, 5 * mDensity, 5 * mDensity, indexbarPaint);
if (mSections != null && mSections.length > 0) {
// Preview is shown when mCurrentSection is set
if (mCurrentSection >= 0) {
Paint previewPaint = ne