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. 
총결산
이상 은 이 글 의 모든 내용 입 니 다.본 고의 내용 이 여러분 의 학습 이나 업무 에 어느 정도 참고 학습 가 치 를 가지 기 를 바 랍 니 다.여러분 의 저희 에 대한 지지 에 감 사 드 립 니 다.더 많은 내용 을 알 고 싶다 면 아래 링크 를 보 세 요.

좋은 웹페이지 즐겨찾기