public class MainActivity extends Activity {
private TextView tv;
private ScrollView scroll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView) findViewById(R.id.textView1);
tv.setText(getResources().getString(R.string.text));
scroll=(ScrollView) findViewById(R.id.scrollView1);
scroll.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
/*
* getScrollY():滚动条滑动的距离
* getMeasuredHeight():文本长度
* getHeight();屏幕的高度
*/
switch (arg1.getAction()) {
case MotionEvent.ACTION_MOVE:
{
//顶部状态
if(scroll.getScaleY()<=0){
Toast.makeText(MainActivity.this, "滚动条在顶部", Toast.LENGTH_SHORT).show();
}
//底部状态
//文本的长度<=屏幕的高度+滚动条滚动的距离
//TextView是父容器(屏幕)的第一个孩子
if(scroll.getChildAt(0).getMeasuredHeight()<=scroll.getScaleY()+scroll.getHeight()){
Toast.makeText(MainActivity.this, "滑动到了底部", Toast.LENGTH_SHORT).show();
//加载新文本内容
tv.append(getResources().getString(R.string.text1));
}
break;
}
}
return false;
}
});
}
}
ziom
相关分类