가로 스크롤보기를 사용하고 있으며 특정 상황에서 첫 번째 항목을 숨기고 싶습니다. 이제 터치 리스너를 사용하여 사용자가 얼마나 스크롤했는지 확인하고이 첫 번째 항목 /보기가 표시되는지 여부를 확인한 다음 일부 계산을 기반으로 scrollTo ()를 수행합니다. 사용자가 튕기고 수평 스크롤 뷰 스크롤이 어느 정도 거리를 두는 경우가 처리되지 않는다는 것을 깨달을 때까지 모든 것이 잘되었습니다. 그래서 정확한 계산을 할 수 없습니다. 따라서 첫 번째 항목이 표시되는지 또는 터치 또는 플링의 결과로 가로 스크롤보기가 마침내 스크롤을 중지 한 후에 만 확인하는 것이 가장 좋다는 것을 깨달았습니다.
그러나 가로 스크롤 막대가 스크롤을 멈추고 여전히 멈출 때 알려주는 방법을 찾을 수 없습니다.
누군가가 이것을 알아낼 수 있도록 도와 주시겠습니까?
감사.
ScrollView를 확장하여 스크롤 리스너를 추가합니다. 그런 다음 해당 사용자 정의 뷰를 사용하고 리스너에서 이벤트를 수신 할 수 있습니다 (코드는 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);
}
}
그런 다음 다음과 같이 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>
스크롤 변경 사항을 알리려는 리스너를 설정하는 것을 잊지 마십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다