android studio listview怎么滚动

 我来答
mcxho
2017-06-30 · TA获得超过1836个赞
知道大有可为答主
回答量:1665
采纳率:80%
帮助的人:1070万
展开全部
按下按钮会触发ListView滚动或停止。

实现该功能并不难,下面给出主要代码MainActivity.java

?1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 package cn.guet.levide;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity implements OnClickListener {
private Button btn_up, btn_down, btn_stop; // 三个按钮
private ListView listview;
private Adapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findBy();
init();
}

private void init() {
btn_up.setOnClickListener(this);
btn_down.setOnClickListener(this);
btn_stop.setOnClickListener(this);

adapter = new Adapter(this);
listview.setAdapter(adapter);
}

private void findBy() {
btn_up = (Button) findViewById(R.id.btn_scroll_up);
btn_down = (Button) findViewById(R.id.btn_scroll_down);
btn_stop = (Button) findViewById(R.id.btn_scroll_stop);

listview = (ListView) findViewById(R.id.listview);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_scroll_down:
listScrollDown();
break;
case R.id.btn_scroll_up:
listScrollUp();
break;
case R.id.btn_scroll_stop:
listScrollOff();
break;
}
}

Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
handler.removeCallbacks(run_scroll_down);
handler.removeCallbacks(run_scroll_up);
}
};

/**
* 向上滚动
*/
public void listScrollUp() {
listScrollOff();
handler.postDelayed(run_scroll_up, 0);
}

/**
* 向下滚动
*/
public void listScrollDown() {
listScrollOff();
handler.postDelayed(run_scroll_down, 0);
}

/**
* 停止滚动
*/
public void listScrollOff() {
handler.removeCallbacks(run_scroll_down);
handler.removeCallbacks(run_scroll_up);
}

Runnable run_scroll_up = new Runnable() {
@Override
public void run() {
/**
* public void smoothScrollBy (int distance, int duration)
*
* Added in API level 8 Smoothly scroll by distance pixels over duration milliseconds.
*
* Parameters
* distance Distance to scroll in pixels.
* duration Duration of the scroll animation in milliseconds.
*/
listview.smoothScrollBy(1, 10);
handler.postDelayed(run_scroll_up, 10);
}
};
Runnable run_scroll_down = new Runnable() {
@Override
public void run() {
listview.smoothScrollBy(-1, 10);
handler.postDelayed(run_scroll_down, 10);
}
};
}

实现ListView位置变动的是smoothScrollBy方法。

?1
2
3
4
5
6 public void smoothScrollBy (int distance, int duration)
Smoothly scroll by distance pixels over duration milliseconds.

Parameters
distance Distance to scroll in pixels.
duration Duration of the scroll animation in milliseconds.

工程源码:

Android listview 代码控制上下移动
寒云清语
2017-06-30 · TA获得超过189个赞
知道小有建树答主
回答量:177
采纳率:0%
帮助的人:95.6万
展开全部
ListView是自带滚动的。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式