Android ScrollView自动滚屏或者直接滚屏到底部

直接上源码


import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends Activity {

	LinearLayout mainLayout;
	Button button1;
	Button button2;
	ScrollView sv;
	TextView tv;
	boolean isScroll=false;
	
	Timer timer;
	AutoScrollTask autoTask;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//mainLayout
		mainLayout=new LinearLayout(this);
		mainLayout.setOrientation(LinearLayout.VERTICAL);
		mainLayout.setLayoutParams(new LinearLayout.LayoutParams(-1,-1));
		//button1
		button1=new Button(this);
		button1.setLayoutParams(new LinearLayout.LayoutParams(-1,-2));
		button1.setText("步进滚动阅读模式");
		button1.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				isScroll=true;
				if(timer==null){
					sv.scrollTo(0, 0);//重新定位到顶端
					timer=new Timer();
					autoTask=new AutoScrollTask();
					timer.schedule(autoTask, 1000, 500);
				}
			}
		});
		//button2
		button2=new Button(this);
		button2.setLayoutParams(new LinearLayout.LayoutParams(-1,-2));
		button2.setText("一步到位到最底部");
		button2.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				isScroll=false;
				//
				try{
					Thread.sleep(600);
				}catch(Exception ex){};
				//
				int h1=tv.getMeasuredHeight();
				sv.scrollTo(0, h1);//直接显示文本底端
			}
		});
		//tv
		tv=new TextView(this);
		tv.setBackgroundColor(Color.WHITE);
		tv.setTextColor(Color.BLACK);
		for(int i=0;i<100;i++){
			tv.append("这是第"+String.valueOf(i)+"行\n");
		}
		//
		sv=new ScrollView(this);
		sv.addView(tv);
		//
		mainLayout.addView(button1);
		mainLayout.addView(button2);
		mainLayout.addView(sv);
		setContentView(mainLayout);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	class AutoScrollTask extends TimerTask{
		public void run(){
			if(isScroll){
				Message msg=new Message();
				msg.what=1;
				handler.sendMessage(msg);
			}
		}
	}

	Handler handler=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			if(msg.what==1){
				//如果没有到底端,Y偏移量增加10
				if(sv.getScrollY()<tv.getMeasuredHeight()-10){
					sv.scrollBy(0, 10);
				}
				else {
					//直接到底端
					sv.scrollTo(0, tv.getMeasuredHeight());
				}
			}
			super.handleMessage(msg);
		}
	};
}

猜你喜欢

转载自blog.csdn.net/mrlixirong/article/details/53872144