Android:全屏下拉菜单 [英] Android: Spinner dropdown in full screen

查看:265
本文介绍了Android:全屏下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以全屏(沉浸模式)打开下拉微调器,但是问题是,当下拉菜单打开时,它会在底部弹出半透明的导航栏.选中选项时,导航栏将隐藏,但只要下拉菜单可见,导航栏便保持可见.我可以在对话框片段中删除此行为,因为我有 show(FragmentManager manager,String tag)方法可以覆盖并添加此

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);

,但没有与此类似的方法可以在微调器中使用.我试图使用父级中的侦听器实现将这些方法放在 performClick()中,但还是没有运气.

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.

推荐答案

为Java用户创建此静态类

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();
    }
}

供Kotlin用户使用此扩展功能

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()
}
}

您需要从 Spinner 中的 OnCreate 方法中,或者在 Spinner 膨胀时或使用之前的任何时间,调用该方法

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()

kakajika GitHub用户kakajika的积分 https://gist.github.com/kakajika/a236ba721a5c0ad3c1446e16a7423a63

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

这篇关于Android:全屏下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