最近項目中需要實現一個GridView顯示6*5=30項,并鋪滿整個界面,界面中還有自定義ActionBar等其他控件,所以需要獲取剩下屏幕的高度。通過百度得知View有一個監聽函數,親測使用有效,特此記錄,方便日后查閱。
gv_test.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { //給GridView設置Adapter,在adapter的getView中獲取GridView的高度,在這個回調之前獲取的高度都是0 //處理完后remove掉,至于為什么,后面有解釋 gv_test.getViewTreeObserver() .removeOnGlobalLayoutListener(this); } });
通過源碼追溯進去,找到ViewTreeObserver這個類,里面有很多interface,都是用來追蹤View的各種狀態變化的。
找到OnGlobalLayoutListener
/** * Interface definition for a callback to be invoked when the global layout state * or the visibility of views within the view tree changes. */ public interface OnGlobalLayoutListener { /** * Callback method to be invoked when the global layout state or the visibility of views * within the view tree changes */ public void onGlobalLayout(); }
注釋的大概意思就是這個回調在布局狀態和可見狀態發生變化時回調,所以準確的說,這個不是監聽View的加載完成,而是監聽布局變化的。
我們來測試一下。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.myapplication.MainActivity"> <Button android:onClick="test" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="test"/> <TextView android:id="@+id/tv_test" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="測試"/></LinearLayout>
public class MainActivity extends AppCompatActivity { TextView tv_test; private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_test = (TextView)findViewById(R.id.tv_test); //app切換到后臺,再點開會調用一次,屏幕關閉運行程序會調用兩次 tv_test.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Log.e(TAG, "onGlobalLayout: "); } }); } public void test(View v){ //改變可見性,調用一次// tv_test.setVisibility(View.GONE); //改變文字布局,沒有效果// tv_test.setGravity(Gravity.CENTER); //修改控件大小,調用一次// LinearLayout.LayoutParams para = (LinearLayout.LayoutParams) tv_test.getLayoutParams();// para.height = 200;// para.weight = 100;// tv_test.setLayoutParams(para); //修改layoutgravity,這個是在LayoutParams中,調用一次 LinearLayout.LayoutParams para = (LinearLayout.LayoutParams) tv_test.getLayoutParams(); para.gravity = Gravity.CENTER_HORIZONTAL; tv_test.setLayoutParams(para); }}
運行程序,得到從android monitor中可以看到,啟動后調用了三次onGlobalLayout,很奇怪,為什么是三次?后來有一次屏幕鎖了,發現調用了兩次。經過測試,app退到后臺后重新進入會調用一次,屏幕鎖屏后重新打開會調用兩次(小米兩次,努比亞1次),其中一次猜測是控件的可見性改變了。
通過按鍵的測試,分別修改控件的可見性和布局,都會調用一次,修改控件內部布局,不會調用,同時修改布局和可見性,只調用一次。
到此三次之謎依舊沒有解決,不過,可以肯定的是,這個會重復
調用多次,使用的時候需要注意。解決的辦法就是第一次回調后,就把回調remove掉,如:gv_test.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
如有錯誤,敬請雅正。
以上這篇android監聽View加載完成的示例講解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答