Skip to content

Instantly share code, notes, and snippets.

@hrules6872
Created October 13, 2015 22:15
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/9ce8c62cd303fafc05bd to your computer and use it in GitHub Desktop.
Save hrules6872/9ce8c62cd303fafc05bd to your computer and use it in GitHub Desktop.
RightAlignedHorizontalScrollView
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RightAlignedHorizontalScrollView">
<attr name="rahsv_gravityRight" format="boolean" />
<attr name="rahsv_autoScroll" format="boolean" />
</declare-styleable>
</resources>
public class RightAlignedHorizontalScrollView extends HorizontalScrollView {
private static final boolean DEFAULT_GRAVITY_RIGHT = true;
private static final boolean DEFAULT_AUTOSCROLL = true;
private boolean autoScroll;
private boolean gravityRight;
public RightAlignedHorizontalScrollView(Context context) {
this(context, null);
}
public RightAlignedHorizontalScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RightAlignedHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public RightAlignedHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(final Context context, final AttributeSet attrs) {
final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RightAlignedHorizontalScrollView);
gravityRight = typedArray.getBoolean(R.styleable.RightAlignedHorizontalScrollView_rahsv_gravityRight, DEFAULT_GRAVITY_RIGHT);
autoScroll = typedArray.getBoolean(R.styleable.RightAlignedHorizontalScrollView_rahsv_autoScroll, DEFAULT_AUTOSCROLL);
typedArray.recycle();
}
View getChildView() {
return getChildAt(0);
}
private int getScrollRange() {
int scrollRange = 0;
if (getChildCount() > 0) {
View childView = getChildView();
scrollRange = Math.max(0, childView.getWidth() - (getWidth() - getPaddingLeft() - getPaddingRight()));
}
return scrollRange;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
boolean canScroll = true;
FrameLayout.LayoutParams layoutParams = (LayoutParams) getChildView().getLayoutParams();
int verticalGravity = layoutParams.gravity & Gravity.VERTICAL_GRAVITY_MASK;
int childWidth = getChildView().getWidth();
super.onLayout(changed, left, top, right, bottom);
int delta = getChildView().getWidth() - childWidth;
if (gravityRight) {
if (getScrollRange() > 0) {
canScroll = true;
layoutParams.gravity = Gravity.START | verticalGravity;
getChildView().setLayoutParams(layoutParams);
super.onLayout(changed, left, top, right, bottom);
} else if (getScrollRange() == 0) {
canScroll = false;
layoutParams.gravity = Gravity.END | verticalGravity;
getChildView().setLayoutParams(layoutParams);
super.onLayout(changed, left, top, right, bottom);
}
}
if (autoScroll && gravityRight && canScroll && delta > 0) {
scrollBy(delta, 0);
}
}
public boolean isAutoScroll() {
return autoScroll;
}
public void setAutoScroll(boolean autoScroll) {
this.autoScroll = autoScroll;
invalidate();
}
public boolean isGravityRight() {
return gravityRight;
}
public void setGravityRight(boolean gravityRight) {
this.gravityRight = gravityRight;
invalidate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment