2013年8月11日星期日

android inheritance View, onDraw methods to achieve fastpositioning initials

 
  
package com.example.contactssort; 

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.SectionIndexer;

public class SideBar extends View {
private int textColor = 0xFFA6A9AA;
private int backgroundColor = 0x55DDDDDD;
private int backgroundActionDownColor = 0xFFCCCCCC;
private boolean isDown = false;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public SideBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

private char[] l = new char[0];
private SectionIndexer sectionIndexter = null;
private ListView list;
private int m_nItemHeight = 29;
private int selfHeight;
private int selfWidth;
private float textSize;
private int selectIndex = -1;

public SideBar(Context context) {
super(context);
init();
}

private void init() {
// setBackgroundColor(backgroundColor);
}

public SideBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public void setListView(ListView _list) {
list
= _list;
sectionIndexter
= (SectionIndexer) _list.getAdapter();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
this.textSize = getResources().getDimension(R.dimen.index_text_size);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
this.selfHeight = height;
this.selfWidth = width;
m_nItemHeight
= selfHeight / l.length;
int newWidth = MeasureSpec.makeMeasureSpec((int) this.textSize + 5, MeasureSpec.AT_MOST);
setMeasuredDimension(newWidth, heightMeasureSpec);
}

public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
int i = (int) event.getY();
int idx = i / m_nItemHeight;
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) {
if (sectionIndexter == null) {
sectionIndexter
= (SectionIndexer) list.getAdapter();
}
int position = sectionIndexter.getPositionForSection(l[idx]);
if (position == -1) {
return true;
}
this.selectIndex = idx;
list.setSelection(position);
isDown
= true;
invalidate();
}
if (event.getAction() == MotionEvent.ACTION_UP) {
isDown
= false;
this.selectIndex = -1;
invalidate();
}
return true;
}

@SuppressLint(
"DrawAllocation")
protected void onDraw(Canvas canvas) {
Paint paint
= new Paint();
paint.setTextAlign(Paint.Align.CENTER);
paint.setTextSize(
this.textSize);
paint.setColor(textColor);
FontMetrics fm
= paint.getFontMetrics();
int textHeight = (int) (Math.ceil(fm.descent - fm.ascent) + 2);
float widthCenter = getMeasuredWidth() / 2;
RectF rect
= null;
Paint bg_paint
= new Paint(paint);

if (isDown) {
rect
= new RectF(0, 0, getWidth(), getHeight());
bg_paint.setColor(backgroundActionDownColor);

}
else {
rect
= new RectF(0, 0, getWidth(), getHeight());
bg_paint.setColor(backgroundColor);
}
canvas.save();
canvas.drawRoundRect(rect,
15, 15, bg_paint);
canvas.restore();
for (int i = 0; i < l.length; i++) {
if (selectIndex == i) {
Paint item_bg_paint
= new Paint(paint);
item_bg_paint.setColor(
0xffccffcc);
canvas.save();
RectF item_rect
= new RectF(0, i * m_nItemHeight, getWidth(), m_nItemHeight + i * m_nItemHeight);
canvas.drawRoundRect(item_rect,
15, 15, item_bg_paint);
canvas.restore();
}
else if (selectIndex == l.length - 1) {
Paint item_bg_paint
= new Paint(paint);
item_bg_paint.setColor(
0xffccffcc);
canvas.save();
RectF item_rect
= new RectF(0, l.length * m_nItemHeight, getWidth(), getHeight());
canvas.drawRoundRect(item_rect,
15, 15, item_bg_paint);
canvas.restore();
}
  

float offsetY = ((i) * m_nItemHeight ) + (( m_nItemHeight -textHeight) / 2 + textHeight-8) ;/ / font vertically centered , 20130530 Updates

  

canvas.drawText (String.valueOf ( l [i]), widthCenter, offsetY, paint);

  
 
}
super.onDraw(canvas);
}

