Android:RealViewSwitcher在垂直方向上无法滚动

4

我正在使用RealViewSwitcher来滑动视图,它很好用,我的问题类似于这个。我将尝试通过这张图片scenario来解释一下我的情况。

列表视图 <------> 详细视图 ------> 滑动视图(水平方向)。 详细视图仅显示部分内容,如果尝试滚动,则不会发生任何事情。

详细布局(没有垂直滚动)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

     <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColorHint="#FF0000"
        android:textSize="12dp"
        android:textStyle="bold"
        android:typeface="sans" />

         <ImageView
            android:id="@+id/imageView1"
            android:layout_width="150dip"
            android:layout_height="120dip"
            android:scaleType="centerCrop"
            android:src="@drawable/stub"/>   
   <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="20dip"
        android:textColor="#999999"
        android:textColorLink="#ff0000"
        android:textSize="20dip"
        android:textStyle="normal"
        android:typeface="sans" />
</LinearLayout>

使用RealViewSwitcher进行布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical">

    <it.application.ppn.lib.RealViewSwitcher
        android:id="@+id/horizontal_pager"
        android:layout_width="fill_parent"
        android:layout_height="0px"
        android:layout_weight="1">

    </it.application.ppn.lib.RealViewSwitcher>

</LinearLayout>
1个回答

1

我遇到了同样的问题,并通过拦截RealViewSwitcher中的触摸事件找到了解决方案。

编辑:之前发布的变体在较新的API上无法工作。代码已进行第三次修订。

将以下行添加到RealViewSwitcher实现中:

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {

    // prevent vertical scrolling when view switching in progress 
    if (!mScroller.isFinished())
        return true;

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mScrollLocked = false;
        mLastX = event.getX();
        mLastY = event.getY();
        mDeltaX = 0;
        mDeltaY = 0;
        onTouchEvent(event);
        break;

    case MotionEvent.ACTION_MOVE:
        final float x = event.getX();
        final float y = event.getY();
        mDeltaX += Math.abs(x - mLastX);
        mDeltaY += Math.abs(y - mLastY);
        mLastX = x;
        mLastY = y;
        if (mDeltaX > mDeltaY) {
            mScrollLocked = true;
            onTouchEvent(event);
        } else {
            snapToDestination();
        }
        break;
    }

    if (mScrollLocked)
        return true; // prevent furhter processing

    return super.onInterceptTouchEvent(event);
}

编辑2:还需要编辑onTouchEvent方法并将case MotionEvent.ACTION_DOWN:的代码替换为以下内容:

    case MotionEvent.ACTION_DOWN:

        /*
         * If being flinged and user touches, stop the fling. isFinished will be false if being flinged.
         */
        mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;

        if (mTouchState == TOUCH_STATE_SCROLLING) {
            mScroller.abortAnimation();
        }

        // Remember where the motion event started
        mLastMotionX = x;

        break;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接