Created
August 12, 2015 07:57
-
-
Save hector6872/88a21da2256b49e07b18 to your computer and use it in GitHub Desktop.
HorizontalScrollViewSelector
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
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); | |
} | |
}); |
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 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