public void setSections(char[] charArray) {
this.l = charArray;
invalidate();
}

}
 
 
  
package com.example.contactssort; 

import java.util.ArrayList;
import java.util.List;

public class IndexItem {

List
<String> indexs = new ArrayList<String>();

/**
* 添加数据索引。每一个数据都要添加
*
*
@param c
*/
public void addIndex(char c) {
indexs.add(String.valueOf(c));
}

public int getCount() {
return indexs.size();
}
public int indexAt(char c){
return indexs.indexOf(String.valueOf(c));
}
public String get(int position) {
return this.indexs.get(position);
}
}
 
 
  
package com.example.contactssort; 

import java.util.ArrayList;
import java.util.Locale;


import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AlphabetIndexer;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter implements SectionIndexer {
private ArrayList<String> stringArray;
private Context context;
IndexItem indexs;
public MyAdapter(Context _context, ArrayList<String> arr,SideBar sidebar) {
stringArray
= arr;
context
= _context;
String alphabet
="";
indexs
=new IndexItem();
for(String frist:arr){
char c = frist.charAt(0);
String upperCase
= String.valueOf(c).toUpperCase();
if(!alphabet.contains(upperCase)){
alphabet
+=upperCase;
}
indexs.addIndex(upperCase.charAt(
0));
}
sidebar.setSections(alphabet.toUpperCase().toCharArray());
}

public int getCount() {
return stringArray.size();
}

public Object getItem(int arg0) {
return stringArray.get(arg0);
}

public long getItemId(int arg0) {
return 0;
}

public View getView(int position, View v, ViewGroup parent) {
LayoutInflater inflate
= ((Activity) context).getLayoutInflater();
View view
= (View) inflate.inflate(R.layout.listview_row, null);
TextView header
= (TextView) view.findViewById(R.id.section);
TextView textView
= (TextView) view.findViewById(R.id.textView);
char currentF=stringArray.get(position).charAt(0);
if(position==0){
header.setVisibility(View.VISIBLE);
header.setText((
""+currentF).toUpperCase());
}
else{

char oldF=stringArray.get(position-1).charAt(0);
if(!(""+currentF).equalsIgnoreCase((""+oldF))){
header.setVisibility(View.VISIBLE);
header.setText((
""+currentF).toUpperCase());
}
else{
header.setVisibility(View.GONE);
header.setText(
"");
}
}
textView.setText(stringArray.get(position));
return view;
}

public int getPositionForSection(int section) {
int position=indexs.indexAt((char) section);
return position;
}

public int getSectionForPosition(int arg0) {
return 0;
}

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

}
 
 
  
package com.example.contactssort; 

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list
= (ListView) findViewById(R.id.myListView);
SideBar side
=(SideBar)findViewById(R.id.side);
ArrayList
<String> stringList = InitListViewData();
MyAdapter adapter
= new MyAdapter(this, stringList,side);
side.setListView(list);
list.setAdapter(adapter);
}

private ArrayList<String> InitListViewData() {
ArrayList
<String> stringList = new ArrayList<String>();
stringList.add(
"asdf");
stringList.add(
"anbcd");
stringList.add(
"bdcd");
stringList.add(
"bfefasf");
stringList.add(
"bcadaf");
stringList.add(
"csdafad");
stringList.add(
"dfdaf");
stringList.add(
"edfdfd");
stringList.add(
"fsdfadfasfd");
stringList.add(
"fadsfasdf");
stringList.add(
"fweff");
stringList.add(
"fhghdfgh");
stringList.add(
"gasdfasdfsa");
stringList.add(
"gadfawe");
stringList.add(
"haof");
stringList.add(
"hjooiuoiuio");
stringList.add(
"iasdfasdfas");
stringList.add(
"zero");
stringList.add(
"zoo");
stringList.add(
"zeus");
stringList.add(
"zebra");
stringList.add(
"zest");
stringList.add(
"zing");
return stringList;
}
}
 
 

 

 

没有评论:

发表评论