SectionIndexer解析与使用 联系人侧边栏

一.SectionIndexer 是一个接口 有三个方法需要实现

       1>.  返回一个数组 可以通过拿到adapter对象拿到这个数组

  Object[] getSections();

 2> 通过上面数组第几个 来获取ListView的位置

  int getPositionForSection(int sectionIndex);
      3>通过listView的位置 来获取数组的位置
  int getSectionForPosition(int position);

二. 一个联系人排序的Demo

  先看效果图


这里只贴主要代码 源码给下载地址


1>.首先定义一个控件  也就是右侧的导航 字母列表

package com.Sidebar.intentseconddemo.sidebardemo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HeaderViewListAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.Sidebar.intentseconddemo.sidebardemo.R;
import com.Sidebar.intentseconddemo.sidebardemo.utils.DensityUtil;


public class Sidebar extends View {
	private Paint paint;
	private TextView header;
	private float height;
	private ListView mListView;
	private Context context;

	private SectionIndexer sectionIndexter = null;

	public void setListView(ListView listView){
		mListView = listView;
	}


	public Sidebar(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	private String[] sections;

	private void init(){
	    String st = context.getString(R.string.search_new);
        sections= new String[]{st,"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","#"};
		paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(Color.parseColor("#8C8C8C"));
		paint.setTextAlign(Align.CENTER);
		paint.setTextSize(DensityUtil.sp2px(context, 10));
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		float center = getWidth() / 2;
		height = getHeight() / sections.length;
		for (int i = sections.length - 1; i > -1; i--) {
			canvas.drawText(sections[i], center, height * (i+1), paint);
		}
	}

	private int sectionForPoint(float y) {
		int index = (int) (y / height);
		if(index < 0) {
			index = 0;
		}
		if(index > sections.length - 1){
			index = sections.length - 1;
		}
		return index;
	}

	private void setHeaderTextAndscroll(MotionEvent event){
		 if (mListView == null) {
		        //check the mListView to avoid NPE. but the mListView shouldn't be null
		        //need to check the call stack later
		        return;
		    }
		String headerString = sections[sectionForPoint(event.getY())];
		header.setText(headerString);
		ListAdapter adapter = mListView.getAdapter();
		if(sectionIndexter == null){
			/*判断adapter是否支持 sectionIndexter    --  要实现SectionIndexer 接口*/
    		if(adapter instanceof HeaderViewListAdapter){ 
    		    sectionIndexter = (SectionIndexer) ((HeaderViewListAdapter) adapter).getWrappedAdapter();
    		}else if(adapter instanceof SectionIndexer){
    		    sectionIndexter = (SectionIndexer)adapter;
    		}else{
    		    throw new RuntimeException("listview sets adpater does not implement SectionIndexer interface");
    		}
		}
		String[] adapterSections = (String[]) sectionIndexter.getSections();
		try {

			for (int i = adapterSections.length - 1; i > -1; i--) {
				if(adapterSections[i].equals(headerString)){
					mListView.setSelection(sectionIndexter.getPositionForSection(i));

					break;
				}
			}
		} catch (Exception e) {
			Log.e("setHeaderTextAndscroll", e.getMessage());
		}

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:{
			if(header == null){
				header = (TextView) ((View)getParent()).findViewById(R.id.floating_header);
			}
			setHeaderTextAndscroll(event);
			header.setVisibility(View.VISIBLE);
			setBackgroundResource(R.drawable.ease_sidebar_background_pressed);
			return true;
		}
		case MotionEvent.ACTION_MOVE:{
	
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值