1. 程式人生 > >android-基礎編程-ScrollView

android-基礎編程-ScrollView

擁有 linear XML 部分 ffffff fff 底部 odin enc

滾動視圖(ScrollView)是指當擁有很多內容,屏幕顯示不完時,需要通過滾動來顯示完整的視圖。包括水平滾動視圖(HorizontalScrollView)和垂直滾動視圖(ScrollView)

基本操作:

setOnTouchListener的使用:判斷ScrollView何時滑動到底部
1、getScorollY()——滾動條滑動的距離
2、getMeasuredHeight()——內容的整體高度,包括隱藏部分
3、getHeight()——顯示高度。內容未布滿屏幕,2=3;內容大於屏幕,3=屏幕高度,2>3。

4、getChildAt(int i)——獲取ScorollView的第i個子控件

scrollTo(相對開始位置)和scrollBy(相對前一個位置):控制ScrollView視圖的位置

隱藏滾動條
1、標簽屬性:android:scrollbars="none"
2、代碼設置:
setHorizontalScrollBarEnabled(false);//隱藏橫向ScorollView
setVerticalScrollBarEnabled(false);//隱藏縱向ScorollView

註意:scrollview內容只有一個子view。

1.ScrollView使內容可以滾動。

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">
     <HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
android:layout_width="wrap_content" android:layout_height="match_parent" android:scrollbars="none" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="match_parent" android:textSize="22sp" android:text="shuiping scroll dfffffffffffffffffffffffffffffffffffffffffffffffffffffff"/> </HorizontalScrollView> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="向上" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="向下" /> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/scrollview" android:scrollbars="vertical" android:scrollbarTrackVertical="@color/colorAccent" android:scrollbarThumbVertical="@color/colorPrimary" android:scrollbarStyle="outsideInset"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="5000dp" android:layout_height="500dp" android:text="zcxzcxzcx"/> <TextView android:layout_width="5000dp" android:layout_height="500dp" android:text="zcxzcxzcx"/> </LinearLayout> </ScrollView> </LinearLayout>

2.代碼 響應函數如下:

public class ScrollViewActi extends Activity implements View.OnClickListener{
    private ScrollView scrollView;
    private Button button1;
    private Button button2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scrollview);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        scrollView = (ScrollView) findViewById(R.id.scrollview);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);

        //這裏是為textView賦值,內容在R.string.text中,測試時最好內容長一些,這裏不再貼出。
        //textView.setText(getResources().getString(R.string.text));
        scrollView.setOnTouchListener(new View.OnTouchListener() {

            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                switch (event.getAction()) {
                    //手指擡起
                    case MotionEvent.ACTION_UP:

                        break;

                    //手指落下
                    case MotionEvent.ACTION_DOWN:

                        break;

                    //手指滑動
                    case MotionEvent.ACTION_MOVE:
                        /**
                         * 1、getScorollY()——滾動條滑動的距離
                         * 2、getMeasuredHeight()——內容的整體高度,包括隱藏部分
                         * 3、getHeight()——顯示高度。內容未布滿屏幕,2=3;內容大於屏幕,3=屏幕高度,2>3。
                         */
                        //頂部狀態
                        if(scrollView.getScrollY()<=0){
                            Log.e(">>>>>>>>>>>>>>", "頂部");
                            Toast.makeText(getApplicationContext(), "頂部", Toast.LENGTH_SHORT).show();
                        }

                        //頂部狀態
                        //TextView的總高度<=一屏幕的高度+滾動條的滾動距離(getChildAt(0):第0個子控件)
                        if(scrollView.getChildAt(0).getMeasuredHeight()<= scrollView.getScrollY() + scrollView.getHeight()){
                            Log.e(">>>>>>>>>>>>>>", "底部");
                            Toast.makeText(getApplicationContext(), "底部", Toast.LENGTH_SHORT).show();

                            //在文本中追加內容
                            //textView.append("111111111111111111111");
                        }
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        //scrollTo:以滾動視圖起始位置開始計算的
        //scrollBy:相對前一次的位置,滾動相應的距離
        switch (v.getId()) {
            case R.id.button1:
          scrollView.scrollTo(0, -30);
            //    scrollView.scrollBy(0, -30);
                break;

            case R.id.button2:
          scrollView.scrollTo(0, -30);
           //     scrollView.scrollBy(0, 30);
                break;
        }
    }
}

3.效果

技術分享

android-基礎編程-ScrollView