首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关闭Android微调器中的下拉列表

问关闭Android微调器中的下拉列表
EN

Stack Overflow用户
提问于 2013-09-09 17:17:45
回答 4查看 3K关注 0票数 2

在Android中打开和关闭微调器时,我需要设置箭头图标的动画。我可以在打开微调器时旋转箭头:我只是在Spinner上放了一个setOnTouchListener。

当下拉菜单关闭或隐藏时,问题就来了,因为我不知道如何在该操作上设置侦听器或类似的东西。

如果可能的话,谁有办法做到这一点?

在此之前非常感谢。

EN

回答 4

Stack Overflow用户

发布于 2014-01-15 04:01:15

我不知道为什么Google不能做这么久,但你可以这样解决这个问题:

您必须覆盖微调器的受保护方法"onDetachedFromWindow“,将其设置为公共方法,并通过单击CustomSpinnerAdapter中的项来调用它。

例如:

代码语言:javascript
复制
    public class CustomSpinner extends Spinner
    {
        Context context = null;

        public CustomSpinner(Context context)
        {
            super(context);
        }

        public CustomSpinner(Context context, int mode)
        {
            super(context, mode);
        }

        public CustomSpinner(Context context, AttributeSet attrs)
        {
            super(context, attrs);
        }

        public CustomSpinner(Context context, AttributeSet attrs, int defStyle)
        {
            super(context, attrs, defStyle);
        }

        public CustomSpinner(Context context, AttributeSet attrs, int defStyle, int mode)
        {
            super(context, attrs, defStyle, mode);
        }

        @Override public void onDetachedFromWindow()
        {
            super.onDetachedFromWindow();
        }
    }

我希望您知道如何创建SpinnerCustomAdapter并将此CustomSpinner插入到xml中。

票数 1
EN

Stack Overflow用户

发布于 2013-09-09 17:26:46

你可以这样做,

代码语言:javascript
复制
 boolean bflag=true;//declare it as public

     spinner.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                // TODO Auto-generated method stub

                 if(bflag==true)
                {
                    //first animation code goes here
                    Toast.makeText(getActivity(), "on", Toast.LENGTH_SHORT).show();
                    bflag=false;
                }

                else
                {
                    //second animation code goes here
                    Toast.makeText(getActivity(), "off", Toast.LENGTH_SHORT).show();
                    bflag=true;
                }


                return false;
            }

        });
票数 0
EN

Stack Overflow用户

发布于 2021-09-13 03:50:27

您需要使用Reflection并访问私有字段'mPopup',然后设置方法setOnDismissListener(),,无论用户单击空白区域还是选择新项目,都会在关闭弹出窗口时触发该方法。你可以在这里了解更多关于它的工作原理:https://stackoverflow.com/a/69156679/3753104

下面是定制微调器的完整源代码

代码语言:javascript
复制
open class CustomSpinner: androidx.appcompat.widget.AppCompatSpinner {

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    lateinit var listPopupWindow: ListPopupWindow
    lateinit var onPopUpClosedListener: (dropDownMenu: DropDownMenu) -> Unit
    lateinit var onPopUpOpenedListener: (dropDownMenu: DropDownMenu) -> Unit

    init {

        try {

            // get the listPopupWindow
            val listPopupWindowField = androidx.appcompat.widget.AppCompatSpinner::class.java.getDeclaredField("mPopup")
            listPopupWindowField.isAccessible = true
            listPopupWindow = listPopupWindowField.get(this) as ListPopupWindow
            listPopupWindow.isModal = false

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun performClick(): Boolean {
        val returnValue = super.performClick()

        // indicate that the pop-up was opened
        if (::onPopUpOpenedListener.isInitialized) {
            onPopUpOpenedListener.invoke(this)
        }

        try {

            // get the popupWindow
            val popupWindowField = ListPopupWindow::class.java.getDeclaredField("mPopup")
            popupWindowField.isAccessible = true
            val popupWindow = popupWindowField.get(listPopupWindow) as PopupWindow

            // get the original onDismissListener
            val onDismissListenerField = PopupWindow::class.java.getDeclaredField("mOnDismissListener")
            onDismissListenerField.isAccessible = true
            val onDismissListener = onDismissListenerField.get(popupWindow) as PopupWindow.OnDismissListener

            // now override the original OnDismissListener
            listPopupWindow.setOnDismissListener {

                // indicate that the pop-up was closed
                if (::onPopUpClosedListener.isInitialized) {
                    onPopUpClosedListener.invoke(this)
                }

                // now we need to call the original listener that will remove the global OnLayoutListener
                onDismissListener.onDismiss()
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }

        return returnValue
    }
}

然后,只需将侦听器附加到您的自定义微调器

代码语言:javascript
复制
val customSpinner = findViewById<CustomSpinner>(R.id.mySpinner)
customSpinner.onPopUpClosedListener = {
    // called when the pop-up is closed
}

customSpinner.onPopUpOpenedListener = { 
    // called when the pop-up is opened      
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18695061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档