Android通过代码控制ListView上下滚动的方法
2019/7/7 21:49:57
本文主要是介绍Android通过代码控制ListView上下滚动的方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文将介绍一种通过代码控制ListView上下滚动的方法。
先上图:
按下按钮会触发ListView滚动或停止。
实现该功能并不难,下面给出主要代码MainActivity.java
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方法。
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上下滚动的方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-18android.permission.read_media_video
- 2024-01-18android_getaddrinfo failed eai_nodata
- 2024-01-18androidmo
- 2024-01-15Android下三种离屏渲染技术
- 2024-01-09Android 蓝牙使用
- 2024-01-06Android对接华为AI - 文本识别
- 2023-11-15代码安全之代码混淆及加固(Android)
- 2023-11-10简述Android语音播报TTS
- 2023-11-06Android WiFi工具类
- 2023-07-22Android开发未来的出路