Sun*_*nny 1 android horizontalscrollview onscrolllistener
我正在使用水平滚动视图,我想在某些情况下隐藏第一个项目.现在,我使用的触摸监听器,看看有多少用户滚动,并看到这第一个项目/视图可见或不可见,然后根据一些计算会做scrollTo().一切都很好,直到我意识到这不会照顾用户只是晃动和水平滚动视图滚动一段距离的情况.所以,我无法进行正确的计算.因此,我已经意识到最好的方法是在水平滚动视图最终停止滚动时检查第一个项目是否可见,这是触摸或投掷的结果.
但我找不到一种方法告诉我何时水平滚动条停止滚动并且仍然是.
有人可以帮我解决这个问题吗?
谢谢.
扩展ScrollView以添加滚动侦听器.然后,您可以使用该自定义View并在侦听器中接收事件(代码使用scrollView,但将其转换为HorizontalScrollView应该非常简单).
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;
public class ScrollViewWithListener extends ScrollView{
private boolean mCurrentlyTouching;
private boolean mCurrentlyFling;
public interface ScrollViewListener {
public void onScrollChanged(ScrollViewWithListener scrollView, int x, int y, int oldx, int oldy);
public void onEndScroll();
}
private ScrollViewListener scrollViewListener = null;
public ScrollViewWithListener(Context context) {
super(context);
}
public ScrollViewWithListener(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScrollViewWithListener(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
@Override
public void fling(int velocityY) {
super.fling(velocityY);
mCurrentlyFling = true;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
}
if (Math.abs(t - oldt) < 2 || t >= getMeasuredHeight() || t == 0) {
if(!mCurrentlyTouching){
if (scrollViewListener != null) {
Log.d("SCROLL WITH LISTENER", "-- OnEndScroll");
scrollViewListener.onEndScroll();
}
}
mCurrentlyFling = false;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mCurrentlyTouching = true;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mCurrentlyTouching = false;
if(!mCurrentlyFling){
if (scrollViewListener != null) {
Log.d("SCROLL WITH LISTENER", "-- OnEndScroll");
scrollViewListener.onEndScroll();
}
}
break;
default:
break;
}
return super.onTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mCurrentlyTouching = true;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mCurrentlyTouching = false;
if(!mCurrentlyFling){
if (scrollViewListener != null) {
Log.d("SCROLL WITH LISTENER", "-- OnEndScroll");
scrollViewListener.onEndScroll();
}
}
break;
default:
break;
}
return super.onInterceptTouchEvent(ev);
}
}
Run Code Online (Sandbox Code Playgroud)
然后你在你的xml中使用它,如下所示:
<com.example.ScrollViewWithListener
android:id="@+id/scrollWithListener"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TEST" />
</com.example.ScrollViewWithListener>
Run Code Online (Sandbox Code Playgroud)
不要忘记使用滚动更改设置要通知的侦听器.
| 归档时间: |
|
| 查看次数: |
2627 次 |
| 最近记录: |