Skip to content

Instantly share code, notes, and snippets.

@Jungerr
Created January 30, 2016 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jungerr/23ecd8e466051c9de15b to your computer and use it in GitHub Desktop.
Save Jungerr/23ecd8e466051c9de15b to your computer and use it in GitHub Desktop.
package com.personal.lqg;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;
import java.util.List;
/**
* Created by LeeQianguo on 14-10-08.
*/
public class FullScrollNotClearFocusScrollView extends ScrollView {
private final Rect mTempRect = new Rect();
public FullScrollNotClearFocusScrollView(Context context) {
super(context);
}
public FullScrollNotClearFocusScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FullScrollNotClearFocusScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean fullScroll(int direction) {
boolean down = direction == View.FOCUS_DOWN;
int height = getHeight();
mTempRect.top = 0;
mTempRect.bottom = height;
if (down) {
int count = getChildCount();
if (count > 0) {
View view = getChildAt(count - 1);
mTempRect.bottom = view.getBottom() + getPaddingBottom();
mTempRect.top = mTempRect.bottom - height;
}
}
return scrollAndFocus(direction, mTempRect.top, mTempRect.bottom);
}
private boolean scrollAndFocus(int direction, int top, int bottom) {
boolean handled = true;
int height = getHeight();
int containerTop = getScrollY();
int containerBottom = containerTop + height;
boolean up = direction == View.FOCUS_UP;
if (top >= containerTop && bottom <= containerBottom) {
handled = false;
} else {
int delta = up ? (top - containerTop) : (bottom - containerBottom);
doScrollY(delta);
}
return handled;
}
private void doScrollY(int delta) {
if (delta != 0) {
if (isSmoothScrollingEnabled()) {
smoothScrollBy(0, delta);
} else {
scrollBy(0, delta);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment