有四种方法可以更新(主要思路在于计算view的高度)(Y轴)
(android:visibility="invisible" 注意要在布局文件当中留给空间位置,不然无法计算高度,在fragment当中使用需要注意)
第一步(在Scrollview里面实现)
写一个自定义的Scrollview里面//需要自己手写一个回调来实现里面的onScrollChanged的方法用来计算高度
public class Custom extends ScrollView { private SetOnScrollViewchangedCallBack setOnScrollViewchangedCallBack;
public void setSetOnScrollViewchangedCallBack(SetOnScrollViewchangedCallBack setOnScrollViewchangedCallBack) { this.setOnScrollViewchangedCallBack = setOnScrollViewchangedCallBack; }
public Custom(Context context) { this(context,null); }
public Custom(Context context, AttributeSet attrs) { this(context, attrs,0); }
public Custom(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }
@Override protected void onScrollChanged(int x, int y, int oldx, int oldy) { if (setOnScrollViewchangedCallBack !=null){ setOnScrollViewchangedCallBack.onScrollChanged(x, y, oldx, oldy); } super.onScrollChanged(x, y, oldx, oldy); } public interface SetOnScrollViewchangedCallBack{ void onScrollChanged(int x, int y, int oldx, int oldy);
} }
scrollview.setSetOnScrollViewchangedCallBack(new Custom.SetOnScrollViewchangedCallBack() { @Override public void onScrollChanged(int x, int y, int oldx, int oldy) { if (y > cur_Y + btn_sign_up + 10) { actiontext001.setVisibility(View.VISIBLE); } else { actiontext001.setVisibility(View.GONE); } }
这里是判定view 的显示和隐藏
|
|
第一种方式(onWindowFocusChanged)(这个方法有个缺陷就是按下home键要从新计算高度)
public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus){ int [] pos = new int[2]; actiontext001.getLocationInWindow(pos); cur_Y=pos[1]; btn_sign_up=actiontext001.getHeight(); }
}
|
|
第二种mearsure (高度只是计算一次方法可行)
line1fragment1.measure(0, 0); cur_Y = line1fragment1.getMeasuredHeight();
测量VIew高度
int[] pos = new int[2]; actiontext001.getLocationInWindow(pos); btn_sign_up = actiontext001.getHeight();
|
第三种getViewTreeObserver()(这个房要一直运行,一直在计算高度 不建议使用)(需要取绝对值)
scrollview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int [] pos = new int[2]; actiontext001.getLocationInWindow(pos); cur_Y=pos[1]; btn_sign_up=actiontext001.getHeight();
} });
|
|
第三种getViewTreeObserver()(建议使用)
scrollview.postDelayed(new Runnable() { @Override public void run() { int [] pos = new int[2]; actiontext001.getLocationInWindow(pos); cur_Y=pos[1]; btn_sign_up=actiontext001.getHeight(); Log.e("zbd",cur_Y+":======"+btn_sign_up); } },200);
|
|
|
|
|