在Android中打开和关闭微调器时,我需要设置箭头图标的动画。我可以在打开微调器时旋转箭头:我只是在Spinner上放了一个setOnTouchListener。
当下拉菜单关闭或隐藏时,问题就来了,因为我不知道如何在该操作上设置侦听器或类似的东西。
如果可能的话,谁有办法做到这一点?
在此之前非常感谢。
发布于 2014-01-15 04:01:15
我不知道为什么Google不能做这么久,但你可以这样解决这个问题:
您必须覆盖微调器的受保护方法"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中。
发布于 2013-09-09 17:26:46
你可以这样做,
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;
}
});发布于 2021-09-13 03:50:27
您需要使用Reflection并访问私有字段'mPopup',然后设置方法setOnDismissListener(),,无论用户单击空白区域还是选择新项目,都会在关闭弹出窗口时触发该方法。你可以在这里了解更多关于它的工作原理:https://stackoverflow.com/a/69156679/3753104
下面是定制微调器的完整源代码
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
}https://stackoverflow.com/questions/18695061
复制相似问题