Android開発37:カスタムのHorizontalScrollViewクラスをpageScrollにするときにフォーカスが選択されないようにする
3059 ワード
HorizontalScrollViewクラスをカスタマイズします.主にこのHorizontalScrollViewをマウスでクリックできず、左右のボタンができず、焦点がありません.
次の操作を行います.
public class ImageMoveHorizontalScrollView extends HorizontalScrollView {
private boolean mSmoothScrollingEnabled = true;
private final Rect mTempRect = new Rect();
public ImageMoveHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ImageMoveHorizontalScrollView(Context context) {
super(context);
}
/**
* ,
*/
@Override
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
/**
* ,
*/
@Override
public boolean executeKeyEvent(KeyEvent event) {
return false;
}
/**
*
*/
@Override
public boolean pageScroll(int direction) {
boolean right = direction == View.FOCUS_RIGHT;
int width = getWidth();
if (right) {
mTempRect.left = getScrollX() + width;
int count = getChildCount();
if (count > 0) {
View view = getChildAt(0);
if (mTempRect.left + width > view.getRight()) {
mTempRect.left = view.getRight() - width;
}
}
} else {
mTempRect.left = getScrollX() - width;
if (mTempRect.left < 0) {
mTempRect.left = 0;
}
}
mTempRect.right = mTempRect.left + width;
return scrollAndFocus(direction, mTempRect.left, mTempRect.right);
}
private boolean scrollAndFocus(int direction, int left, int right) {
boolean handled = true;
int width = getWidth();
int containerLeft = getScrollX();
int containerRight = containerLeft + width;
boolean goLeft = direction == View.FOCUS_LEFT;
// ,
// View newFocused = findFocusableViewInBounds(goLeft, left, right);
// if (newFocused == null) {
// newFocused = this;
// }
if (left >= containerLeft && right <= containerRight) {
handled = false;
} else {
int delta = goLeft ? (left - containerLeft) : (right - containerRight);
doScrollX(delta);
}
// foucus
// if (newFocused != findFocus())
// newFocused.requestFocus(direction);
return handled;
}
/**
* Smooth scroll by a X delta
* @param delta the number of pixels to scroll by on the X axis
*/
private void doScrollX(int delta) {
if (delta != 0) {
if (mSmoothScrollingEnabled) {
smoothScrollBy(delta, 0);
} else {
scrollBy(delta, 0);
}
}
}
}
次の操作を行います.
sc.pageScroll(View.FOCUS_RIGHT); //