系统不支持快速滚动栏在左侧,没事,自己写一个:效果如下:
控件类:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class LPFastScrollBar extends View {
OnChangeFastScrollPlaceListener onChangeFastScrollPlaceListener;
private float currentY = 0;
private float savedY = 0;
private float downY = 0;
private Bitmap bitFastScroll;
private int viewWidth = 0;
private int viewHeight = 0;
private int barWidth = 0;
private int barHeight = 0;
public LPFastScrollBar(Context context) {
super(context);
initParams();
}
public LPFastScrollBar(Context context, AttributeSet attrs) {
super(context, attrs);
initParams();
}
private void initParams(){
bitFastScroll = BitmapFactory.decodeResource(getResources(), R.drawable.ca_sc_003);
barWidth = bitFastScroll.getWidth();
barHeight = bitFastScroll.getHeight();
}
public int getBarWidth(){
return barWidth;
}
public void setCurrentPlace(float precent){
currentY = (viewHeight - barHeight) * precent;
savedY = currentY;
invalidate();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(barWidth, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(viewWidth == 0 || viewHeight == 0){
viewWidth = getWidth();
viewHeight = getHeight();
}
Log.d("fast", "viewHeight:"+viewHeight);
Paint paint = new Paint();
canvas.drawBitmap(bitFastScroll, 0, currentY, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
currentY = savedY;
downY = eventY;
Log.d("fast", "down:"+currentY);
break;
case MotionEvent.ACTION_MOVE:
if((int)(savedY + eventY - downY) >= 0 && (int)(savedY + eventY - downY) <= (viewHeight - barHeight)){
currentY = savedY + eventY - downY;
float precent = currentY / (viewHeight - barHeight);
onChangeFastScrollPlaceListener.onTouchingLetterChanged(precent);
onChangeFastScrollPlaceListener.onState(true);
}
Log.d("fast", "move:"+currentY);
break;
case MotionEvent.ACTION_UP:
savedY = currentY;
onChangeFastScrollPlaceListener.onState(false);
Log.d("fast", "up:"+currentY);
break;
}
invalidate();
return true;
}
public void setOnChangeFastScrollPlace(OnChangeFastScrollPlaceListener onChangeListener){
this.onChangeFastScrollPlaceListener = onChangeListener;
}
public interface OnChangeFastScrollPlaceListener{
public void onTouchingLetterChanged(float precent);
public void onState(boolean isMoving);
}
}
调用方法:
listMain = (ListView) findViewById(R.id.listView1);
listMain.setAdapter(new MyBaseAdapter());
lpFastScrollBar = (LPFastScrollBar) findViewById(R.id.fastScrollBar1);
lpFastScrollBar
.setOnChangeFastScrollPlace(new OnChangeFastScrollPlaceListener() {
@Override
public void onTouchingLetterChanged(float precent) {
// TODO Auto-generated method stub
isFastScrolling = true;
if(listMain.getCount() > 0)
listMain.setSelection((int) ((listMain.getCount() - listMain.getHeight() / listMain.getChildAt(0).getHeight()) * precent));
}
@Override
public void onState(boolean isMoving) {
// TODO Auto-generated method stub
isFastScrolling = isMoving;
}
});
listMain.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (curVisibleItemCount == 0) {
curVisibleItemCount = visibleItemCount - 1;
}
if (!isFastScrolling) {
lpFastScrollBar.setCurrentPlace((float) firstVisibleItem
/ (totalItemCount - visibleItemCount));
}
}
});