android通讯录实例(二)

本文是android通讯录实例的第二部分,主要讲解UI组件IndexableListView及其辅助类IndexScroller。IndexScroller负责右侧字母滚动条,而IndexableListView是ListView的扩展,整合了滚动条。文章分析了两个类的核心代码,并介绍了onInterceptTouchEvent和onTouchEvent在事件处理中的作用。最后提到下篇将介绍如何获取手机通讯录信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一篇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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值