StickyListHeaders的用法说明(带字母索引条)

2023-02-10 18:57:25 浏览数 (1)

有些同学可能没用过这个SckyListHeadersListView 先发个效果图,图片是从别的地方拷贝过来的

以下的几个类 直接拷贝到项目中 备用。

代码语言:javascript复制
import java.lang.reflect.Field;
import java.util.ArrayList;


import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SectionIndexer;

/**
 * @author Emil Sjölander
 */
public class StickyListHeadersListView extends ListView {
	
	public interface OnHeaderClickListener {
		public void onHeaderClick(StickyListHeadersListView l, View header,
				int itemPosition, long headerId, boolean currentlySticky);
	}

	public OnScrollListener mOnScrollListenerDelegate;
	private boolean mAreHeadersSticky = true; //header是否停留
	private int mHeaderBottomPosition;
	private View mHeader;
	private int mDividerHeight;
	private Drawable mDivider;
	private Boolean mClippingToPadding;
	private final Rect mClippingRect = new Rect();
	private Long mCurrentHeaderId = null;
	public AdapterWrapper mAdapter;
	private float mHeaderDownY = -1;
	private boolean mHeaderBeingPressed = false;
	private OnHeaderClickListener mOnHeaderClickListener;
	private Integer mHeaderPosition;
	private ViewConfiguration mViewConfig;
	private ArrayList<View> mFooterViews;
	private boolean mDrawingListUnderStickyHeader = false;
	private Rect mSelectorRect = new Rect();// for if reflection fails
	private Field mSelectorPositionField;
	
	private AdapterWrapper.OnHeaderClickListener mAdapterHeaderClickListener = new AdapterWrapper.OnHeaderClickListener() {

		@Override
		public void onHeaderClick(View header, int itemPosition, long headerId) {
			if (mOnHeaderClickListener != null) {
				mOnHeaderClickListener.onHeaderClick(
						StickyListHeadersListView.this, header, itemPosition,
						headerId, false);
			}
		}
	};

	private DataSetObserver mDataSetChangedObserver = new DataSetObserver() {
		@Override
		public void onChanged() {
			reset();
		}

		@Override
		public void onInvalidated() {
			reset();
		}
	};

	/**
	 *
	 */
	public interface OnLoadingMoreLinstener {
		/**
		 */
		void OnLoadingMore();
	}
	
	public OnLoadingMoreLinstener loadMoreListener;
	
	public void setLoadingMoreListener(OnLoadingMoreLinstener listener) {
		this.loadMoreListener = listener;
	}
	
