ScrollView
在程序设计中,有时我们需要实现自动滚屏或根据选择直接滚动到指定的位置的功能。这里用到的主要组件就是滚动视图(ScrollView)。
----------
那么使用ScrollView如何实现布局自动滚动。
----------
首先我们要对控件对象进行声明;
private LinearLayout linearLayout = null; private ScrollView scrollView = null;
然后通过控件的ID得到代表控件的对象;
- 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对象;
private final Handler handler = new Handler();
实现滚动线程;
- 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);
}
}
}
};
在自动滚动按钮上添加监听器;
btnSelf.setOnClickListener(new btnSelfListener());
实现自动滚动按钮监听器;
- /*
- * 自动滚动按钮监听器
- */
- 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);
}
}
}
这样我们就实现了布局的自动滚动。
----------
那么如何实现根据选择直接滚动到指定的位置。
// 跳转至开头 scrollView.fullScroll(ScrollView.FOCUS_UP);
// 跳转至结尾 scrollView.fullScroll(ScrollView.FOCUS_DOWN);
我们只要在跳转按钮上添加监听器;
btnGoto.setOnClickListener(new btnGotoListener());
然后实现该监听器;
- /*
- * 跳转按钮监听器
- */
- 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实现布局自动滚动



浙公网安备 33010602011771号