<pre>
public class MainActivity extends AppCompatActivity {
private NestedScrollView mScrollView;
private TextView mTextView;
private int mScreenHeight;
private int mScreenWeight;
private Rect mRect;
private boolean isReady;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Point point = new Point();
windowManager.getDefaultDisplay().getSize(point);
mScreenHeight = point.y;
mScreenWeight = point.x;
initViews();
}
private void initViews() {
mScrollView= (NestedScrollView) findViewById(R.id.nested_scroll);
mTextView= (TextView) findViewById(R.id.text4);
mRect = new Rect(0,0,mScreenWeight,mScreenHeight );
mScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if(mTextView.getLocalVisibleRect(mRect)){
if (isReady) {
isReady=false;
System.out.println("控件在屏幕显示范围内");
}
}else{
if (!isReady){
isReady=true;
System.out.println("控件在屏幕显示范围外");
}
}
}
});
}}
</pre>
网友评论