2011-12-15 58 views

回答

0

用来扩展简单的手势listener.My代码大致在这里。这贴只是为了给有关的一些想法。创建水平滚动视图时,我已经完成了这项工作

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