ScrollView滑动

周正真
2023-12-01
package com.example.test;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.ScrollView;import android.widget.Toast;public class MainActivity extends Activity {ScrollView scrollView;int totalScrollSize = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState); //布局文件,用户自定义,当长度达到一定程度时,才会出现滑动现象setContentView(R.layout.activity_main);scrollView = (ScrollView) findViewById(R.id.scrollView);scrollView.setOnTouchListener(new MyTouchListener());}private class MyTouchListener implements OnTouchListener {@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubswitch (event.getAction()) {case MotionEvent.ACTION_UP:totalScrollSize += getViewSize();new MyThread().start();break;}return false;}} /* * 开启新线程,让其滚动到相应位置 */private class MyThread extends Thread {public void run() {scrollView.scrollTo(0, totalScrollSize);}}/* * 滚动大小 */public int getViewSize() {int scrollHeight = scrollView.getMeasuredHeight();return scrollHeight - 300; //如果滚动大小不合适,可依据实际情况,此处大小}}
 类似资料: