首页 > 代码库 > ScrollView
ScrollView
在程序设计中,有时我们需要实现自动滚屏或根据选择直接滚动到指定的位置的功能。这里用到的主要组件就是滚动视图(ScrollView)。
----------
那么使用ScrollView如何实现布局自动滚动。
----------
首先我们要对控件对象进行声明;
[java] view plain copy
print?
- private LinearLayout linearLayout = null;
- private ScrollView scrollView = null;
private LinearLayout linearLayout = null; private ScrollView scrollView = null;
然后通过控件的ID得到代表控件的对象;
[java] view plain copy
print?
- linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
- scrollView = (ScrollView) findViewById(R.id.scrollView);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout); scrollView = (ScrollView) findViewById(R.id.scrollView);
定义一个Handler对象;
[java] view plain copy
print?
- private final Handler handler = new Handler();
private final Handler handler = new Handler();
实现滚动线程;
[java] view plain copy
print?
- 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);
- }
- }
- }
- };
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?
- btnSelf.setOnClickListener(new btnSelfListener());
btnSelf.setOnClickListener(new btnSelfListener());
实现自动滚动按钮监听器;
[java] view plain copy
print?
- /*
- * 自动滚动按钮监听器
- */
- 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);
- }
- }
- }
/* * 自动滚动按钮监听器 */ 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?
- // 跳转至开头
- scrollView.fullScroll(ScrollView.FOCUS_UP);
// 跳转至开头 scrollView.fullScroll(ScrollView.FOCUS_UP);
[java] view plain copy
print?
- // 跳转至结尾
- scrollView.fullScroll(ScrollView.FOCUS_DOWN);
// 跳转至结尾 scrollView.fullScroll(ScrollView.FOCUS_DOWN);
我们只要在跳转按钮上添加监听器;
[java] view plain copy
print?
- btnGoto.setOnClickListener(new btnGotoListener());
btnGoto.setOnClickListener(new btnGotoListener());
然后实现该监听器;
[java] view plain copy
print?
- /*
- * 跳转按钮监听器
- */
- 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();
- }
- }
/* * 跳转按钮监听器 */ 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。