	public OnScrollListener mOnScrollListener = new OnScrollListener() {

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			Log.e("parent","onScrollStateChanged");
			if (mOnScrollListenerDelegate != null) {
				mOnScrollListenerDelegate.onScrollStateChanged(view,
						scrollState);
			}
			
			if (scrollState == OnScrollListener.SCROLL_STATE_FLING) {
				
			} else if (scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL
					|| scrollState == OnScrollListener.SCROLL_STATE_IDLE) {

				
				if (getLastVisiblePosition() == (getCount() - 1)) {
					if(loadMoreListener != null) {
						loadMoreListener.OnLoadingMore();
					}
				}
			}
		
		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			Log.e("parent","onScroll");
			if (mOnScrollListenerDelegate != null) {
				mOnScrollListenerDelegate.onScroll(view, firstVisibleItem,
						visibleItemCount, totalItemCount);
			}
			
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
				scrollChanged(firstVisibleItem);
			}
		}
	};

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

	public StickyListHeadersListView(Context context, AttributeSet attrs) {
		this(context, attrs, android.R.attr.listViewStyle);
	}

	public StickyListHeadersListView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);

		super.setOnScrollListener(mOnScrollListener);
		// null out divider, dividers are handled by adapter so they look good
		// with headers
		super.setDivider(null);
		super.setDividerHeight(0);
		mViewConfig = ViewConfiguration.get(context);
		if (mClippingToPadding == null) {
			mClippingToPadding = true;
		}

		try {
			Field selectorRectField = AbsListView.class
					.getDeclaredField("mSelectorRect");
			selectorRectField.setAccessible(true);
			mSelectorRect = (Rect) selectorRectField.get(this);

			mSelectorPositionField = AbsListView.class
					.getDeclaredField("mSelectorPosition");
			mSelectorPositionField.setAccessible(true);
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		if (changed) {
			reset();
			scrollChanged(getFirstVisiblePosition());
		}
	}

	private void reset() {
		mHeader = null;
		mCurrentHeaderId = null;
		mHeaderPosition = null;
		mHeaderBottomPosition = -1;
	}

	@Override
	public boolean performItemClick(View view, int position, long id) {
		if (view instanceof WrapperView) {
			view = ((WrapperView) view).mItem;
		}
		return super.performItemClick(view, position, id);
	}

	@Override
	public void setDivider(Drawable divider) {
		this.mDivider = divider;
		if (divider != null) {
			int dividerDrawableHeight = divider.getIntrinsicHeight();
			if (dividerDrawableHeight >= 0) {
				setDividerHeight(dividerDrawableHeight);
			}
		}
		if (mAdapter != null) {
			mAdapter.setDivider(divider);
			requestLayout();
			invalidate();
		}
	}

	@Override
	public void setDividerHeight(int height) {
		mDividerHeight = height;
		if (mAdapter != null) {
			mAdapter.setDividerHeight(height);
			requestLayout();
			invalidate();
		}
	}

	@Override
	public void setOnScrollListener(OnScrollListener l) {
		mOnScrollListenerDelegate = l;
	}

	public void setAreHeadersSticky(boolean areHeadersSticky) {
		if (this.mAreHeadersSticky != areHeadersSticky) {
			this.mAreHeadersSticky = areHeadersSticky;
			requestLayout();
		}
	}

	public boolean getAreHeadersSticky() {
		return mAreHeadersSticky;
	}

	@Override
	public void setAdapter(ListAdapter adapter) {
		if (this.isInEditMode()) {
			super.setAdapter(adapter);
			return;
		}
		if (adapter == null) {
			mAdapter = null;
			reset();
			super.setAdapter(null);
			return;
		}
		if (!(adapter instanceof StickyListHeadersAdapter)) {
			throw new IllegalArgumentException(
					"Adapter must implement StickyListHeadersAdapter");
		}
		mAdapter = wrapAdapter(adapter);
		reset();
		super.setAdapter(this.mAdapter);
	}

	private AdapterWrapper wrapAdapter(ListAdapter adapter) {
		AdapterWrapper wrapper;
		if (adapter instanceof SectionIndexer) {
			wrapper = new SectionIndexerAdapterWrapper(getContext(),
					(StickyListHeadersAdapter) adapter);
		} else {
			wrapper = new AdapterWrapper(getContext(),
					(StickyListHeadersAdapter) adapter);
		}
		wrapper.setDivider(mDivider);
		wrapper.setDividerHeight(mDividerHeight);
		wrapper.registerDataSetObserver(mDataSetChangedObserver);
		wrapper.setOnHeaderClickListener(mAdapterHeaderClickListener);
		return wrapper;
	}

	public StickyListHeadersAdapter getWrappedAdapter() {
		return mAdapter == null ? null : mAdapter.mDelegate;
	}

	public View getWrappedView(int position) {
		View view = getChildAt(position);
		if ((view instanceof WrapperView))
			return ((WrapperView) view).mItem;
		return view;
	}

	@Override
	protected void dispatchDraw(Canvas canvas) {
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) {
			scrollChanged(getFirstVisiblePosition());
		}
		positionSelectorRect();
		if (!mAreHeadersSticky || mHeader == null) {
			super.dispatchDraw(canvas);
			return;
		}

		if (!mDrawingListUnderStickyHeader) {
			mClippingRect
					.set(0, mHeaderBottomPosition, getWidth(), getHeight());
			canvas.save();
			canvas.clipRect(mClippingRect);
		}

		super.dispatchDraw(canvas);

		if (!mDrawingListUnderStickyHeader) {
			canvas.restore();
		}

		drawStickyHeader(canvas);
	}

	private void positionSelectorRect() {
		if (!mSelectorRect.isEmpty()) {
			int selectorPosition = getSelectorPosition();
			if (selectorPosition >= 0) {
				int firstVisibleItem = fixedFirstVisibleItem(getFirstVisiblePosition());
				View v = getChildAt(selectorPosition - firstVisibleItem);
				if (v instanceof WrapperView) {
					WrapperView wrapper = ((WrapperView) v);
					mSelectorRect.top = wrapper.getTop()   wrapper.mItemTop;
				}
			}
		}
	}

	private int getSelectorPosition() {
		if (mSelectorPositionField == null) { // not all supported andorid
												// version have this variable
			for (int i = 0; i < getChildCount(); i  ) {
				if (getChildAt(i).getBottom() == mSelectorRect.bottom) {
					return i   fixedFirstVisibleItem(getFirstVisiblePosition());
				}
			}
		} else {
			try {
				return mSelectorPositionField.getInt(this);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return -1;
	}

	private void drawStickyHeader(Canvas canvas) {
		int headerHeight = getHeaderHeight();
		int top = mHeaderBottomPosition - headerHeight;
		// clip the headers drawing region
		mClippingRect.left = getPaddingLeft();
		mClippingRect.right = getWidth() - getPaddingRight();
		mClippingRect.bottom = top   headerHeight;
		mClippingRect.top = mClippingToPadding ? getPaddingTop() : 0;

		canvas.save();
		canvas.clipRect(mClippingRect);
		canvas.translate(getPaddingLeft(), top);
		mHeader.draw(canvas);
		canvas.restore();
	}

	private void measureHeader() {
		
		int widthMeasureSpec = MeasureSpec.makeMeasureSpec(getWidth()
				- getPaddingLeft() - getPaddingRight()
				- (isScrollBarOverlay() ? 0 : getVerticalScrollbarWidth()), MeasureSpec.EXACTLY);
		int heightMeasureSpec = 0;

		ViewGroup.LayoutParams params = mHeader.getLayoutParams();
		if (params != null && params.height > 0) {
			heightMeasureSpec = MeasureSpec.makeMeasureSpec(params.height,
					MeasureSpec.EXACTLY);
		} else {
			heightMeasureSpec = MeasureSpec.makeMeasureSpec(0,
					MeasureSpec.UNSPECIFIED);
		}
		mHeader.measure(widthMeasureSpec, heightMeasureSpec);
		
		
		mHeader.layout(getPaddingLeft(), 0, getWidth()
				- getPaddingRight(), mHeader.getMeasuredHeight());
	}

	private boolean isScrollBarOverlay() {
		int scrollBarStyle = getScrollBarStyle();
		return scrollBarStyle == SCROLLBARS_INSIDE_OVERLAY || scrollBarStyle == SCROLLBARS_OUTSIDE_OVERLAY;
	}

	private int getHeaderHeight() {
		return mHeader == null ? 0 : mHeader.getMeasuredHeight();
	}

	@Override
	public void setClipToPadding(boolean clipToPadding) {
		super.setClipToPadding(clipToPadding);
		mClippingToPadding = clipToPadding;
	}

	private void scrollChanged(int reportedFirstVisibleItem) {

		Log.e("parent","scrollChanged");
		int adapterCount = mAdapter == null ? 0 : mAdapter.getCount();
		if (adapterCount == 0 || !mAreHeadersSticky) {
			return;
		}

		final int listViewHeaderCount = getHeaderViewsCount();
		final int firstVisibleItem = fixedFirstVisibleItem(reportedFirstVisibleItem)
				- listViewHeaderCount;

		if (firstVisibleItem < 0 || firstVisibleItem > adapterCount - 1) {
			reset();
			updateHeaderVisibilities();
			invalidate();
			return;
		}

		if (mHeaderPosition == null || mHeaderPosition != firstVisibleItem) {
			mHeaderPosition = firstVisibleItem;
			mCurrentHeaderId = mAdapter.getHeaderId(firstVisibleItem);
			mHeader = mAdapter.getHeaderView(mHeaderPosition, mHeader, this);
			measureHeader();
		}

		int childCount = getChildCount();
		if (childCount != 0) {
			View viewToWatch = null;
			int watchingChildDistance = Integer.MAX_VALUE;
			boolean viewToWatchIsFooter = false;

			for (int i = 0; i < childCount; i  ) {
				final View child = super.getChildAt(i);
				final boolean childIsFooter = mFooterViews != null
						&& mFooterViews.contains(child);

				final int childDistance = child.getTop()
						- (mClippingToPadding ? getPaddingTop() : 0);
				if (childDistance < 0) {
					continue;
				}

				if (viewToWatch == null
						|| (!viewToWatchIsFooter && !((WrapperView) viewToWatch)
								.hasHeader())
						|| ((childIsFooter || ((WrapperView) child).hasHeader()) && childDistance < watchingChildDistance)) {
					viewToWatch = child;
					viewToWatchIsFooter = childIsFooter;
					watchingChildDistance = childDistance;
				}
			}

			final int headerHeight = getHeaderHeight();
			if (viewToWatch != null
					&& (viewToWatchIsFooter || ((WrapperView) viewToWatch)
							.hasHeader())) {
				if (firstVisibleItem == listViewHeaderCount
						&& super.getChildAt(0).getTop() > 0
						&& !mClippingToPadding) {
					mHeaderBottomPosition = 0;
				} else {
					final int paddingTop = mClippingToPadding ? getPaddingTop()
							: 0;
					mHeaderBottomPosition = Math.min(viewToWatch.getTop(),
							headerHeight   paddingTop);
					mHeaderBottomPosition = mHeaderBottomPosition < paddingTop ? headerHeight
							  paddingTop
							: mHeaderBottomPosition;
				}
			} else {
				mHeaderBottomPosition = headerHeight
						  (mClippingToPadding ? getPaddingTop() : 0);
			}
		}
		updateHeaderVisibilities();
		invalidate();
	}

	@Override
	public void addFooterView(View v) {
		super.addFooterView(v);
		if (mFooterViews == null) {
			mFooterViews = new ArrayList<View>();
		}
		mFooterViews.add(v);
	}

	@Override
	public boolean removeFooterView(View v) {
		if (super.removeFooterView(v)) {
			mFooterViews.remove(v);
			return true;
		}
		return false;
	}

	private void updateHeaderVisibilities() {
		int top = mClippingToPadding ? getPaddingTop() : 0;
		int childCount = getChildCount();
		for (int i = 0; i < childCount; i  ) {
			View child = super.getChildAt(i);
			if (child instanceof WrapperView) {
				WrapperView wrapperViewChild = (WrapperView) child;
				if (wrapperViewChild.hasHeader()) {
					View childHeader = wrapperViewChild.mHeader;
					if (wrapperViewChild.getTop() < top) {
						childHeader.setVisibility(View.INVISIBLE);
					} else {
						childHeader.setVisibility(View.VISIBLE);
					}
				}
			}
		}
	}

	private int fixedFirstVisibleItem(int firstVisibleItem) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			return firstVisibleItem;
		}

		for (int i = 0; i < getChildCount(); i  ) {
			if (getChildAt(i).getBottom() >= 0) {
				firstVisibleItem  = i;
				break;
			}
		}

		// work around to fix bug with firstVisibleItem being to high because
		// listview does not take clipToPadding=false into account
		if (!mClippingToPadding && getPaddingTop() > 0) {
			if (super.getChildAt(0).getTop() > 0) {
				if (firstVisibleItem > 0) {
					firstVisibleItem -= 1;
				}
			}
		}
		return firstVisibleItem;
	}

	public void setOnHeaderClickListener(
			OnHeaderClickListener onHeaderClickListener) {
		this.mOnHeaderClickListener = onHeaderClickListener;
	}

	public void setDrawingListUnderStickyHeader(
			boolean drawingListUnderStickyHeader) {
		mDrawingListUnderStickyHeader = drawingListUnderStickyHeader;
	}

	public boolean isDrawingListUnderStickyHeader() {
		return mDrawingListUnderStickyHeader;
	}

