Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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