2

在 Android 中打开和关闭微调器时,我需要为箭头图标设置动画。打开微调器时我可以旋转箭头:我只是setOnTouchListenerSpinner.

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

如果可能的话,有人知道如何做到这一点吗?

提前非常感谢。

4

4 回答 4

1

我不知道为什么谷歌这么久都做不到,但是你可以这样解决问题:

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

例如:

    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。

于 2014-01-14T20:01:14.840 回答
0

你可以做这样的事情,

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

        });
于 2013-09-09T09:26:46.037 回答
0

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

这是自定义 Spinner 的完整源代码

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

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

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      
}
于 2021-09-13T03:50:27.277 回答
-1

试试这个方法

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // // called when spiner will closed

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // called when spiner will closed

            }
        });
于 2013-09-09T09:25:06.360 回答