布局
<ScrollView
android:id="@+id/sv"
android:layout_width="368dp"
android:layout_height="300dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp" android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:layout_width="match_parent" android:layout_height="100dp"
android:background="#F00"/>
<TextView android:layout_width="match_parent" android:layout_height="100dp"
android:background="#0F0"/>
<TextView android:layout_width="match_parent" android:layout_height="100dp"
android:background="#00F"/>
<TextView android:layout_width="match_parent" android:layout_height="100dp"
android:background="#F00"/>
<TextView android:layout_width="match_parent" android:layout_height="100dp"
android:background="#0F0"/>
<TextView android:layout_width="match_parent" android:layout_height="100dp"
android:background="#00F"/>
<TextView android:layout_width="match_parent" android:layout_height="100dp"
android:background="#F00"/>
<TextView android:layout_width="match_parent" android:layout_height="100dp"
android:background="#0F0"/>
<TextView android:layout_width="match_parent" android:layout_height="100dp"
android:background="#00F"/>
<TextView android:layout_width="match_parent" android:layout_height="100dp"
android:background="#F00"/>
<TextView android:layout_width="match_parent" android:layout_height="100dp"
android:background="#0F0"/>
<TextView android:layout_width="match_parent" android:layout_height="100dp"
android:background="#00F"/>
</LinearLayout>
</ScrollView>
sv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
if (sv.getScrollY()<=0){
Log.d("meee",getClass()+":\n"+"滑动到顶部");
}
if(sv.getChildAt(0).getMeasuredHeight()<= sv.getScrollY() + sv.getHeight()){
Log.d("meee",getClass()+":\n"+"滑动到底部");
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
break;
}
return false;
}
});
滚动到底部或头部
button_left.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean b = sv.fullScroll(ScrollView.FOCUS_DOWN);
Log.d("meee",getClass()+":\n"+b);
}
});
button_right.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean b = sv.fullScroll(ScrollView.FOCUS_UP);
Log.d("meee",getClass()+":\n"+b);
}
});
代码控制滚动
sv.scrollTo(0,x);
sv.smoothScrollTo(0,x);
滑动灵敏度控制
public class MyScrollView extends ScrollView {
public MyScrollView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyScrollView (Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollView (Context context) {
super(context);
}
@Override
public void fling(int velocityY) {
super.fling(velocityY / 4);
}
}
使用valueAnimator实现自动滚动效果
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, sv.getChildAt(0).getMeasuredHeight())
.setDuration(10*1000);
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
Integer animatedValue = (Integer) valueAnimator.getAnimatedValue();
sv.smoothScrollTo(0,animatedValue);
}
});
valueAnimator.addListener(new Animator.AnimatorListener() {
long startTime;
@Override
public void onAnimationStart(Animator animator) {
startTime=System.currentTimeMillis();
}
@Override
public void onAnimationEnd(Animator animator) {
long processTime = System.currentTimeMillis() - startTime;
Toast.makeText(getApplicationContext(),"动画完毕processTime:"+processTime,Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
valueAnimator.start();