Custom Left Fast Scroll Bar自定义左侧快速滚动条

系统不支持快速滚动栏在左侧,没事,自己写一个:效果如下:


控件类:

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));
				}
			}
		});


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值