首页 > 代码库 > ScrollView

ScrollView

在程序设计中,有时我们需要实现自动滚屏或根据选择直接滚动到指定的位置的功能。这里用到的主要组件就是滚动视图(ScrollView)。

----------

那么使用ScrollView如何实现布局自动滚动。

----------

首先我们要对控件对象进行声明;

 

[java] view plain copy
 
print?
  1. private LinearLayout linearLayout = null;  
  2. private ScrollView scrollView = null;  
技术分享
private LinearLayout linearLayout = null;
private ScrollView scrollView = null;

 



然后通过控件的ID得到代表控件的对象;

 

[java] view plain copy
 
print?
  1. linearLayout = (LinearLayout) findViewById(R.id.linearLayout);  
  2. scrollView = (ScrollView) findViewById(R.id.scrollView);  
技术分享
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
scrollView = (ScrollView) findViewById(R.id.scrollView);



定义一个Handler对象;

[java] view plain copy
 
print?
  1. private final Handler handler = new Handler();  
技术分享
private final Handler handler = new Handler();

 

 

实现滚动线程;

[java] view plain copy
 
print?
  1. private Runnable ScrollRunnable = new Runnable() {  
  2.     @Override  
  3.     public void run() {  
  4.         int off = linearLayout.getMeasuredHeight() - scrollView.getHeight();  
  5.         if (off > 0) {  
  6.             scrollView.scrollBy(0, 30);  
  7.             if (scrollView.getScrollY() == off) {  
  8.                 Thread.currentThread().interrupt();  
  9.             } else {  
  10.                 handler.postDelayed(this, 1000);  
  11.             }  
  12.         }  
  13.     }  
  14. };  
技术分享
	private Runnable ScrollRunnable = new Runnable() {
		@Override
		public void run() {
			int off = linearLayout.getMeasuredHeight() - scrollView.getHeight();
			if (off > 0) {
				scrollView.scrollBy(0, 30);
				if (scrollView.getScrollY() == off) {
					Thread.currentThread().interrupt();
				} else {
					handler.postDelayed(this, 1000);
				}
			}
		}
	};

 

 

在自动滚动按钮上添加监听器;

[java] view plain copy
 
print?
  1. btnSelf.setOnClickListener(new btnSelfListener());  
技术分享
btnSelf.setOnClickListener(new btnSelfListener());

 

实现自动滚动按钮监听器;

 

[java] view plain copy
 
print?
  1. /* 
  2.  * 自动滚动按钮监听器 
  3.  */  
  4. class btnSelfListener implements OnClickListener {  
  5.   
  6.     @Override  
  7.     public void onClick(View v) {  
  8.         // 当前按钮文字是自动滚动  
  9.         if (btnSelfStr == R.string.selfMotion) {  
  10.             // 将按钮文字设为“停止滚动”  
  11.             btnSelf.setText(R.string.stopSelfMotion);  
  12.             btnSelfStr = R.string.stopSelfMotion;  
  13.             // 开始自动滚动  
  14.             handler.post(ScrollRunnable);  
  15.         } else {  
  16.             // 将按钮文字设为“自动滚动”  
  17.             btnSelf.setText(R.string.selfMotion);  
  18.             btnSelfStr = R.string.selfMotion;  
  19.             // 停止自动滚动  
  20.             handler.removeCallbacks(ScrollRunnable);  
  21.         }  
  22.     }  
  23. }  
技术分享
	/*
	 * 自动滚动按钮监听器
	 */
	class btnSelfListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// 当前按钮文字是自动滚动
			if (btnSelfStr == R.string.selfMotion) {
				// 将按钮文字设为“停止滚动”
				btnSelf.setText(R.string.stopSelfMotion);
				btnSelfStr = R.string.stopSelfMotion;
				// 开始自动滚动
				handler.post(ScrollRunnable);
			} else {
				// 将按钮文字设为“自动滚动”
				btnSelf.setText(R.string.selfMotion);
				btnSelfStr = R.string.selfMotion;
				// 停止自动滚动
				handler.removeCallbacks(ScrollRunnable);
			}
		}
	}

 



