編輯:關於Android編程
本文將介紹一種通過代碼控制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); } }; }
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 代碼控制上下移動
main.xml代碼如下: .java代碼如下: package org.lxh.demo; import android.
2月下旬辭職了,去海南度假到現在,領略了一把三亞風情也算任性和然而這樣任性帶來的後果就是。。不行了我必須吐槽一句。。沒畢業的找工作就這麼難嘛!投了57家一家面試機會都沒有
1.強制使用http替換https鏈接 Tools》選擇Options,勾選上”Use Download Cache”和”Force
Android 網絡直播彈幕