尽管使用onTouchEvent,但ViewFlipper中的ScrollView不起作用

时间:2011-08-07 20:02:13

标签: android scrollview viewflipper ontouchevent

在这篇文章中: Android: ScrollView in flipper

建议将onTouchnListener设置为ScrollView,它是ViewFlipper子项的一部分。

我做到了并且也使用了相同的逻辑:

    public class MainActivity extends Activity implements OnGestureListener {

// ...

@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...

    this.gestureDetector = new GestureDetector(this);

    // ..

           createViews();
}


private void createViews() {

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            displayView = inflater.inflate(R.layout.viewflippercontent, null);

    ScrollView scrollView = (ScrollView) displayView
            .findViewById(R.id.scrollview);

    scrollView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
             if (gestureDetector.onTouchEvent(event)) {
                Log.d("ScrollViewOnTouchListener", "true");                 
                return true;
            } else {
                Log.d("ScrollViewOnTouchListener", "false");
                return false;                       
            }
        }
    });

    // ..
}


@Override
public boolean onTouchEvent(MotionEvent me) {
    Log.d("onTouchEvent", "onTouchEvent");
    return gestureDetector.onTouchEvent(me);
}

public boolean onDown(MotionEvent e) {
    return true;
}

public void onLongPress(MotionEvent e) {
}

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    Log.d("onScroll", "onScroll");
    return false;
}

public void onShowPress(MotionEvent e) {
}

public boolean onSingleTapUp(MotionEvent e) {
    return true;
}

}

当我滚动ViewFlipper的子项时,我在LogCat中得到以下内容:


08-07 21:57:14.581:DEBUG / ScrollViewOnTouchListener(4586):true

08-07 21:57:14.591:DEBUG / ScrollViewOnTouchListener(4586):false

08-07 21:57:14.622:DEBUG / ScrollViewOnTouchListener(4586):false

08-07 21:57:14.646:DEBUG / onScroll(4586):onScroll

08-07 21:57:14.646:DEBUG / ScrollViewOnTouchListener(4586):false

08-07 21:57:14.671:DEBUG / onScroll(4586):onScroll

08-07 21:57:14.671:DEBUG / ScrollViewOnTouchListener(4586):false

08-07 21:57:14.704:DEBUG / onScroll(4586):onScroll

08-07 21:57:14.704:DEBUG / ScrollViewOnTouchListener(4586):false

08-07 21:57:14.731:DEBUG / onScroll(4586):onScroll

08-07 21:57:14.731:DEBUG / ScrollViewOnTouchListener(4586):false

08-07 21:57:14.761:DEBUG / onScroll(4586):onScroll

08-07 21:57:14.761:DEBUG / ScrollViewOnTouchListener(4586):false

08-07 21:57:14.791:DEBUG / onScroll(4586):onScroll

08-07 21:57:14.791:DEBUG / ScrollViewOnTouchListener(4586):false

08-07 21:57:14.791:DEBUG / ScrollViewOnTouchListener(4586):true


然而,由于内容大于屏幕,因此不应该进行滚动。

我做错了什么?

2 个答案:

答案 0 :(得分:0)

我认为你的问题类似于这篇文章中提到的问题:Scrollview doesn't swipe when it's too short to scroll

看看提出的解决方案,即作者扩展ScrollView视图类的解决方案。我相信这会解决你的问题

答案 1 :(得分:0)

在此链接找到解决方案 - > Fragment using ScrollView inside RelativeLayout > ontouch doesn't work

将以下代码添加到scrollview

中的视图中
android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"