//----------注释-------------
	// TODO handle touches better, multitouch etc.
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		int action = ev.getAction();
		if (action == MotionEvent.ACTION_DOWN
				&& ev.getY() <= mHeaderBottomPosition) {
			mHeaderDownY = ev.getY();
			mHeaderBeingPressed = true;
			mHeader.setPressed(true);
			mHeader.invalidate();
			invalidate(0, 0, getWidth(), mHeaderBottomPosition);
			return true;
		}
		if (mHeaderBeingPressed) {
			if (Math.abs(ev.getY() - mHeaderDownY) < mViewConfig
					.getScaledTouchSlop()) {
				if (action == MotionEvent.ACTION_UP
						|| action == MotionEvent.ACTION_CANCEL) {
					mHeaderDownY = -1;
					mHeaderBeingPressed = false;
					mHeader.setPressed(false);
					mHeader.invalidate();
					invalidate(0, 0, getWidth(), mHeaderBottomPosition);
					if (mOnHeaderClickListener != null) {
						mOnHeaderClickListener.onHeaderClick(this, mHeader,
								mHeaderPosition, mCurrentHeaderId, true);
					}
				}
				return true;
			} else {
				mHeaderDownY = -1;
				mHeaderBeingPressed = false;
				mHeader.setPressed(false);
				mHeader.invalidate();
				invalidate(0, 0, getWidth(), mHeaderBottomPosition);
			}
		}
		return super.onTouchEvent(ev);
	}

	
}
代码语言:javascript复制
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;

public interface StickyListHeadersAdapter extends ListAdapter {
	View getHeaderView(int position, View convertView, ViewGroup parent);
	long getHeaderId(int position);
}
代码语言:javascript复制
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

/**
 * 
 */
class WrapperView extends ViewGroup {

	View mItem;
	Drawable mDivider;
	int mDividerHeight;
	View mHeader;
	int mItemTop;

	public WrapperView(Context c) {
		super(c);
	}

	void update(View item, View header, Drawable divider, int dividerHeight) {
		
		if (item == null) {
			throw new NullPointerException("List view item must not be null.");
		}

		if (this.mItem != item) {
			removeView(this.mItem);
			this.mItem = item;
			final ViewParent parent = item.getParent();
			if(parent != null && parent != this) {
				if(parent instanceof ViewGroup) {
					((ViewGroup) parent).removeView(item);
				}
			}
			addView(item);
		}

		if (this.mHeader != header) {
			if (this.mHeader != null) {
				removeView(this.mHeader);
			}
			this.mHeader = header;
			if (header != null) {
				addView(header);
			}
		}

		if (this.mDivider != divider) {
			this.mDivider = divider;
			this.mDividerHeight = dividerHeight;
			invalidate();
		}
	}

	boolean hasHeader() {
		return mHeader != null;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
		int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(measuredWidth,
				MeasureSpec.EXACTLY);
		int measuredHeight = 0;
		
		//measure header or divider. when there is a header visible it acts as the divider
		if (mHeader != null) {
			LayoutParams params = mHeader.getLayoutParams();
			if (params != null && params.height > 0) {
				mHeader.measure(childWidthMeasureSpec,
						MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY));
			} else {
				mHeader.measure(childWidthMeasureSpec,
						MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
			}
			measuredHeight  = mHeader.getMeasuredHeight();
		} else if (mDivider != null) {
			measuredHeight  = mDividerHeight;
		}
		
		//measure item
		LayoutParams params = mItem.getLayoutParams();
		if (params != null && params.height > 0) {
			mItem.measure(childWidthMeasureSpec,
					MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY));
		} else {
			mItem.measure(childWidthMeasureSpec,
					MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
		}
		measuredHeight  = mItem.getMeasuredHeight();

		setMeasuredDimension(measuredWidth, measuredHeight);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {

		l = 0;
		t = 0;
		r = getWidth();
		b = getHeight();

		if (mHeader != null) {
			int headerHeight = mHeader.getMeasuredHeight();
			mHeader.layout(l, t, r, headerHeight);
			mItemTop = headerHeight;
			mItem.layout(l, headerHeight, r, b);
		} else if (mDivider != null) {
			mDivider.setBounds(l, t, r, mDividerHeight);
			mItemTop = mDividerHeight;
			mItem.layout(l, mDividerHeight, r, b);
		} else {
			mItemTop = t;
			mItem.layout(l, t, r, b);
		}
	}

	@Override
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		if (mHeader == null && mDivider != null) {
			// Drawable.setBounds() does not seem to work pre-honeycomb. So have
			// to do this instead
			if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
				canvas.clipRect(0, 0, getWidth(), mDividerHeight);
			}
			mDivider.draw(canvas);
		}
	}
}
代码语言:javascript复制
import android.content.Context;
import android.widget.SectionIndexer;

class SectionIndexerAdapterWrapper extends
		AdapterWrapper implements SectionIndexer {
	
	final SectionIndexer mSectionIndexerDelegate;

	SectionIndexerAdapterWrapper(Context context,
			StickyListHeadersAdapter delegate) {
		super(context, delegate);
		mSectionIndexerDelegate = (SectionIndexer) delegate;
	}

	@Override
	public int getPositionForSection(int section) {
		return mSectionIndexerDelegate.getPositionForSection(section);
	}

	@Override
	public int getSectionForPosition(int position) {
		return mSectionIndexerDelegate.getSectionForPosition(position);
	}

	@Override
	public Object[] getSections() {
		return mSectionIndexerDelegate.getSections();
	}

}
代码语言:javascript复制
import android.content.Context;
import android.widget.Checkable;

/**
 * A WrapperView that implements the checkable interface
 * 
 * @author Emil Sjölander
 */
class CheckableWrapperView extends WrapperView implements Checkable {

	public CheckableWrapperView(final Context context) {
		super(context);
	}

	@Override
	public boolean isChecked() {
		return ((Checkable) mItem).isChecked();
	}

	@Override
	public void setChecked(final boolean checked) {
		((Checkable) mItem).setChecked(checked);
	}

	@Override
	public void toggle() {
		setChecked(!isChecked());
	}
}
代码语言:javascript复制
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Checkable;
import android.widget.ListAdapter;

/**
 * A {@link ListAdapter} which wraps a {@link StickyListHeadersAdapter} and
 * automatically handles wrapping the result of
 * {@link StickyListHeadersAdapter#getView(int, View, ViewGroup)}
 * and
 * {@link StickyListHeadersAdapter#getHeaderView(int, View, ViewGroup)}
 * appropriately.
 *
 * @author Jake Wharton (jakewharton@gmail.com)
 */
public class AdapterWrapper extends BaseAdapter implements StickyListHeadersAdapter {


	public interface OnHeaderClickListener{
		public void onHeaderClick(View header, int itemPosition, long headerId);
	}

	final StickyListHeadersAdapter mDelegate;
	private final List<View> mHeaderCache = new LinkedList<View>();
	private final Context mContext;
	private Drawable mDivider;
	private int mDividerHeight;
	private OnHeaderClickListener mOnHeaderClickListener;
	private DataSetObserver mDataSetObserver = new DataSetObserver() {

		@Override
		public void onInvalidated() {
			mHeaderCache.clear();
			AdapterWrapper.super.notifyDataSetInvalidated();
		}
		
		@Override
		public void onChanged() {
			AdapterWrapper.super.notifyDataSetChanged();
		}
	};

	AdapterWrapper(Context context,
			StickyListHeadersAdapter delegate) {
		this.mContext = context;
		this.mDelegate = delegate;
		delegate.registerDataSetObserver(mDataSetObserver);
	}

	void setDivider(Drawable divider) {
		this.mDivider = divider;
	}

	void setDividerHeight(int dividerHeight) {
		this.mDividerHeight = dividerHeight;
	}

	@Override
	public boolean areAllItemsEnabled() {
		return mDelegate.areAllItemsEnabled();
	}

	@Override
	public boolean isEnabled(int position) {
		return mDelegate.isEnabled(position);
	}

	@Override
	public int getCount() {
		return mDelegate.getCount();
	}

	@Override
	public Object getItem(int position) {
		return mDelegate.getItem(position);
	}

