简体   繁体   中英

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. or in other words, it can scroll few block everytime. it's similar to the menu on the android phone 2.3 any idea? if you can please show me the sample codes. thanks so much for the help

select scroll view from the advanced layout from 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>

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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