这样我们就实现了布局的自动滚动。
----------

那么如何实现根据选择直接滚动到指定的位置。

 

[java] view plain copy
 
print?
  1. // 跳转至开头  
  2. scrollView.fullScroll(ScrollView.FOCUS_UP);  
技术分享
// 跳转至开头
scrollView.fullScroll(ScrollView.FOCUS_UP);
[java] view plain copy
 
print?
  1. // 跳转至结尾  
  2. scrollView.fullScroll(ScrollView.FOCUS_DOWN);  
技术分享
// 跳转至结尾
scrollView.fullScroll(ScrollView.FOCUS_DOWN);

 

我们只要在跳转按钮上添加监听器;

 

[java] view plain copy
 
print?
  1. btnGoto.setOnClickListener(new btnGotoListener());  
技术分享
btnGoto.setOnClickListener(new btnGotoListener());

 

 

然后实现该监听器;

[java] view plain copy
 
print?
  1. /* 
  2.  * 跳转按钮监听器 
  3.  */  
  4. class btnGotoListener implements OnClickListener {  
  5.     int choice = -1;  
  6.     @Override  
  7.     public void onClick(View v) {  
  8.         // 弹出跳转设置对话框  
  9.         new AlertDialog.Builder(MainActivity.this)  
  10.                 .setTitle("跳转设置")  
  11.                 .setSingleChoiceItems(new String[] { "开头", "结尾" }, -1,  
  12.                         new DialogInterface.OnClickListener() {  
  13.                             @Override  
  14.                             public void onClick(DialogInterface dialog,  
  15.                                     int which) {  
  16.                                 switch (which) {  
  17.                                 case 0:  
  18.                                     choice = 0;  
  19.                                     break;  
  20.                                 case 1:  
  21.                                     choice = 1;  
  22.                                     break;  
  23.                                 }  
  24.                             }  
  25.                         })  
  26.                 .setPositiveButton("跳转",  
  27.                         new DialogInterface.OnClickListener() {  
  28.   
  29.                             @Override  
  30.                             public void onClick(DialogInterface dialog,  
  31.                                     int which) {  
  32.                                 switch (choice) {  
  33.                                 case 0:  
  34.                                     // 跳转至开头  
  35.                                     scrollView  
  36.                                             .fullScroll(ScrollView.FOCUS_UP);  
  37.                                     break;  
  38.                                 case 1:  
  39.                                     // 跳转至结尾  
  40.                                     scrollView  
  41.                                             .fullScroll(ScrollView.FOCUS_DOWN);  
  42.                                     break;  
  43.                                 }  
  44.                             }  
  45.                         }).setNegativeButton("返回", null).show();  
  46.     }  
  47. }  
技术分享
	/*
	 * 跳转按钮监听器
	 */
	class btnGotoListener implements OnClickListener {
		int choice = -1;
		@Override
		public void onClick(View v) {
			// 弹出跳转设置对话框
			new AlertDialog.Builder(MainActivity.this)
					.setTitle("跳转设置")
					.setSingleChoiceItems(new String[] { "开头", "结尾" }, -1,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									switch (which) {
									case 0:
										choice = 0;
										break;
									case 1:
										choice = 1;
										break;
									}
								}
							})
					.setPositiveButton("跳转",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									switch (choice) {
									case 0:
										// 跳转至开头
										scrollView
												.fullScroll(ScrollView.FOCUS_UP);
										break;
									case 1:
										// 跳转至结尾
										scrollView
												.fullScroll(ScrollView.FOCUS_DOWN);
										break;
									}
								}
							}).setNegativeButton("返回", null).show();
		}
	}


这样我们就实现了布局的自动滚动。
----------

 



利用ScrollView实现布局自动滚动

源码下载:http://download.csdn.net/detail/dkbnull/9298787

ScrollView