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);
}
};
}
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.
총결산이상 은 이 글 의 모든 내용 입 니 다.본 고의 내용 이 여러분 의 학습 이나 업무 에 어느 정도 참고 학습 가 치 를 가지 기 를 바 랍 니 다.여러분 의 저희 에 대한 지지 에 감 사 드 립 니 다.더 많은 내용 을 알 고 싶다 면 아래 링크 를 보 세 요.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.