从ViewDragLayout中学到的一些事实

123 阅读1分钟
  1. View的onLayout方法画出的界面,与这个View在Xml中给定的LayoutParams的关系是,onLayout只关心显示什么,而XML中写的高度宽度事实地规定了View的大小和位置。相当于在View上挡了一个遮罩。

  2. 如何获取一个超出高度大于屏幕范围的ScrollView的高度? getHeight和getMessuredHeight都不行。高度可以这么计算: int height = mScrollView.getChildAt(0).getBottom(); 因为ScrollView只能有一个子布局,所以获取它的Bottom相对于Top的值就是ScrollView真正的高度。

  3. 通常用这样的Math方法来规定一个数值的范围:

		@Override
		public int clampViewPositionVertical(View child, int top, int dy) {
			int topBound = Math.min(mHeaderHeight, getHeight() - mScroll.getChildAt(0).getBottom());
			int bottomBound = mMaxFinalTopHeight + SDKDisplayUtil.dip2px(50);
			return Math.max(topBound, Math.min(top, bottomBound));
		}
  1. getHeight获取的是onLayout的给定的。
  2. LinearLayout即便让它wrap content,包含了超越了整个屏幕的高度,getBottom()的时候仍然只能取到屏幕底部。