Skip to content

Instantly share code, notes, and snippets.

@hrules6872
Created August 12, 2015 07:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrules6872/88a21da2256b49e07b18 to your computer and use it in GitHub Desktop.
Save hrules6872/88a21da2256b49e07b18 to your computer and use it in GitHub Desktop.
HorizontalScrollViewSelector
int totalItems=10;
horizontalScrollView = (HorizontalScrollViewSelector) findViewById(R.id.horizontalScrollView);
horizontalScrollView.setListener(new HorizontalScrollViewSelector.OnScrollChangedListener() {
@Override
public void onScrollChanged(int totalX, int x) {
items = x * totalItems / maxX);
}
});
public class HorizontalScrollViewSelector extends HorizontalScrollView {
private static final boolean DEFAULT_FLING_STATE = true;
private OnScrollChangedListener listener;
private boolean flingState;
public HorizontalScrollViewSelector(Context context) {
this(context, null);
}
public HorizontalScrollViewSelector(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public HorizontalScrollViewSelector(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
flingState = DEFAULT_FLING_STATE;
}
@Override
public void fling(int velocityY) {
if (flingState) {
super.fling(velocityY);
}
}
public boolean isFlingState() {
return flingState;
}
public void setFlingState(boolean flingState) {
this.flingState = flingState;
}
@Override
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
View view = getChildAt(getChildCount() - 1);
int totalX = (view.getRight() - (getWidth()));
if (listener != null) {
listener.onScrollChanged(totalX, x);
}
super.onScrollChanged(x, y, oldX, oldY);
}
public OnScrollChangedListener getListener() {
return listener;
}
public void setListener(OnScrollChangedListener listener) {
this.listener = listener;
}
public interface OnScrollChangedListener {
void onScrollChanged(int totalX, int x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment