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);
}
}
- <?xml version="1.0" encoding="utf-8"?>
- <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/ScrollView" android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:scrollbars="vertical">
- <LinearLayout android:id="@+id/LinearLayout"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextView android:id="@+id/TestView" android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="TestView0" />
- <Button android:id="@+id/Button" android:text="Button0" android:layout_width="fill_parent"
- android:layout_height="wrap_content"></Button>
- </LinearLayout>
- </ScrollView>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:scrollbars="vertical">
<LinearLayout android:id="@+id/LinearLayout"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/TestView" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="TestView0" />
<Button android:id="@+id/Button" android:text="Button0" android:layout_width="fill_parent"
android:layout_height="wrap_content"></Button>
</LinearLayout>
</ScrollView>
- package com.Aina.Android;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.view.KeyEvent;
- import android.view.View;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.ScrollView;
- import android.widget.TextView;
-
- public class Test_ScrollView extends Activity {
-
- private LinearLayout mLayout;
- private ScrollView sView;
- private final Handler mHandler = new Handler();
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- mLayout = (LinearLayout) this.findViewById(R.id.LinearLayout);
-
- sView = (ScrollView) this.findViewById(R.id.ScrollView);
- Button mBtn = (Button) this.findViewById(R.id.Button);
- mBtn.setOnClickListener(mClickListener);
- }
-
- public boolean onKeyDown(int keyCode, KeyEvent event){
- Button b = (Button) this.getCurrentFocus();
- int count = mLayout.getChildCount();
- Button bm = (Button) mLayout.getChildAt(count-1);
-
- if(keyCode==KeyEvent.KEYCODE_DPAD_UP && b.getId()==R.id.Button){
- bm.requestFocus();
- return true;
- }else if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN && b.getId()==bm.getId()){
- this.findViewById(R.id.Button).requestFocus();
- return true;
- }
- return false;
- }
-
- private Button.OnClickListener mClickListener = new Button.OnClickListener() {
-
- private int index = 1;
-
- @Override
- public void onClick(View v) {
- TextView tView = new TextView(Test_ScrollView.this);
- tView.setText("TextView" + index);
-
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.FILL_PARENT,
- LinearLayout.LayoutParams.WRAP_CONTENT);
- mLayout.addView(tView, params);
- Button button = new Button(Test_ScrollView.this);
- button.setText("Button" + index);
- button.setId(index++);
- mLayout.addView(button, params);
- mHandler.post(mScrollToButton);
- }
-
- };
- private Runnable mScrollToButton = new Runnable() {
-
- @Override
- public void run() {
- int off = mLayout.getMeasuredHeight() - sView.getHeight();
- if (off > 0) {
- sView.scrollTo(0, off);
- }
- }
-
- }; }