如何在每个滚动中滚动Android ScrollView xml中的一定数量的值

时间:2011-12-15 04:07:43

标签: android

我尝试建立scrollView,每次用户轻弹时都可以滚动一定距离。换句话说,它可以每次滚动几个块。它类似于Android手机2.3上的菜单 任何的想法?如果你能告诉我示例代码。非常感谢你的帮助

2 个答案:

答案 0 :(得分:0)

从eclip高级布局中选择滚动视图

<ScrollView
        android:id="@+id/scrol`<ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </LinearLayout>
    </ScrollView>`lView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </LinearLayout>
</ScrollView>

答案 1 :(得分:0)

用于扩展简单的手势监听器。我的代码大致粘贴在这里。这只是为了解一下。我在创建水平滚动视图时已经这样做了。

setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (mGestureDetector.onTouchEvent(event)) {
            return true;
        } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
            int scrollX = getScrollX();
            int featureWidth = v.getMeasuredWidth();
            mActiveFeature = ((scrollX + (featureWidth/2))/featureWidth);
            int scrollTo = mActiveFeature*featureWidth;
            smoothScrollTo(scrollTo, 0);
            return true;
        } else {
              return false;
        }
    }
}

简单的GestureListener:

class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try {
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                int featureWidth = getMeasuredWidth();
                mActiveFeature = (mActiveFeature < (mItems.size() - 1))? mActiveFeature + 1:mItems.size() -1;
                smoothScrollTo(mActiveFeature*featureWidth, 0);
                return true;
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                int featureWidth = getMeasuredWidth();
                mActiveFeature = (mActiveFeature > 0)? mActiveFeature - 1:0;
                smoothScrollTo(mActiveFeature*featureWidth, 0);
                return true;
            }
        } catch (Exception e) {
            Log.e("Fling", "There was an error processing the Fling event:" + e.getMessage());
        }

        return false;
    }
}