Android: Spinner dropdown in full screen

不羁岁月 提交于 2021-02-10 05:31:07

问题


I'm trying to open drop down spinner in full screen (immersive mode), but the issue is that when the drop down opens up it brings out translucent navigation bar at the bottom. The navigation bars hides when an option is selected, but remains visible as long as dropdown is visible. I was able to remove this behavior in dialog fragment since I have show(FragmentManager manager, String tag) method to override and add this

getDialog().getWindow().getDecorView().setSystemUiVisibility(getActivity()
.getWindow().getDecorView().getSystemUiVisibility());

// Make the dialogs window focusable
 again.getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

but no method similar to this available in spinner. I tried to put these method in performClick() using listener implementation in the parent but still no luck.

Any solution to this issue.


回答1:


For java users create this static class

import android.widget.ListPopupWindow;
import android.widget.PopupWindow;
import android.widget.Spinner;

public static void avoidSpinnerDropdownFocus(Spinner spinner) {
    try {
        Field listPopupField = Spinner.class.getDeclaredField("mPopup");
        listPopupField.setAccessible(true);
        Object listPopup = listPopupField.get(spinner);
        if (listPopup instanceof ListPopupWindow) {
            Field popupField = ListPopupWindow.class.getDeclaredField("mPopup");
            popupField.setAccessible(true);
            Object popup = popupField.get((ListPopupWindow) listPopup);
            if (popup instanceof PopupWindow) {
                ((PopupWindow) popup).setFocusable(false);
            }
        }
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

For Kotlin user use this extension function

import android.widget.ListPopupWindow
import android.widget.PopupWindow
import android.widget.Spinner

fun Spinner.avoidDropdownFocus() {
try {
    val listPopup = Spinner::class.java
            .getDeclaredField("mPopup")
            .apply { isAccessible = true }
            .get(this)
    if (listPopup is ListPopupWindow) {
        val popup = ListPopupWindow::class.java
                .getDeclaredField("mPopup")
                .apply { isAccessible = true }
                .get(listPopup)
        if (popup is PopupWindow) {
            popup.isFocusable = false
        }
    }
} catch (e: Exception) {
    e.printStackTrace()
}
}

You need to call that method from your spinner in your OnCreate method or when your Spinner is inflated or in any time before use it.

spinner.avoidSpinnerDropdownFocus()

Credits to kakajika GitHub user kakajika https://gist.github.com/kakajika/a236ba721a5c0ad3c1446e16a7423a63




回答2:


Try this code :

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);


来源:https://stackoverflow.com/questions/45817144/android-spinner-dropdown-in-full-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!