美文网首页
判断控件是不是在屏幕可见范围

判断控件是不是在屏幕可见范围

作者: 屠夫的羞涩 | 来源:发表于2016-10-18 16:38 被阅读0次

    <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>

    相关文章

      网友评论

          本文标题:判断控件是不是在屏幕可见范围

          本文链接:https://www.haomeiwen.com/subject/erdtuttx.html