	@Override
	public long getItemId(int position) {
		return mDelegate.getItemId(position);
	}

	@Override
	public boolean hasStableIds() {
		return mDelegate.hasStableIds();
	}

	@Override
	public int getItemViewType(int position) {
		return mDelegate.getItemViewType(position);
	}

	@Override
	public int getViewTypeCount() {
		return mDelegate.getViewTypeCount();
	}

	@Override
	public boolean isEmpty() {
		return mDelegate.isEmpty();
	}

	/**
	 * Will recycle header from {@link WrapperView} if it exists
	 */
	private void recycleHeaderIfExists(WrapperView wv) {
		View header = wv.mHeader;
		if (header != null) {
			mHeaderCache.add(header);
		}
	}

	/**
	 * Get a header view. This optionally pulls a header from the supplied
	 * {@link WrapperView} and will also recycle the divider if it exists.
	 */
	private View configureHeader(WrapperView wv, final int position) {
		View header = wv.mHeader == null ? popHeader() : wv.mHeader;
		header = mDelegate.getHeaderView(position, header, wv);
		if (header == null) {
			throw new NullPointerException("Header view must not be null.");
		}
		//if the header isn't clickable, the listselector will be drawn on top of the header
		header.setClickable(true);
		header.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(mOnHeaderClickListener != null){
					long headerId = mDelegate.getHeaderId(position);
					mOnHeaderClickListener.onHeaderClick(v, position, headerId);
				}
			}
		});
		return header;
	}

	private View popHeader() {
		if(mHeaderCache.size() > 0) {
			return mHeaderCache.remove(0);
		}
		return null;
	}

	
	private boolean previousPositionHasSameHeader(int position) {
		return position != 0
				&& mDelegate.getHeaderId(position) == mDelegate
						.getHeaderId(position - 1);
	}

	@Override
	public WrapperView getView(int position, View convertView, ViewGroup parent) {
		WrapperView wv = (convertView == null) ? new WrapperView(mContext) : (WrapperView) convertView;
		View item = mDelegate.getView(position, wv.mItem, wv);
		View header = null;
		if (previousPositionHasSameHeader(position)) {
			recycleHeaderIfExists(wv);
		} else {
			header = configureHeader(wv, position);
		}
		if((item instanceof Checkable) && !(wv instanceof CheckableWrapperView)) {
			// Need to create Checkable subclass of WrapperView for ListView to work correctly
			wv = new CheckableWrapperView(mContext);
		} else if(!(item instanceof Checkable) && (wv instanceof CheckableWrapperView)) {
			wv = new WrapperView(mContext);
		}
		wv.update(item, header, mDivider, mDividerHeight);
		return wv;
	}

	public void setOnHeaderClickListener(OnHeaderClickListener onHeaderClickListener){
		this.mOnHeaderClickListener = onHeaderClickListener;
	}

	@Override
	public boolean equals(Object o) {
		return mDelegate.equals(o); 
	}

	@Override
	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		return ((BaseAdapter) mDelegate).getDropDownView(position, convertView, parent);
	}

	@Override
	public int hashCode() {
		return mDelegate.hashCode();
	}

	@Override
	public void notifyDataSetChanged() {
		((BaseAdapter) mDelegate).notifyDataSetChanged();
	}

	@Override
	public void notifyDataSetInvalidated() {
		((BaseAdapter) mDelegate).notifyDataSetInvalidated();
	}

	@Override
	public String toString() {
		return mDelegate.toString();
	}

	@Override
	public View getHeaderView(int position, View convertView, ViewGroup parent) {
		return mDelegate.getHeaderView(position, convertView, parent);
	}
	@Override
	public long getHeaderId(int position) {
		return mDelegate.getHeaderId(position);
	}

}

上面的几个类大家直接建一个目录拷贝进去就行 下面开始写项目的代码

Activity

