最近項(xiàng)目中需要實(shí)現(xiàn)一個(gè)GridView顯示6*5=30項(xiàng),并鋪滿整個(gè)界面,界面中還有自定義ActionBar等其他控件,所以需要獲取剩下屏幕的高度。通過(guò)百度得知View有一個(gè)監(jiān)聽函數(shù),親測(cè)使用有效,特此記錄,方便日后查閱。
gv_test.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { //給GridView設(shè)置Adapter,在adapter的getView中獲取GridView的高度,在這個(gè)回調(diào)之前獲取的高度都是0 //處理完后remove掉,至于為什么,后面有解釋 gv_test.getViewTreeObserver() .removeOnGlobalLayoutListener(this); } });通過(guò)源碼追溯進(jìn)去,找到ViewTreeObserver這個(gè)類,里面有很多interface,都是用來(lái)追蹤View的各種狀態(tài)變化的。
找到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(); }注釋的大概意思就是這個(gè)回調(diào)在布局狀態(tài)和可見狀態(tài)發(fā)生變化時(shí)回調(diào),所以準(zhǔn)確的說(shuō),這個(gè)不是監(jiān)聽View的加載完成,而是監(jiān)聽布局變化的。
我們來(lái)測(cè)試一下。
<?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="測(cè)試"/></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切換到后臺(tái),再點(diǎn)開會(huì)調(diào)用一次,屏幕關(guān)閉運(yùn)行程序會(huì)調(diào)用兩次 tv_test.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Log.e(TAG, "onGlobalLayout: "); } }); } public void test(View v){ //改變可見性,調(diào)用一次// tv_test.setVisibility(View.GONE); //改變文字布局,沒(méi)有效果// tv_test.setGravity(Gravity.CENTER); //修改控件大小,調(diào)用一次// LinearLayout.LayoutParams para = (LinearLayout.LayoutParams) tv_test.getLayoutParams();// para.height = 200;// para.weight = 100;// tv_test.setLayoutParams(para); //修改layoutgravity,這個(gè)是在LayoutParams中,調(diào)用一次 LinearLayout.LayoutParams para = (LinearLayout.LayoutParams) tv_test.getLayoutParams(); para.gravity = Gravity.CENTER_HORIZONTAL; tv_test.setLayoutParams(para); }}運(yùn)行程序,得到從android monitor中可以看到,啟動(dòng)后調(diào)用了三次onGlobalLayout,很奇怪,為什么是三次?后來(lái)有一次屏幕鎖了,發(fā)現(xiàn)調(diào)用了兩次。經(jīng)過(guò)測(cè)試,app退到后臺(tái)后重新進(jìn)入會(huì)調(diào)用一次,屏幕鎖屏后重新打開會(huì)調(diào)用兩次(小米兩次,努比亞1次),其中一次猜測(cè)是控件的可見性改變了。
通過(guò)按鍵的測(cè)試,分別修改控件的可見性和布局,都會(huì)調(diào)用一次,修改控件內(nèi)部布局,不會(huì)調(diào)用,同時(shí)修改布局和可見性,只調(diào)用一次。
到此三次之謎依舊沒(méi)有解決,不過(guò),可以肯定的是,這個(gè)會(huì)重復(fù)
調(diào)用多次,使用的時(shí)候需要注意。解決的辦法就是第一次回調(diào)后,就把回調(diào)remove掉,如:gv_test.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
如有錯(cuò)誤,敬請(qǐng)雅正。
以上這篇android監(jiān)聽View加載完成的示例講解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選