Created
January 23, 2013 07:05
-
-
Save youxiachai/4602734 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class IndexXlistView extends XListView { | |
private boolean mIsFastScrollEnabled = false; | |
private IndexScroller mScroller = null; | |
private GestureDetector mGestureDetector = null; | |
public IndexXlistView(Context context) { | |
super(context); | |
// TODO Auto-generated constructor stub | |
} | |
public IndexXlistView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
// TODO Auto-generated constructor stub | |
} | |
public IndexXlistView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
// TODO Auto-generated constructor stub | |
} | |
@Override | |
public void setFastScrollEnabled(boolean enabled) { | |
mIsFastScrollEnabled = enabled; | |
if (mIsFastScrollEnabled) { | |
if (mScroller == null) | |
mScroller = new IndexScroller(getContext(), this); | |
} else { | |
if (mScroller != null) { | |
mScroller.hide(); | |
mScroller = null; | |
} | |
} | |
} | |
@Override | |
public void draw(Canvas canvas) { | |
super.draw(canvas); | |
if (mScroller != null) | |
mScroller.draw(canvas); | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent ev) { | |
// TODO Auto-generated method stub | |
if (mScroller != null && mScroller.onTouchEvent(ev)) | |
return true; | |
if (mGestureDetector == null) { | |
mGestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() { | |
@Override | |
public boolean onFling(MotionEvent e1, MotionEvent e2, | |
float velocityX, float velocityY) { | |
// If fling happens, index bar shows | |
if(isFastScrollEnabled()){ | |
mScroller.show(); | |
} | |
return super.onFling(e1, e2, velocityX, velocityY); | |
} | |
}); | |
} | |
mGestureDetector.onTouchEvent(ev); | |
return super.onTouchEvent(ev); | |
} | |
@Override | |
public boolean isFastScrollEnabled() { | |
return mIsFastScrollEnabled; | |
} | |
@Override | |
public boolean onInterceptTouchEvent(MotionEvent ev) { | |
return true; | |
} | |
@Override | |
public void setAdapter(ListAdapter adapter) { | |
super.setAdapter(adapter); | |
if (mScroller != null) | |
mScroller.setAdapter(adapter); | |
} | |
@Override | |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
super.onSizeChanged(w, h, oldw, oldh); | |
if (mScroller != null) | |
mScroller.onSizeChanged(w, h, oldw, oldh); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment