ScrollView within ViewFlipper does not work althou

2019-09-15 09:53发布

问题:

In this post: Android: ScrollView in flipper

It was suggested to set an onTouchnListener to the ScrollView which is part of a child of a ViewFlipper.

I did that and also used the same logic:

    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;
}

}

When I scroll in the child of the ViewFlipper I get the following in the 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


However no scrolling takes place although it should be possible since the content is bigger than the screen.

What am I doing wrong?

回答1:

I think you're problem is similar to one found in this post:Scrollview doesn't swipe when it's too short to scroll

take a look at the proposed solution, the one where the author extended the ScrollView view class. i believe this will fix your pro



回答2:

Found a solution at this link -> Fragment using ScrollView inside RelativeLayout > ontouch doesn't work

Add the following code to your view inside scrollview

android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"