SlowScrollView用法大全

来源:互联网 发布:三国杀陈琳淘宝 编辑:程序博客网 时间:2024/05/01 02:07

 public class SlowScrollView extends ScrollView {
        public SlowScrollView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
        public SlowScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        public SlowScrollView(Context context) {
            super(context);
        }
        /**
         * 滑动事件
         
*/
        @Override
        public void fling(int velocityY) {
            super.fling(velocityY / 4);
        }
    }

代码说明:

    重点在"velocityY / 4",这里意思是滑动速度减慢到原来四分之一的速度,这里大家可以根据自己的需求加快或减慢滑动速度。

ScrollView(滚动视图)是实现滚动的一个控件,只需要将需要滚动的控件添加到ScrollView中即可!ScrollView可以在代码中进行设置,也可以在XML布局文件中进行设置!

1.Activity文件如下:

import android.app.Activity;
import android.os.Bundle;
import android.widget.ScrollView;
import android.widget.TextView;

public class ScrollViewActivity extends Activity {


 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScrollView scrollView=new ScrollView(this);
        String s="中华儿女显神威,华夏大地起风云!男儿立志出乡关,誓不成名死不休!";
        String msg="";
       
        TextView textView=new TextView(this);
        for(int t=0;t<20;t++){
         msg+=s;

}
        textView.setText(msg);
        textView.setTextSize(23);
        scrollView.addView(textView);
        setContentView(scrollView);
       
    }
}

  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.      android:id="@+id/ScrollView" android:layout_width="fill_parent"  
  4.      android:layout_height="wrap_content" android:scrollbars="vertical">   
  5.      <LinearLayout android:id="@+id/LinearLayout"  
  6.          android:orientation="vertical" android:layout_width="fill_parent"  
  7.          android:layout_height="wrap_content">   
  8.          <TextView android:id="@+id/TestView" android:layout_width="fill_parent"  
  9.              android:layout_height="wrap_content" android:text="TestView0" />   
  10.          <Button android:id="@+id/Button" android:text="Button0" android:layout_width="fill_parent"  
  11.              android:layout_height="wrap_content"></Button>   
  12.      </LinearLayout>   
  13. </ScrollView>  
  1. package com.Aina.Android;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.os.Handler;   
  6. import android.view.KeyEvent;   
  7. import android.view.View;   
  8. import android.widget.Button;   
  9. import android.widget.LinearLayout;   
  10. import android.widget.ScrollView;   
  11. import android.widget.TextView;   
  12.   
  13. publicclass Test_ScrollViewextends Activity {   
  14.     /** Called when the activity is first created. */  
  15.     private LinearLayout mLayout;   
  16.     private ScrollView sView;   
  17.     privatefinal Handler mHandler =new Handler();   
  18.   
  19.     @Override  
  20.     publicvoid onCreate(Bundle savedInstanceState) {   
  21.         super.onCreate(savedInstanceState);   
  22.          setContentView(R.layout.main);   
  23.         // 创建一个线性布局  
  24.          mLayout = (LinearLayout) this.findViewById(R.id.LinearLayout);   
  25.         // 创建一个ScrollView对象  
  26.          sView = (ScrollView) this.findViewById(R.id.ScrollView);   
  27.          Button mBtn = (Button) this.findViewById(R.id.Button);   
  28.          mBtn.setOnClickListener(mClickListener);// 添加点击事件监听  
  29.      }   
  30.   
  31.     publicboolean onKeyDown(int keyCode, KeyEvent event){   
  32.          Button b = (Button) this.getCurrentFocus();   
  33.         int count = mLayout.getChildCount();   
  34.          Button bm = (Button) mLayout.getChildAt(count-1);   
  35.   
  36.         if(keyCode==KeyEvent.KEYCODE_DPAD_UP && b.getId()==R.id.Button){   
  37.              bm.requestFocus();   
  38.             returntrue;   
  39.          }elseif(keyCode==KeyEvent.KEYCODE_DPAD_DOWN && b.getId()==bm.getId()){   
  40.             this.findViewById(R.id.Button).requestFocus();   
  41.             returntrue;   
  42.          }   
  43.         returnfalse;   
  44.      }   
  45.     // Button事件监听,当点击第一个按钮时增加一个button和一个textview  
  46.     private Button.OnClickListener mClickListener =new Button.OnClickListener() {   
  47.   
  48.         privateint index =1;   
  49.   
  50.         @Override  
  51.         publicvoid onClick(View v) {   
  52.              TextView tView = new TextView(Test_ScrollView.this);//定义一个TextView  
  53.              tView.setText("TextView" + index);//设置TextView的文本信息  
  54.             //设置线性布局的属性  
  55.              LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(   
  56.                      LinearLayout.LayoutParams.FILL_PARENT,   
  57.                      LinearLayout.LayoutParams.WRAP_CONTENT);   
  58.              mLayout.addView(tView, params);//添加一个TextView控件  
  59.              Button button = new Button(Test_ScrollView.this);//定义一个Button  
  60.              button.setText("Button" + index);//设置Button的文本信息  
  61.              button.setId(index++);   
  62.              mLayout.addView(button, params);//添加一个Button控件  
  63.              mHandler.post(mScrollToButton);//传递一个消息进行滚动  
  64.          }   
  65.   
  66.      };   
  67.     private Runnable mScrollToButton =new Runnable() {   
  68.   
  69.         @Override  
  70.         publicvoid run() {   
  71.             int off = mLayout.getMeasuredHeight() - sView.getHeight();   
  72.             if (off >0) {   
  73.                  sView.scrollTo(0, off);//改变滚动条的位置  
  74.              }   
  75.          }   
  76.   
  77.      };    }  
原创粉丝点击