zoukankan      html  css  js  c++  java
  • Android_scrollview

    xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.android_scollview.MainActivity" >
        <Button 
            android:id="@+id/up"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="up"
        />
        <Button 
            android:id="@+id/down"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="down"
         />
    
        <ScrollView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/scroll1"
            android:scrollbars="none">
            <!-- 
                ScrollView 的种类:
                    ScrollView:垂直滚动视图
                    HorizontalScrollView:水平滚动视图
                隐藏ScrollView
                    (1)标签属性:scrollbars 设置不显示纵向滚动条 
                    (2)代码设置:
                        setVerticalScrollBarEnabled(false);隐藏纵向ScrollView
                        setHorizontalSrollBarEnabled(false);隐藏横向ScrollView
                    
                    
                    -->
            <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/text" />
        </ScrollView>
    </LinearLayout>

    main.java

    package com.example.android_scollview;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.View.OnTouchListener;
    import android.widget.Button;
    import android.widget.ScrollView;
    import android.widget.TextView;
    /**
     * 应用案例:判断ScrollView何时滑动到底部
     * 利用setOnTouchListener
     *
     */
    public class MainActivity extends Activity implements OnClickListener{
    
        private TextView tv;
        private ScrollView scroll;
        private Button up;
        private Button down;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            up = (Button) findViewById(R.id.up);
            down = (Button) findViewById(R.id.down);
            
            up.setOnClickListener(this);
            down.setOnClickListener(this);
            tv = (TextView) findViewById(R.id.text);
            scroll = (ScrollView) findViewById(R.id.scroll1);
            scroll.setOnTouchListener(new OnTouchListener() {
                
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    switch(event.getAction()){
                    case MotionEvent.ACTION_MOVE:{
                        /**
                         * (1)getScrollY()--滚动条滑动的距离
                         * (2)getMeasuredHeight--显示的高度加上隐藏的高度
                         * (3)getHeight
                         */
                        //顶部状态
                        if(scroll.getScaleY()<=0){
                            Log.i("Main", "顶部状态");
                        }
                        //底部状态
                        //TextView的总高度<=一屏幕的高度+滚动条滚动的距离
                        if(scroll.getChildAt(0).getMeasuredHeight()<=scroll.getHeight()+scroll.getY()){
                            Log.i("Main", "底部状态");
                        }
                        break;
                    }
                    
                    }
                    
                    return false;
                }
            });
        }
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch(v.getId()){
            /**
             * scroll的两种滑动方式:
             *     scrollTo:以滚动视图起始位置开始计算
             *     scrollBy:相对前一次的位置,去滚动对应的距离
             */
                case R.id.up:{
                    scroll.scrollBy(0, -30);
                    break;
                }
                case R.id.down:{
                    scroll.scrollBy(0, 30);
                    break;
                }
            }
        }
        }
  • 相关阅读:
    内存与缓存认识
    翻转字符串里的单词
    c++ STD Gems07
    C++ STD Gems06
    C++ STD Gems05
    Silverlight RIA Services基础专题
    超漂亮的WPF界面框架(Modern UI for WPF)
    实验三——阶乘
    实验二
    实验一 Java环境的搭建&Eclipse的安装
  • 原文地址:https://www.cnblogs.com/fangg/p/5723190.html
Copyright © 2011-2022 走看看