代码语言:javascript复制
public class CitySelectListActivity extends Activity implements
        StickyListHeadersListView.OnHeaderClickListener, AdapterView.OnItemClickListener
        , StickyListHeadersListView.OnLoadingMoreLinstener {

    CityLetterSortAdapter mAdapter;
    StickyListHeadersListView stickyLV;
    private static final String TAG = CitySelectListActivity.class.getSimpleName();

    private CharacterParser mCharacterParser;
    private List<SortModel> sourceDateFilterList = new ArrayList<SortModel>();
    private EditText searchEt;
    private List<SortModel> sourceDateList;

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

    public void initView() {
        mCharacterParser = CharacterParser.getInstance();
        PinyinComparator pinyinComparator = new PinyinComparator();
        sourceDateList = filledData(getResources().getStringArray(R.array.cities_data));
        Collections.sort(sourceDateList, pinyinComparator);

        mAdapter = new CityLetterSortAdapter(this, sourceDateList);
        stickyLV = (StickyListHeadersListView) this.findViewById(R.id.stickyList);
        stickyLV.setAdapter(mAdapter);
        stickyLV.setOnItemClickListener(this);
        stickyLV.setOnHeaderClickListener(this);
        stickyLV.setLoadingMoreListener(this);
        LetterSideBar letterSideBar = (LetterSideBar) findViewById(R.id.cs_letter_sb);
        letterSideBar.setOnTouchingLetterChangedListener(
                new LetterSideBar.OnTouchingLetterChangedListener() {
                    @Override
                    public void onTouchingLetterChanged(String letter) {
                        Logger.d(TAG, "onTouchingLetterChanged  letter: "   letter);
                        int jumpPos = mAdapter.getPositionForSection(letter.charAt(0));
                        stickyLV.setSelection(jumpPos);
                    }
                });
        letterSideBar.setTextView((TextView) findViewById(R.id.cs_selected_letter_tv));

        searchEt = (EditText) findViewById(R.id.cs_search_et);
        searchEt.setVisibility(View.VISIBLE);
        searchEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.toString().length() == 0) {
                    mAdapter.updateListView(sourceDateList);
                    mAdapter.notifyDataSetChanged();
                } else {
                    String searchText = searchEt.getText().toString();
                    if (TextUtils.isEmpty(searchText)) {
                        SingleToast.show(getApplicationContext(), R.string.msg_keyword_is_empty);
                    }
                    sourceDateFilterList.clear();
                    for (int h = 0; h < sourceDateList.size(); h  ) {
                        if (sourceDateList.get(h).getName().contains(searchText)) {
                            sourceDateFilterList.add(sourceDateList.get(h));
                        }
                    }

                    if (sourceDateFilterList.size() <= 0) {
                        //                    noFilterPhoneFriends();
                    } else {
                        mAdapter.updateListView(sourceDateFilterList);
                        mAdapter.notifyDataSetChanged();
                    }

                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    private List<SortModel> filledData(String[] date) {
        List<SortModel> mSortList = new ArrayList<SortModel>();

        for (int i = 0, n = date.length; i < n; i  ) {
            SortModel sortModel = new SortModel();
            sortModel.setName(date[i]);
            String pinyin = mCharacterParser.getSelling(date[i]);
            sortModel.setPinyin(pinyin);
            String sortString = pinyin.substring(0, 1).toUpperCase();
            if (sortString.matches("[A-Z]")) {
                sortModel.setSortLetter(sortString.toUpperCase());
            } else {
                sortModel.setSortLetter("#");
            }

            mSortList.add(sortModel);
        }
        return mSortList;
    }

    @Override
    public void OnLoadingMore() {
    }

    @Override
    public void onHeaderClick(StickyListHeadersListView l, View header,
            int itemPosition, long headerId, boolean currentlySticky) {
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        String city = ((SortModel) mAdapter.getItem(position)).getName();
        setResult(RESULT_OK, getIntent().putExtra(Constants.EXTRA_KEY_SELECT_CITY, city));
        finish();
    }
}

Adapter

代码语言:javascript复制
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.ccvideo.R;
import com.yizhibo.video.bean.SortModel;
import com.yizhibo.video.utils.Utils;
import com.yizhibo.video.view.stickylistview.StickyListHeadersAdapter;

public class CityLetterSortAdapter extends BaseAdapter
        implements StickyListHeadersAdapter, SectionIndexer, Filterable {
    private static final String TAG = CityLetterSortAdapter.class.getSimpleName();
    private List<SortModel> mListAll = new ArrayList<SortModel>();
    private List<SortModel> mList = null;
    private Context mContext;

    public CityLetterSortAdapter(Context mContext, List<SortModel> list) {
        this.mContext = mContext;
        this.mList = list;
        mListAll.clear();
        this.mListAll.addAll(list);
    }

    public void updateListView(List<SortModel> list) {
        this.mList = list;
        this.mListAll = list;
        notifyDataSetChanged();
    }

    public int getCount() {
        return this.mList.size();
    }

    public Object getItem(int position) {
        return mList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_city_letter_sort, viewGroup, false);
            viewHolder.tvTitle = (TextView) view.findViewById(R.id.title);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        if (position >= mList.size()) {
            Utils.statisticError(TAG, "City list adapter size error !");
            return view;
        }
        viewHolder.tvTitle.setText(this.mList.get(position).getName());
        return view;
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                FilterResults filterResults = new FilterResults();
                ArrayList<SortModel> filterArrayName = new ArrayList<SortModel>();

                String search = charSequence.toString().toLowerCase();
                for (int i = 0, n = mListAll.size(); i < n; i  ) {
                    String name = mListAll.get(i).getPinyin();
                    if (name.toLowerCase().startsWith(search)) {
                        filterArrayName.add(mListAll.get(i));
                    }
                }
                filterResults.count = filterArrayName.size();
                filterResults.values = filterArrayName;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                mList.clear();
                mList.addAll((List<SortModel>) filterResults.values);
                notifyDataSetChanged();
            }
        };
    }

    @Override
    public View getHeaderView(int position, View view, ViewGroup viewGroup) {
        HeaderViewHolder hViewHolder;
        if (view == null) {
            hViewHolder = new HeaderViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_sticky_header, viewGroup, false);
            hViewHolder.tvLetter = (TextView) view.findViewById(R.id.sticky_header_letter_tv);
            view.setTag(hViewHolder);
        } else {
            hViewHolder = (HeaderViewHolder) view.getTag();
        }
        hViewHolder.tvLetter.setText(mList.get(position).getSortLetter());
        return view;
    }

    @Override
    public long getHeaderId(int position) {
        return mList.get(position).getSortLetter().subSequence(0, 1).charAt(0);
    }

    final static class HeaderViewHolder {
        TextView tvLetter;
    }
    final static class ViewHolder {
        TextView tvTitle;
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */
    public int getSectionForPosition(int position) {
        return mList.get(position).getSortLetter().charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i  ) {
            String sortStr = mList.get(i).getSortLetter();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 提取英文的首字母,非英文字母用#代替。
     */
    private String getAlpha(String letter) {
        String sortStr = letter.trim().substring(0, 1).toUpperCase();
        // 正则表达式,判断首字母是否是英文字母
        if (sortStr.matches("[A-Z]")) {
            return sortStr;
        } else {
            return "#";
        }
    }

    @Override
    public Object[] getSections() {
        return null;
    }
}

Layout

activity_city_select.xml

代码语言:javascript复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:fitsSystemWindows="true"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    <EditText
        android:id="@ id/cs_search_et"
        android:layout_width="match_parent"
        android:layout_height="34dp"
        android:layout_margin="10dp"
        android:paddingLeft="5dp"
        android:singleLine="true"
        android:imeOptions="actionSearch"
        android:layout_gravity="center_vertical"
        android:hint="@string/city_select"
        android:visibility="gone"
        android:drawableLeft="@drawable/icon_search"
        android:textSize="12sp"
        android:background="@drawable/search_box_shape"/>
    <com.yizhibo.video.view.stickylistview.StickyListHeadersListView
        android:id="@ id/stickyList"
        android:layout_width="match_parent"
        android:layout_below="@id/cs_search_et"
        android:layout_height="wrap_content"
        android:cacheColorHint="@android:color/transparent"
        android:listSelector="@drawable/list_item_selector"/>
    <com.yizhibo.video.view.LetterSideBar
        android:id="@ id/cs_letter_sb"
        android:layout_width="20dp"
        android:layout_height="match_parent"
        android:layout_below="@id/cs_search_et"
        android:layout_alignParentRight="true"/>

    <TextView
        android:id="@ id/cs_selected_letter_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="60sp"
        android:textColor="@color/text_gray"/>
</RelativeLayout>

item_city_letter_sort.xml

代码语言:javascript复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/list_item_video_limit_height"
    android:paddingLeft="9dp"
    android:paddingRight="15dp"
    android:gravity="center_vertical"
    android:background="@color/white"
    android:orientation="horizontal">
    <TextView
        android:id="@ id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="12dip"
        android:gravity="center_vertical"
        android:paddingBottom="10dip"
        android:paddingTop="10dip"
        android:layout_weight="1"
        android:text=""
        android:textSize="@dimen/text_size_13"
        android:textColor="@color/text_subtitle"/>
    <TextView
        android:id="@ id/name_code_txv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|right"
        android:gravity="right"
        android:layout_weight="1"
        android:paddingRight="10dip"
        android:paddingBottom="10dip"
        android:paddingTop="10dip"
        android:text=""
        android:textSize="@dimen/text_size_13"
        android:textColor="@color/text_subtitle"/>

</LinearLayout>

LetterSideBar

package com.yizhibo.video.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Typeface; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.TextView; import com.ccvideo.*; public class LetterSideBar extends View {     private OnTouchingLetterChangedListener onTouchingLetterChangedListener;     public static String[] mLetters = { "#", "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 int mChooseIndex = -1;     private Paint mPaint = new Paint();     private TextView mTextDialog;     public LetterSideBar(Context context, AttributeSet attrs, int defStyle) {         super(context, attrs, defStyle);     }     public LetterSideBar(Context context, AttributeSet attrs) {         super(context, attrs);     }     public LetterSideBar(Context context) {         super(context);     }     protected void onDraw(Canvas canvas) {         super.onDraw(canvas);         int height = getHeight();         int width = getWidth();         int singleHeight = height / mLetters.length;         for (int i = 0; i < mLetters.length; i ) {             mPaint.setColor(Color.rgb(33, 65, 98));             // mPaint.setColor(Color.WHITE);             mPaint.setTypeface(Typeface.DEFAULT);             mPaint.setAntiAlias(true);             mPaint.setTextSize(30);             // Selected state             if (i == mChooseIndex) {                 mPaint.setColor(getResources().getColor(R.color.text_common));                 mPaint.setFakeBoldText(true);             }             float x = width / 2 - mPaint.measureText(mLetters[i]) / 2;             float y = singleHeight * i singleHeight;             canvas.drawText(mLetters[i], x, y, mPaint);             mPaint.reset();         }     }     @Override     public boolean dispatchTouchEvent(MotionEvent event) {         final int action = event.getAction();         final float y = event.getY();         final int oldChooseIndex = mChooseIndex;         final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;         // 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.         final int chooseIndex = (int) (y / getHeight() * mLetters.length);         switch (action) {             case MotionEvent.ACTION_UP:                 setBackgroundResource(android.R.color.transparent);                 mChooseIndex = -1;//                 invalidate();                 if (mTextDialog != null) {                     mTextDialog.setVisibility(View.INVISIBLE);                 }                 break;             default:                 setBackgroundResource(R.color.black_alpha_percent_30);                 if (oldChooseIndex != chooseIndex) {                     if (chooseIndex >= 0 && chooseIndex < mLetters.length) {                         if (listener != null) {                             listener.onTouchingLetterChanged(mLetters[chooseIndex]);                         }                         if (mTextDialog != null) {                             mTextDialog.setText(mLetters[chooseIndex]);                             mTextDialog.setVisibility(View.VISIBLE);                         }                         mChooseIndex = chooseIndex;                         invalidate();                     }                 }                 break;         }         return true;     }     public void setTextView(TextView mTextDialog) {         this.mTextDialog = mTextDialog;     }     public void setOnTouchingLetterChangedListener(             OnTouchingLetterChangedListener onTouchingLetterChangedListener) {         this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;     }     public interface OnTouchingLetterChangedListener {         void onTouchingLetterChanged(String letter);     } }

item_sticky_header.xml

<?xml version="1.0" encoding="utf-8"?> <TextView     xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@ id/sticky_header_letter_tv"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="@color/default_line_color"     android:gravity="center_vertical|left"     android:padding="9dip"     android:text=""     android:textColor="@color/text_brown"     android:textSize="@dimen/text_size_12"/>

SortModel.java

public class SortModel extends BaseSortModel {     private String name;     public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     } }

CharacterParser.java

package com.yizhibo.video.utils; /**  * Java汉字转换为拼音  */ public class CharacterParser {     private static int[] pyvalue = new int[]{-20319, -20317, -20304, -20295, -20292, -20283, -20265, -20257, -20242, -20230, -20051, -20036, -20032,             -20026, -20002, -19990, -19986, -19982, -19976, -19805, -19784, -19775, -19774, -19763, -19756, -19751, -19746, -19741, -19739, -19728,             -19725, -19715, -19540, -19531, -19525, -19515, -19500, -19484, -19479, -19467, -19289, -19288, -19281, -19275, -19270, -19263, -19261,             -19249, -19243, -19242, -19238, -19235, -19227, -19224, -19218, -19212, -19038, -19023, -19018, -19006, -19003, -18996, -18977, -18961,             -18952, -18783, -18774, -18773, -18763, -18756, -18741, -18735, -18731, -18722, -18710, -18697, -18696, -18526, -18518, -18501, -18490,             -18478, -18463, -18448, -18447, -18446, -18239, -18237, -18231, -18220, -18211, -18201, -18184, -18183, -18181, -18012, -17997, -17988,             -17970, -17964, -17961, -17950, -17947, -17931, -17928, -17922, -17759, -17752, -17733, -17730, -17721, -17703, -17701, -17697, -17692,             -17683, -17676, -17496, -17487, -17482, -17468, -17454, -17433, -17427, -17417, -17202, -17185, -16983, -16970, -16942, -16915, -16733,             -16708, -16706, -16689, -16664, -16657, -16647, -16474, -16470, -16465, -16459, -16452, -16448, -16433, -16429, -16427, -16423, -16419,             -16412, -16407, -16403, -16401, -16393, -16220, -16216, -16212, -16205, -16202, -16187, -16180, -16171, -16169, -16158, -16155, -15959,             -15958, -15944, -15933, -15920, -15915, -15903, -15889, -15878, -15707, -15701, -15681, -15667, -15661, -15659, -15652, -15640, -15631,             -15625, -15454, -15448, -15436, -15435, -15419, -15416, -15408, -15394, -15385, -15377, -15375, -15369, -15363, -15362, -15183, -15180,             -15165, -15158, -15153, -15150, -15149, -15144, -15143, -15141, -15140, -15139, -15128, -15121, -15119, -15117, -15110, -15109, -14941,             -14937, -14933, -14930, -14929, -14928, -14926, -14922, -14921, -14914, -14908, -14902, -14894, -14889, -14882, -14873, -14871, -14857,             -14678, -14674, -14670, -14668, -14663, -14654, -14645, -14630, -14594, -14429, -14407, -14399, -14384, -14379, -14368, -14355, -14353,             -14345, -14170, -14159, -14151, -14149, -14145, -14140, -14137, -14135, -14125, -14123, -14122, -14112, -14109, -14099, -14097, -14094,             -14092, -14090, -14087, -14083, -13917, -13914, -13910, -13907, -13906, -13905, -13896, -13894, -13878, -13870, -13859, -13847, -13831,             -13658, -13611, -13601, -13406, -13404, -13400, -13398, -13395, -13391, -13387, -13383, -13367, -13359, -13356, -13343, -13340, -13329,             -13326, -13318, -13147, -13138, -13120, -13107, -13096, -13095, -13091, -13076, -13068, -13063, -13060, -12888, -12875, -12871, -12860,             -12858, -12852, -12849, -12838, -12831, -12829, -12812, -12802, -12607, -12597, -12594, -12585, -12556, -12359, -12346, -12320, -12300,             -12120, -12099, -12089, -12074, -12067, -12058, -12039, -11867, -11861, -11847, -11831, -11798, -11781, -11604, -11589, -11536, -11358,             -11340, -11339, -11324, -11303, -11097, -11077, -11067, -11055, -11052, -11045, -11041, -11038, -11024, -11020, -11019, -11018, -11014,             -10838, -10832, -10815, -10800, -10790, -10780, -10764, -10587, -10544, -10533, -10519, -10331, -10329, -10328, -10322, -10315, -10309,             -10307, -10296, -10281, -10274, -10270, -10262, -10260, -10256, -10254};     public static String[] pystr = new String[]{"a", "ai", "an", "ang", "ao", "ba", "bai", "ban", "bang", "bao", "bei", "ben", "beng", "bi", "bian",             "biao", "bie", "bin", "bing", "bo", "bu", "ca", "cai", "can", "cang", "cao", "ce", "ceng", "cha", "chai", "chan", "chang", "chao", "che",             "chen", "cheng", "chi", "chong", "chou", "chu", "chuai", "chuan", "chuang", "chui", "chun", "chuo", "ci", "cong", "cou", "cu", "cuan",             "cui", "cun", "cuo", "da", "dai", "dan", "dang", "dao", "de", "deng", "di", "dian", "diao", "die", "ding", "diu", "dong", "dou", "du",             "duan", "dui", "dun", "duo", "e", "en", "er", "fa", "fan", "fang", "fei", "fen", "feng", "fo", "fou", "fu", "ga", "gai", "gan", "gang",             "gao", "ge", "gei", "gen", "geng", "gong", "gou", "gu", "gua", "guai", "guan", "guang", "gui", "gun", "guo", "ha", "hai", "han", "hang",             "hao", "he", "hei", "hen", "heng", "hong", "hou", "hu", "hua", "huai", "huan", "huang", "hui", "hun", "huo", "ji", "jia", "jian",             "jiang", "jiao", "jie", "jin", "jing", "jiong", "jiu", "ju", "juan", "jue", "jun", "ka", "kai", "kan", "kang", "kao", "ke", "ken",             "keng", "kong", "kou", "ku", "kua", "kuai", "kuan", "kuang", "kui", "kun", "kuo", "la", "lai", "lan", "lang", "lao", "le", "lei", "leng",             "li", "lia", "lian", "liang", "liao", "lie", "lin", "ling", "liu", "long", "lou", "lu", "lv", "luan", "lue", "lun", "luo", "ma", "mai",             "man", "mang", "mao", "me", "mei", "men", "meng", "mi", "mian", "miao", "mie", "min", "ming", "miu", "mo", "mou", "mu", "na", "nai",             "nan", "nang", "nao", "ne", "nei", "nen", "neng", "ni", "nian", "niang", "niao", "nie", "nin", "ning", "niu", "nong", "nu", "nv", "nuan",             "nue", "nuo", "o", "ou", "pa", "pai", "pan", "pang", "pao", "pei", "pen", "peng", "pi", "pian", "piao", "pie", "pin", "ping", "po", "pu",             "qi", "qia", "qian", "qiang", "qiao", "qie", "qin", "qing", "qiong", "qiu", "qu", "quan", "que", "qun", "ran", "rang", "rao", "re",             "ren", "reng", "ri", "rong", "rou", "ru", "ruan", "rui", "run", "ruo", "sa", "sai", "san", "sang", "sao", "se", "sen", "seng", "sha",             "shai", "shan", "shang", "shao", "she", "shen", "sheng", "shi", "shou", "shu", "shua", "shuai", "shuan", "shuang", "shui", "shun",             "shuo", "si", "song", "sou", "su", "suan", "sui", "sun", "suo", "ta", "tai", "tan", "tang", "tao", "te", "teng", "ti", "tian", "tiao",             "tie", "ting", "tong", "tou", "tu", "tuan", "tui", "tun", "tuo", "wa", "wai", "wan", "wang", "wei", "wen", "weng", "wo", "wu", "xi",             "xia", "xian", "xiang", "xiao", "xie", "xin", "xing", "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", "yan", "yang", "yao", "ye", "yi",             "yin", "ying", "yo", "yong", "you", "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", "zao", "ze", "zei", "zen", "zeng", "zha",             "zhai", "zhan", "zhang", "zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou", "zhu", "zhua", "zhuai", "zhuan", "zhuang", "zhui",             "zhun", "zhuo", "zi", "zong", "zou", "zu", "zuan", "zui", "zun", "zuo"};     private StringBuilder buffer;     private String resource;     private static CharacterParser characterParser = new CharacterParser();     public static CharacterParser getInstance() {         return characterParser;     }     public String getResource() {         return resource;     }     public void setResource(String resource) {         this.resource = resource;     }     /**      * 汉字转成ASCII码 * * @param chs * @return      */     private int getChsAscii(String chs) {         int asc = 0;         try {             byte[] bytes = chs.getBytes("gb2312");             if (bytes.length > 2 || bytes.length <= 0) {                 throw new RuntimeException("illegal resource string");             }             if (bytes.length == 1) {                 asc = bytes[0];             }             if (bytes.length == 2) {                 int hightByte = 256 bytes[0];                 int lowByte = 256 bytes[1];                 asc = (256 * hightByte lowByte) - 256 * 256;             }         } catch (Exception e) {             System.out.println("ERROR:ChineseSpelling.class-getChsAscii(String chs)" e);         }         return asc;     }     /**      * 单字解析 * * @param str * @return      */     public String convert(String str) {         String result = null;         int ascii = getChsAscii(str);         if (ascii > 0 && ascii < 160) {             result = String.valueOf((char) ascii);         } else {             for (int i = (pyvalue.length - 1); i >= 0; i--) {                 if (pyvalue[i] <= ascii) {                     result = pystr[i];                     break;                 }             }         }         return result;     }     /**      * 词组解析 * * @param chs * @return      */     public String getSelling(String chs) {         String key, value;         buffer = new StringBuilder();         for (int i = 0; i < chs.length(); i ) {             key = chs.substring(i, i 1);             if (key.getBytes().length >= 2) {                 value = convert(key);                 if (value == null) {                     value = "unknown";                 }             } else {                 value = key;             }             buffer.append(value);         }         return buffer.toString();     }     public String getSpelling() {         return this.getSelling(this.getResource());     } }

PinyinComparator.java

import java.util.Comparator; import com.yizhibo.video.bean.SortModel; /**  * @author xiaanming  */ public class PinyinComparator implements Comparator<SortModel> {     public int compare(SortModel o1, SortModel o2) {         if (o1.getSortLetter().equals("@")                 || o2.getSortLetter().equals("#")) {             return -1;         } else if (o1.getSortLetter().equals("#")                 || o2.getSortLetter().equals("@")) {             return 1;         } else {             return o1.getSortLetter().compareTo(o2.getSortLetter());         }     } }

WrapperView.java

import android.content.Context; import android.graphics.Canvas; import android.graphics.drawable.Drawable; import android.os.Build; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; /**  *   */ class WrapperView extends ViewGroup { View mItem; Drawable mDivider; int mDividerHeight; View mHeader; int mItemTop; public WrapperView(Context c) { super(c); } void update(View item, View header, Drawable divider, int dividerHeight) { if (item == null) { throw new NullPointerException("List view item must not be null."); } if (this.mItem != item) { removeView(this.mItem); this.mItem = item; final ViewParent parent = item.getParent(); if(parent != null && parent != this) { if(parent instanceof ViewGroup) { ((ViewGroup) parent).removeView(item); } } addView(item); } if (this.mHeader != header) { if (this.mHeader != null) { removeView(this.mHeader); } this.mHeader = header; if (header != null) { addView(header); } } if (this.mDivider != divider) { this.mDivider = divider; this.mDividerHeight = dividerHeight; invalidate(); } } boolean hasHeader() { return mHeader != null; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int measuredWidth = MeasureSpec.getSize(widthMeasureSpec); int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(measuredWidth, MeasureSpec.EXACTLY); int measuredHeight = 0; //measure header or divider. when there is a header visible it acts as the divider if (mHeader != null) { LayoutParams params = mHeader.getLayoutParams(); if (params != null && params.height > 0) { mHeader.measure(childWidthMeasureSpec, MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY)); } else { mHeader.measure(childWidthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); } measuredHeight = mHeader.getMeasuredHeight(); } else if (mDivider != null) { measuredHeight = mDividerHeight; } //measure item LayoutParams params = mItem.getLayoutParams(); if (params != null && params.height > 0) { mItem.measure(childWidthMeasureSpec, MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY)); } else { mItem.measure(childWidthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); } measuredHeight = mItem.getMeasuredHeight(); setMeasuredDimension(measuredWidth, measuredHeight); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { l = 0; t = 0; r = getWidth(); b = getHeight(); if (mHeader != null) { int headerHeight = mHeader.getMeasuredHeight(); mHeader.layout(l, t, r, headerHeight); mItemTop = headerHeight; mItem.layout(l, headerHeight, r, b); } else if (mDivider != null) { mDivider.setBounds(l, t, r, mDividerHeight); mItemTop = mDividerHeight; mItem.layout(l, mDividerHeight, r, b); } else { mItemTop = t; mItem.layout(l, t, r, b); } } @Override protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); if (mHeader == null && mDivider != null) { // Drawable.setBounds() does not seem to work pre-honeycomb. So have // to do this instead if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { canvas.clipRect(0, 0, getWidth(), mDividerHeight); } mDivider.draw(canvas); } } }

R.array.cities_data

代码语言:javascript复制
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="cities_data">
        <item>北京</item>
        <item>上海</item>
        <item>广州</item>
        <item>深圳</item>
        <item>天津</item>
        <item>西安</item>
        <item>福州</item>
        <item>重庆</item>
        <item>杭州</item>
        <item>宁波</item>
        <item>无锡</item>
        <item>南京</item>
        <item>合肥</item>
        <item>武汉</item>
        <item>成都</item>
        <item>青岛</item>
        <item>厦门</item>
        <item>大连</item>
        <item>沈阳</item>
        <item>长沙</item>
        <item>郑州</item>
        <item>石家庄</item>
        <item>苏州</item>
        <item>淄博</item>
        <item>南通</item>
        <item>南昌</item>
        <item>保定</item>
        <item>蚌埠</item>
        <item>常州</item>
        <item>大庆</item>
        <item>东莞</item>
        <item>佛山</item>
        <item>桂林</item>
        <item>海口</item>
        <item>葫芦岛</item>
        <item>济南</item>
        <item>焦作</item>
        <item>锦州</item>
        <item>南宁</item>
        <item>太原</item>
        <item>芜湖</item>
        <item>新乡</item>
        <item>烟台</item>
        <item>哈尔滨</item>
        <item>廊坊</item>
        <item>贵阳</item>
        <item>珠海</item>
        <item>齐齐哈尔</item>
        <item>泉州</item>
        <item>三亚</item>
        <item>温州</item>
        <item>中山</item>
        <item>昆明</item>
        <item>九江</item>
        <item>长春</item>
        <item>汕头</item>
        <item>香港</item>
        <item>徐州</item>
        <item>扬州</item>
        <item>唐山</item>
        <item>秦皇岛</item>
        <item>邯郸</item>
        <item>邢台</item>
        <item>张家口</item>
        <item>承德</item>
        <item>沧州</item>
        <item>衡水</item>
        <item>大同</item>
        <item>阳泉</item>
        <item>长治</item>
        <item>晋城</item>
        <item>朔州</item>
        <item>晋中</item>
        <item>运城</item>
        <item>忻州</item>
        <item>临汾</item>
        <item>吕梁</item>
        <item>呼和浩特</item>
        <item>包头</item>
        <item>乌海</item>
        <item>赤峰</item>
        <item>通辽</item>
        <item>鄂尔多斯</item>
        <item>呼伦贝尔</item>
        <item>巴彦淖尔</item>
        <item>乌兰察布</item>
        <item>兴安</item>
        <item>锡林郭勒</item>
        <item>阿拉善</item>
        <item>鞍山</item>
        <item>抚顺</item>
        <item>本溪</item>
        <item>丹东</item>
        <item>营口</item>
        <item>阜新</item>
        <item>辽阳</item>
        <item>盘锦</item>
        <item>铁岭</item>
        <item>朝阳</item>
        <item>吉林</item>
        <item>四平</item>
        <item>辽源</item>
        <item>通化</item>
        <item>白山</item>
        <item>松原</item>
        <item>白城</item>
        <item>延边</item>
        <item>鸡西</item>
        <item>鹤岗</item>
        <item>双鸭山</item>
        <item>伊春</item>
        <item>佳木斯</item>
        <item>七台河</item>
        <item>牡丹江</item>
        <item>黑河</item>
        <item>绥化</item>
        <item>大兴安岭</item>
        <item>连云港</item>
        <item>淮安</item>
        <item>盐城</item>
        <item>镇江</item>
        <item>泰州</item>
        <item>宿迁</item>
        <item>嘉兴</item>
        <item>湖州</item>
        <item>绍兴</item>
        <item>金华</item>
        <item>衢州</item>
        <item>舟山</item>
        <item>台州</item>
        <item>丽水</item>
        <item>淮南</item>
        <item>马鞍山</item>
        <item>淮北</item>
        <item>铜陵</item>
        <item>安庆</item>
        <item>黄山</item>
        <item>滁州</item>
        <item>阜阳</item>
        <item>宿州</item>
        <item>巢湖</item>
        <item>六安</item>
        <item>亳州</item>
        <item>池州</item>
        <item>宣城</item>
        <item>莆田</item>
        <item>三明</item>
        <item>漳州</item>
        <item>南平</item>
        <item>龙岩</item>
        <item>宁德</item>
        <item>景德镇</item>
        <item>萍乡</item>
        <item>新余</item>
        <item>鹰潭</item>
        <item>赣州</item>
        <item>吉安</item>
        <item>宜春</item>
        <item>抚州</item>
        <item>上饶</item>
        <item>枣庄</item>
        <item>东营</item>
        <item>潍坊</item>
        <item>济宁</item>
        <item>泰安</item>
        <item>威海</item>
        <item>日照</item>
        <item>莱芜</item>
        <item>临沂</item>
        <item>德州</item>
        <item>聊城</item>
        <item>滨州</item>
        <item>菏泽</item>
        <item>开封</item>
        <item>洛阳</item>
        <item>平顶山</item>
        <item>安阳</item>
        <item>鹤壁</item>
        <item>濮阳</item>
        <item>许昌</item>
        <item>漯河</item>
        <item>三门峡</item>
        <item>南阳</item>
        <item>商丘</item>
        <item>信阳</item>
        <item>周口</item>
        <item>驻马店</item>
        <item>济源</item>
        <item>黄石</item>
        <item>十堰</item>
        <item>宜昌</item>
        <item>襄阳</item>
        <item>鄂州</item>
        <item>荆门</item>
        <item>孝感</item>
        <item>荆州</item>
        <item>黄冈</item>
        <item>咸宁</item>
        <item>随州</item>
        <item>恩施</item>
        <item>三峡</item>
        <item>株洲</item>
        <item>湘潭</item>
        <item>衡阳</item>
        <item>邵阳</item>
        <item>岳阳</item>
        <item>常德</item>
        <item>张家界</item>
        <item>益阳</item>
        <item>郴州</item>
        <item>永州</item>
        <item>怀化</item>
        <item>娄底</item>
        <item>湘西</item>
        <item>韶关</item>
        <item>江门</item>
        <item>湛江</item>
        <item>茂名</item>
        <item>肇庆</item>
        <item>惠州</item>
        <item>梅州</item>
        <item>汕尾</item>
        <item>河源</item>
        <item>阳江</item>
        <item>清远</item>
        <item>潮州</item>
        <item>揭阳</item>
        <item>云浮</item>
        <item>柳州</item>
        <item>梧州</item>
        <item>北海</item>
        <item>防城港</item>
        <item>钦州</item>
        <item>贵港</item>
        <item>玉林</item>
        <item>百色</item>
        <item>贺州</item>
        <item>河池</item>
        <item>来宾</item>
        <item>崇左</item>
        <item>自贡</item>
        <item>攀枝花</item>
        <item>泸州</item>
        <item>德阳</item>
        <item>绵阳</item>
        <item>广元</item>
        <item>遂宁</item>
        <item>内江</item>
        <item>乐山</item>
        <item>南充</item>
        <item>眉山</item>
        <item>宜宾</item>
        <item>广安</item>
        <item>达州</item>
        <item>雅安</item>
        <item>巴中</item>
        <item>资阳</item>
        <item>阿坝</item>
        <item>甘孜</item>
        <item>凉山</item>
        <item>六盘水</item>
        <item>遵义</item>
        <item>安顺</item>
        <item>铜仁</item>
        <item>黔西南</item>
        <item>毕节</item>
        <item>黔东南</item>
        <item>黔南</item>
        <item>曲靖</item>
        <item>玉溪</item>
        <item>保山</item>
        <item>昭通</item>
        <item>丽江</item>
        <item>普洱</item>
        <item>临沧</item>
        <item>楚雄</item>
        <item>红河</item>
        <item>文山</item>
        <item>西双版纳</item>
        <item>大理</item>
        <item>德宏</item>
        <item>怒江</item>
        <item>迪庆</item>
        <item>拉萨</item>
        <item>昌都</item>
        <item>山南</item>
        <item>日喀则</item>
        <item>那曲</item>
        <item>阿里</item>
        <item>林芝</item>
        <item>铜川</item>
        <item>宝鸡</item>
        <item>咸阳</item>
        <item>渭南</item>
        <item>延安</item>
        <item>汉中</item>
        <item>榆林</item>
        <item>安康</item>
        <item>商洛</item>
        <item>兰州</item>
        <item>金昌</item>
        <item>白银</item>
        <item>天水</item>
        <item>武威</item>
        <item>张掖</item>
        <item>平凉</item>
        <item>酒泉</item>
        <item>庆阳</item>
        <item>定西</item>
        <item>陇南</item>
        <item>临夏</item>
        <item>甘南</item>
        <item>西宁</item>
        <item>海东</item>
        <item>海北</item>
        <item>黄南</item>
        <item>海南州</item>
        <item>果洛</item>
        <item>玉树</item>
        <item>海西</item>
        <item>银川</item>
        <item>石嘴山</item>
        <item>吴忠</item>
        <item>固原</item>
        <item>中卫</item>
        <item>乌鲁木齐</item>
        <item>克拉玛依</item>
        <item>吐鲁番</item>
        <item>哈密</item>
        <item>昌吉</item>
        <item>博尔塔拉</item>
        <item>巴州</item>
        <item>阿克苏</item>
        <item>克州</item>
        <item>喀什</item>
        <item>和田</item>
        <item>伊犁</item>
        <item>塔城</item>
        <item>阿勒泰</item>
        <item>台北</item>
        <item>澳门</item>
        <item>昆山</item>
        <item>江阴</item>
        <item>义乌</item>
        <item>顺德</item>
        <item>石河子</item>
        <item>嘉峪关</item>
        <item>花都</item>
        <item>番禺</item>
        <item>仙桃</item>
        <item>富阳</item>
        <item>峨眉山</item>
        <item>琼海</item>
        <item>张家港</item>
        <item>晋江</item>
        <item>从化</item>
        <item>常熟</item>
        <item>延吉</item>
        <item>海宁</item>
        <item>桐乡</item>
        <item>涿州</item>
        <item>涪陵</item>
        <item>万州</item>
        <item>江宁</item>
        <item>迁安</item>
        <item>丹阳</item>
        <item>太仓</item>
        <item>吴江</item>
        <item>敦煌</item>
        <item>塘沽</item>
        <item>靖江</item>
        <item>石狮</item>
        <item>武夷山</item>
        <item>萧山</item>
    </string-array>
</resources>

最后效果图  gif不太会弄 只能放静态图了

为了方便大家,整理出一个demo。 DEMO源码下载

0 人点赞