android-HorizontalScrollView-逃跑后计算停止位置
作者:互联网
使用HorizontalScrollView,如何在用户移动视图后检测滚动何时停止?或者,如何知道滚动停止时它将在什么位置(然后可以将其与onScrollChanged一起使用).
我继承了HorizontalScrollView.我想要的是让视图在用户与其交互时自然地表现出来,但是当它停止更新其某些子视图时-如果我在视图滚动时这样做,则在滚动过程中会导致小的滞后.
谢谢
解决方法:
我为HorizontalScrollView编写了一个自定义类,该类具有一个带有滚动停止方法的接口,该方法在滚动停止时给出scrollX的位置.
这是代码:
public class CustomMCSDHorizontalScroll extends HorizontalScrollView {
private Runnable scrollerTask;
private int initialPosition;
private int newCheck = 100;
public interface onScrollStopedListner{
void onScrollStoped();
}
private onScrollStopedListner onScrollStoped;
public CustomMCSDHorizontalScroll(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
scrollerTask = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
int newPosition = getScrollX();
if ( initialPosition - newPosition == 0 ) {
if ( onScrollStoped != null ) {
onScrollStoped.onScrollStoped();
}
} else {
initialPosition = getScrollX();
CustomMCSDHorizontalScroll.this.postDelayed(
scrollerTask, newCheck);
}
}
};
}
public void setOnScrollStopListner (CustomMCSDHorizontalScroll.
onScrollStopedListner listener) {
onScrollStoped = listener;
}
public void startScrollerTask(){
initialPosition = getScrollX();
CustomMCSDHorizontalScroll.this.postDelayed(scrollerTask, newCheck);
}
}
这是ontouch()方法:
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.mHorizontalScrollViewMain:
if (event.getAction() == KeyEvent.ACTION_UP) {
hsvUpperTab.startScrollerTask();
}
break;
case R.id.mHorizontalScrollViewSub:
if (event.getAction() == KeyEvent.ACTION_UP) {
hsvLowerTab.startScrollerTask();
}
default:
break;
}
return false;
}
这是onScrollStop()方法:
hsvLowerTab.setOnScrollStopListner(new onScrollStopedListner() {
@Override
public void onScrollStoped() {
// TODO Auto-generated method stub
getLowerTabScrolled(hsvLowerTab.getScrollX());
}
});
hsvUpperTab.setOnScrollStopListner(new onScrollStopedListner() {
@Override
public void onScrollStoped() {
// TODO Auto-generated method stub
getUpperTabScrolled(hsvUpperTab.getScrollX());
}
});
标签:onfling,horizontalscrollview,android 来源: https://codeday.me/bug/20191023/1912959.html