无需明显更改布局即可调用onGlobalLayout()

时间:2018-10-14 22:40:06

标签: android autocomplete googleplacesautocomplete android-viewtreeobserver

我有一个运行良好的软键盘侦听器(一项活动除外)。在这里。

public static void setKeyboardListener(final Activity activity, final ImageView iv_header){
    final View view =activity.findViewById(android.R.id.content);
    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout(){

            //get cached screen height
            String s_heightRoot =StringCacheFactory.stringCache.get("screen_height");
            int heightRoot = (s_heightRoot ==null) ? 0 : FormatFactory.tryParseStringToInt(s_heightRoot);

            //get height when onGlobalLayout is called
            Rect measureNewRect = new Rect();
            view.getWindowVisibleDisplayFrame(measureNewRect);
            int heightNew =measureNewRect.bottom;

            if ((heightNew < heightRoot) || (heightRoot ==0)){
                //screen has shrunk, keyboard open, perform awesome code
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }else{
                //full screen, do nothing
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        }
    });
}

对于所有活动,侦听器等待屏幕更改,然后调用onGlobalLayout()。但是一个活动onGlobalLayout()被立即调用(无需更改屏幕)。此活动的独特之处在于它隐含以下内容

public class PlacesActivity extends AppCompatActivity implements
    GoogleApiClient.OnConnectionFailedListener,
    GoogleApiClient.ConnectionCallbacks {
    //lotsa code
}

PlacesActivity有一个AutoCompleteTextView,用户可以在其中借助Google Places API来选择世界各地的城市。这些回调会干扰我的onGlobalLayoutListener吗?

在没有明显改变布局的情况下可以调用onGLobalLayout()的其他情况吗?

AutoCompleteTextView会触发onGlobalLayout()吗?

0 个答案:

没有答案