2012-12-06 72 views
3

我想在向上和向下按鈕中顯示scrollView,如圖所示。上下滾動後,滾動條應該滾動,長按上下按鈕後,應該滾動到終點。 請告訴我如何實現這一點。將滾動視圖置於向上和向下按鈕

在此先感謝。

enter image description here

回答

0

對於打算頂部,你可以使用的方法

ScrollView.fullScroll(ScrollView.FOCUS_UP) 

滾動部分..基本上你可以使用函數

ScrollView.smoothScrollTo (int x, int y) 

您可以獲得當前X, Y位置通過getScrollX()getScrollY(),然後添加一些量進一步滾動。

+0

讓我知道,如果它的工作對你 – Umair

+0

但它不是一個完整的答案。 –

3

執行ScrollView .pageScroll()ScrollView.smoothScrollBy()點擊事件按鈕。

長按事件發生時,使用Runnable繼續調用ScrollView.scrollBy()。我爲你寫了一個演示。

public class TestAndroidActivity extends Activity implements OnTouchListener, 
     OnGestureListener { 
    private static final String TAG = "TestAndroid"; 
    private Handler mHandler = new Handler(); 
    private ScrollView mScrollView; 
    private Button mBtnUp; 
    private Button mBtnDown; 
    private View mCurBtn; 
    private GestureDetector mDetecor; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     mScrollView = (ScrollView) findViewById(R.id.scroll); 
     mBtnUp = (Button) findViewById(R.id.btn_up); 
     mBtnDown = (Button) findViewById(R.id.btn_down); 
     mBtnUp.setOnTouchListener(this); 
     mBtnDown.setOnTouchListener(this); 
     mDetecor = new GestureDetector(this, this); 
    } 

    private long mStartTime = 0; 
    private float mSpeed = 0.5f; 
    private Runnable mScrollRunnable = new Runnable() { 

     @Override 
     public void run() { 
      int dy = (int) ((SystemClock.uptimeMillis() - mStartTime) * mSpeed); 
      mStartTime = SystemClock.uptimeMillis(); 
      int direction = getCurrentBtnDirection(); 
      if (direction == View.FOCUS_UP) 
       dy *= -1; 
      mScrollView.scrollBy(0, dy); 
      mHandler.post(this); 
     } 
    }; 

    private int getCurrentBtnDirection() { 
     if (mCurBtn == mBtnUp) { 
      return View.FOCUS_UP; 
     } else if (mCurBtn == mBtnDown) { 
      return View.FOCUS_DOWN; 
     } else 
      return 0; 
    } 

    private void perfromPageScroll() { 
     int direction = getCurrentBtnDirection(); 
     mScrollView.pageScroll(direction); 
    } 

    private void stopContinuousScroll() { 
     mStartTime = 0; 
     mHandler.removeCallbacks(mScrollRunnable); 
    } 

    private void startContinuousScroll() { 
     mStartTime = SystemClock.uptimeMillis(); 
     mHandler.post(mScrollRunnable); 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     mCurBtn = v; 
     mDetecor.onTouchEvent(event); 
     if (event.getAction() == MotionEvent.ACTION_UP) { 
      stopContinuousScroll(); 
     } 
     return true; 
    } 

    @Override 
    public boolean onSingleTapUp(MotionEvent e) { 
     perfromPageScroll(); 
     return true; 
    } 

    @Override 
    public void onLongPress(MotionEvent e) { 
     startContinuousScroll(); 
    } 

    @Override 
    public boolean onDown(MotionEvent e) { 
     return false; 
    } 

    @Override 
    public void onShowPress(MotionEvent e) { 
    } 

    @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
      float distanceY) { 
     return false; 
    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
      float velocityY) { 
     return false; 
    } 

} 

如果您要檢測的滾動型的滾動狀態來啓用或禁用按鈕,你必須寫一個CustomView以滾動型延伸並覆蓋OnScrollChanged()方法。

編輯:添加布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

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

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="1\n\n\n\n\n\n2\n\n\n\n\n\n3\n\n\n\n\n4\n\n\n\n5\n\n" 
      android:textSize="32sp" /> 
    </ScrollView> 

    <Button 
     android:id="@+id/btn_up" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:text="up" /> 

    <Button 
     android:id="@+id/btn_down" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="down" /> 

</RelativeLayout> 

EDIT2:如果您正在使用一個ListView,你可以使用ListView.smoothScrollBy()更換ScrollView.scrollBy()。 ListView中沒有pageScroll()方法,可以自己寫,它不是很難。

EDIT3: pageScroll爲ListView控件

private void pageScroll(ListView l) { 
    l.smoothScrollBy(l.getHeight(), 300); 
} 
+1

我不敢相信這樣的詳細樣本不值得任何反饋。 – faylon

+0

我沒有試過這個,Bcz試圖實現我所問的所有事情。 –

+1

你怎麼知道我沒有實現你所有的要求,即使沒有嘗試演示。 – faylon