简体   繁体   English

如何在每次滚动时在android ScrollView xml中滚动一定数量的值

[英]How to scroll certain amount of value in android ScrollView xml for every scroll

i try to build up the scrollView which can scroll certain amount of distance everytime the user flick it. 我尝试建立一个scrollView,每次用户轻拂它时,它都可以滚动一定距离。 or in other words, it can scroll few block everytime. 或换句话说,它每次可以滚动几个块。 it's similar to the menu on the android phone 2.3 any idea? 它类似于android手机2.3上的菜单,有什么想法吗? if you can please show me the sample codes. 如果可以的话,请告诉我示例代码。 thanks so much for the help 非常感谢你的帮助

select scroll view from the advanced layout from eclip 从Eclipse的高级布局中选择滚动视图

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

Use to extends simple gesture listener.My code is roughly pasted here.This is only to give some idea about that.I have done this when creating horizontal scroll view. 用于扩展简单的手势侦听器,我的代码大致粘贴在这里,这只是为了给我一些想法,我在创建水平滚动视图时已经做到了。

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

Simple GestureListener: 简单的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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM