ScrollView

/**
    HorizontalScrollView -- 水平SCrollView
    scrollbars -- 滾動條
    */
    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        >
        <TextView
            android:id="@+id/scroll_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
    </ScrollView>




public class ScrollActivity extends Activity implements View.OnClickListener{

    private TextView tv;
    private ScrollView scrollView;
    private Button  upbtn;
    private Button  downbtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scroll);

        upbtn = (Button)findViewById(R.id.upbtn);
        downbtn = (Button)findViewById(R.id.downbtn);
        upbtn.setOnClickListener(this);
        downbtn.setOnClickListener(this);

        tv = (TextView)findViewById(R.id.scroll_text);
        //或者value資源
        tv.setText(getResources().getString(R.string.scroll_text));

        scrollView = (ScrollView)findViewById(R.id.scrollView);
        //代碼禁止顯示滾動條
        scrollView.setHorizontalScrollBarEnabled(false);
        scrollView.setVerticalScrollBarEnabled(false);
        //滾動監聽
        scrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_UP:{//手指向上
                        break;
                    }
                    case MotionEvent.ACTION_DOWN:{//手指向下
                        break;
                    }
                    case MotionEvent.ACTION_MOVE:{//手指滑動
                        /**
                         * getScrollY() -- 滾到條滾動的距離
                         * getMeasuredHeight -- contentOffset
                         * getHeight()  -- scroll的高度
                         * */
                        String str = "";
                        if (scrollView.getScrollY() <= 0){
                            str = "滑動到頂部";
                        }
                        //contentOffset <= scroll高度+滾動條距離
                        if (scrollView.getChildAt(0).getMeasuredHeight()<= scrollView.getHeight()+scrollView.getScrollY()){
                            str = "滑動到底部";
                        }
                        Toast.makeText(ScrollActivity.this,str,Toast.LENGTH_SHORT).show();
                        //textview添加內容
                        tv.append(getResources().getString(R.string.scroll_text));
                        break;

                    }
                }
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        //scrollTo:一直以滾動條的初始位置爲初始位置,一旦設置就是跳到滾動位置
        //scrollBy:以上一次的位置爲初始位置
        switch (v.getId()){
            case R.id.upbtn:
            {
                scrollView.scrollTo(0,-30);
                break;
            }
            case R.id.downbtn:
            {
                scrollView.scrollBy(0,30);
                break;
            }
        }
    }
}
相關文章
相關標籤/搜索