Android微信通讯录滑动快速定位实现

前言

提前祝大家元旦快乐,恭喜发财!

深夜来一发,2015年最后一篇博客。 差不多一个月没写博客了,最近开始实习了,空闲时间少了,人也变懒了^_^ 今天来个比较简单的,先看效果图。

截图

实现

下面简单说下实现原理。 源码不长,先贴上来

public class IndexBar extends LinearLayout implements View.OnTouchListener {
    private static final String[] INDEXES = new String[]{"#", "A", "B", "C", "D", "E", "F", "G", "H",
            "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    private static final int TOUCHED_BACKGROUND_COLOR = 0x40000000;
    private OnIndexChangedListener mListener;

    public void setOnIndexChangedListener(OnIndexChangedListener listener) {
        mListener = listener;
    }

    public IndexBar(Context context) {
        this(context, null);
    }

    public IndexBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IndexBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.IndexBar);
        float indexTextSize = ta.getDimension(R.styleable.IndexBar_indexTextSize, Utils.sp2px(getContext(), 12));
        int indexTextColor = ta.getColor(R.styleable.IndexBar_indexTextColor, 0xFF616161);
        ta.recycle();

        setOrientation(VERTICAL);
        setOnTouchListener(this);
        for (String index : INDEXES) {
            TextView text = new TextView(getContext());
            text.setText(index);
            text.setTextSize(TypedValue.COMPLEX_UNIT_PX, indexTextSize);
            text.setTextColor(indexTextColor);
            text.setGravity(Gravity.CENTER);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);
            text.setLayoutParams(params);
            addView(text);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                setBackgroundColor(TOUCHED_BACKGROUND_COLOR);
                handle(v, event);
                return true;
            case MotionEvent.ACTION_MOVE:
                handle(v, event);
                return true;
            case MotionEvent.ACTION_UP:
                setBackgroundColor(Color.TRANSPARENT);
                handle(v, event);
                return true;
        }
        return super.onTouchEvent(event);
    }

    private void handle(View v, MotionEvent event) {
        int y = (int) event.getY();
        int height = v.getHeight();
        int position = INDEXES.length * y / height;
        if (position < 0) {
            position = 0;
        } else if (position >= INDEXES.length) {
            position = INDEXES.length - 1;
        }

        String index = INDEXES[position];
        boolean showIndicator = event.getAction() != MotionEvent.ACTION_UP;
        if (mListener != null) {
            mListener.onIndexChanged(index, showIndicator);
        }
    }

    public interface OnIndexChangedListener {
        void onIndexChanged(String index, boolean showIndicator);
    }
}
复制代码

使用

public class CompanyActivity extends BaseActivity implements IndexBar.OnIndexChangedListener {
    @Bind(R.id.lv_company)
    ListView lvCompany;
    @Bind(R.id.ib_indicator)
    IndexBar ibIndicator;
    @Bind(R.id.tv_indicator)
    TextView tvIndicator;
    private List<CompanyEntity> mCompanyList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_company);

        // ...
    }

    @Override
    public void onIndexChanged(String index, boolean showIndicator) {
        int position = -1;
        for (CompanyEntity company : mCompanyList) {
            if (TextUtils.equals(company.getName(), index)) {
                position = mCompanyList.indexOf(company);
                break;
            }
        }
        if (position != -1) {
            lvCompany.setSelection(position);
        }
        tvIndicator.setText(index);
        tvIndicator.setVisibility(showIndicator ? View.VISIBLE : View.GONE);
    }
}
复制代码

继承自LinearLayout,添加了26个字母索引TextView,当手指滑动时通知Activity更新界面。 核心是OnTouchListener,手指滑动的时候根据当前Y坐标计算出手指所在的索引位置,要注意临界值。

源码下载

迁移自我的CSDN博客 2015.12.31

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值