一、绘制界面,两个按钮,一个scrollview中添加一个textview
scrollview.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Button
android:id="@+id/scrollview_up"
android:text="up"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/scrollview_down"
app:layout_constraintLeft_toRightOf="@+id/scrollview_up"
android:text="down"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ScrollView
android:id="@+id/scrollView_scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<TextView
android:id="@+id/scrollview_content"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="20dp"/>
</ScrollView>
</android.support.constraint.ConstraintLayout>
scrollview.java
package com.example.administrator.listview;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by Administrator on 2018/3/16 0016.
*/
public class scrollview extends AppCompatActivity implements View.OnClickListener{
private TextView scrollview_textview;
private ScrollView scrollView;
private Button scrollview_up;
private Button scrollview_down;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scrollview);
scrollview_textview=findViewById(R.id.scrollview_content);
scrollView=findViewById(R.id.scrollView_scrollview);
scrollview_textview.setText(R.string.scrollviewcontent);
scrollview_up=findViewById(R.id.scrollview_up);
scrollview_down=findViewById(R.id.scrollview_down);
scrollview_up.setOnClickListener(this);
scrollview_down.setOnClickListener(this);
scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
//手指何时抬起
case MotionEvent.ACTION_UP:{
Toast.makeText(scrollview.this,"手指何时抬起",Toast.LENGTH_SHORT).show();
System.out.println("手指何时抬起");
break;
}
//手指何时落下
case MotionEvent.ACTION_DOWN:{
System.out.println("手指何时落下");
break;
}
case MotionEvent.ACTION_MOVE:{
/*
* getscrollY()--滚动条滑动的距离
* getMeasuredHeight
* getHeight
* */
//顶部状态
if(scrollView.getScrollY()<=0){
System.out.println("onTouch: 滑动到顶部");
}
//底部状态
//当textview的高度<=屏幕的高度+滚动条的滚动距离
if (scrollView.getChildAt(0).getMeasuredHeight()
<=scrollView.getHeight()+scrollView.getScrollY()){
System.out.println("onTouch: 滑动到底部");
System.out.println("scrollView.getChildAt(0).getMeasuredHeight()"+scrollView.getChildAt(0).getMeasuredHeight()+" scrollView.getHeight()="+scrollView.getHeight()+" scrollView.getScrollY()"+scrollView.getScrollY());
//滑动到底部时,模拟刷新功能,向textview中添加一段文字
scrollview_textview.append(getResources().getString(R.string.scrollviewcontent));
}
break;
}
}
return false;
}
});
}
@Override
public void onClick(View view) {
/*
* scrollTo一步到位
* scrollBy可以持续点击
* */
switch (view.getId()){
case R.id.scrollview_up:{
scrollView.scrollBy(0,-30);
break;
}
case R.id.scrollview_down:{
scrollView.scrollBy(0,30);
break;
}
}
}
}