LocationSelectAutoTextListAdapater
package com.reacnn.android.assistadpter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
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.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;
import com.reacnn.android.R;
import com.reacnn.android.bean.CountryRegion;
import com.reacnn.android.utils.LogUtil;
public class LocationSelectAutoTextListAdapater extends BaseAdapter implements
Filterable, SectionIndexer {
private LayoutInflater inflater;
private List<CountryRegion> list;
private ArrayList<CountryRegion> mOriginalValues;
private HashMap<String, Integer> alphaIndexer;
private String[] sections;
MyFilter myFilter;
private final Object mLock = new Object();
private final String TAG = "LocationSelectAutoTextListAdapater";
public LocationSelectAutoTextListAdapater(Context context,
List<CountryRegion> list) {
this.inflater = LayoutInflater.from(context);
this.list = list;
this.alphaIndexer = new HashMap<String, Integer>();
this.sections = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
String name = list.get(i).getAlphaString();
if (!alphaIndexer.containsKey(name)) {
alphaIndexer.put(name, i);
}
}
Set<String> sectionLetters = alphaIndexer.keySet();
ArrayList<String> sectionList = new ArrayList<String>(sectionLetters);
Collections.sort(sectionList);
sections = new String[sectionList.size()];
sectionList.toArray(sections);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder {
LinearLayout alphalayout;
TextView alphaTextView;
TextView name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.layout_item_selectlocation,
null);
holder = new ViewHolder();
holder.alphalayout = (LinearLayout) convertView
.findViewById(R.id.alphalayout);
holder.name = (TextView) convertView
.findViewById(R.id.locationname);
holder.alphaTextView = (TextView) convertView
.findViewById(R.id.locationalpha);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
CountryRegion cv = list.get(position);
holder.name.setText(cv.getRegionString());
String currentStr = cv.getAlphaString();
String previewStr = (position - 1) >= 0 ? list.get(position - 1)
.getAlphaString() : " ";
if (!previewStr.equals(currentStr)) {
holder.alphalayout.setVisibility(View.VISIBLE);
holder.alphaTextView.setText(currentStr);
} else {
holder.alphalayout.setVisibility(View.GONE);
}
return convertView;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public Filter getFilter() {
if (null == myFilter) {
myFilter = new MyFilter();
}
return myFilter;
}
class MyFilter extends Filter {
List<CountryRegion> strsContains;
public MyFilter() {
super();
strsContains = list;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
LogUtil.e(TAG, "constraint=" + constraint, false);
FilterResults results = new FilterResults();
if (mOriginalValues == null) {
synchronized (mLock) {
mOriginalValues = new ArrayList<CountryRegion>(list);
}
}
if (constraint == null || constraint.length() == 0) {
synchronized (mLock) {
ArrayList<CountryRegion> list = new ArrayList<CountryRegion>(
mOriginalValues);
results.values = list;
results.count = list.size();
}
} else {
String prefixString = constraint.toString().toLowerCase();
final ArrayList<CountryRegion> values = mOriginalValues;
final int count = values.size();
final ArrayList<CountryRegion> newValues = new ArrayList<CountryRegion>(
count);
for (int i = 0; i < count; i++) {
final CountryRegion value = (CountryRegion) values.get(i);
if (value.getRegionString().contains(prefixString)) {
newValues.add(value);
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// 重新将与适配器相关联的List重赋值一下
list = (List<CountryRegion>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
@Override
public int getPositionForSection(int section) {
CountryRegion mContent;
String l;
if (section == '!') {
return 0;
} else {
for (int i = 0; i < getCount(); i++) {
mContent = (CountryRegion) list.get(i);
l = mContent.getAlphaString();
char firstChar = l.toUpperCase().charAt(0);
if (firstChar == section) {
return i + 1;
}
}
}
mContent = null;
l = null;
return -1;
}
@Override
public int getSectionForPosition(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object[] getSections() {
// TODO Auto-generated method stub
return null;
}
}
SideBar
package com.reacnn.android.view;
import com.reacnn.android.assistadpter.LocationSelectAutoTextListAdapater;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HeaderViewListAdapter;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.TextView;
public class SideBar extends View {
private char[] l;
private SectionIndexer sectionIndexter = null;
private ListView list;
private TextView mDialogText;
private int color = 0xff858c94;
public SideBar(Context context) {
super(context);
init();
}
public SideBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
l = new char[] { '#', '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' };
}
public SideBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void setListView(ListView _list) {
list = _list;
HeaderViewListAdapter ha = (HeaderViewListAdapter) _list.getAdapter();
LocationSelectAutoTextListAdapater ad = (LocationSelectAutoTextListAdapater) ha
.getWrappedAdapter();
sectionIndexter = (SectionIndexer) ad;
}
public void setTextView(TextView mDialogText) {
this.mDialogText = mDialogText;
}
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
int i = (int) event.getY();
int idx = i / (getMeasuredHeight() / l.length);
if (idx >= l.length) {
idx = l.length - 1;
} else if (idx < 0) {
idx = 0;
}
if (event.getAction() == MotionEvent.ACTION_DOWN
|| event.getAction() == MotionEvent.ACTION_MOVE) {
mDialogText.setVisibility(View.VISIBLE);
mDialogText.setText(String.valueOf(l[idx]));
mDialogText.setTextSize(34);
if (sectionIndexter == null) {
sectionIndexter = (SectionIndexer) list.getAdapter();
}
int position = sectionIndexter.getPositionForSection(l[idx]);
if (position == -1) {
return true;
}
list.setSelection(position);
} else {
mDialogText.setVisibility(View.INVISIBLE);
}
if (event.getAction() == MotionEvent.ACTION_UP) {
setBackgroundDrawable(new ColorDrawable(0x00000000));
}
return true;
}
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(color);
paint.setTextSize(12);
paint.setStyle(Style.FILL);
paint.setTextAlign(Paint.Align.CENTER);
float widthCenter = getMeasuredWidth() / 2;
if (l.length > 0) {
float height = getMeasuredHeight() / l.length;
for (int i = 0; i < l.length; i++) {
canvas.drawText(String.valueOf(l[i]), widthCenter, (i + 1)
* height, paint);
}
}
this.invalidate();
super.onDraw(canvas);
}
}
indexBar.setTextView(mDialogText);
mDialogText.setVisibility(View.INVISIBLE);
indexBar.setListView(mNormalListView);
搜索:
if (adapter != null&& (StringUtils.IsEmptyOrNull(this.txtSearchKeyText.getText().toString()) != true)) {
adapter.getFilter().filter(this.txtSearchKeyText.getText().toString());}