照着视频教程做的,onTouch能执行,case语句部分也执行到了,两个if语句都没有执行到,为什么?
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- HroizontalScrollView --> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/scroll" android:scrollbars="none"> <!-- scrollbars="none" 表示隐藏滚动条 --> <TextView android:textSize="30dp" android:id="@+id/content" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout>
package com.example.administrator.first; import android.app.Activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.ScrollView; import android.widget.TextView; public class MainActivity extends Activity { TextView tv; ScrollView scroll; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.m1); tv = (TextView) findViewById(R.id.content); tv.setText(getResources().getString(R.string.content)); scroll = (ScrollView) findViewById(R.id.scroll); scroll.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch(motionEvent.getAction()) { case MotionEvent.ACTION_MOVE: /* 1、getScrollY()--滚动条滑动的距离 2、getMeasuredHeight() 3、getHeight() */ if(scroll.getScaleY()<=0)//顶部状态 { Log.i("Main", "滑动到顶部"); } //底部状态 //TextView的总高度 <= 一屏的高度+ 滚动条的滚动距离 if(scroll.getChildAt(0).getMeasuredHeight()<= scroll.getHeight() + scroll.getScaleY()) { Log.i("Main", "滑动到底部"); Log.i("Main", "(scroll.getChildAt(0).getMeasuredHeight()="+scroll.getChildAt(0).getMeasuredHeight()+ "scroll.getHeight()= "+scroll.getHeight()+ "scroll.getScaleY()=" + scroll.getScaleY()); } break; } return false; } }); } }
解决方案
20
Scale是缩放的意思,本人觉得你可能调用错了方法