打开时在旋转器中旋转箭头

时间:2016-05-12 09:01:43

标签: android android-spinner

我在布局上有微调器。 Spinner有箭头。当微调器关闭箭头从右到左显示时。

enter image description here

当我打开微调器并显示下拉视图时,我想要旋转箭头。 enter image description here

根据状态微调器,有必要改变箭头的方向 我该怎么做?感谢

2 个答案:

答案 0 :(得分:0)

更改适配器中布局的背景。请参阅此代码:

public class adapterActivity extends ArrayAdapter<DTO_MCode> {

    public adapterActivity(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        // TODO Auto-generated constructor stub
    }

    // Note: for combobox
    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
        // TODO Auto-generated method stub
        return getViewChung(position, convertView, parent, true);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        return getViewChung(position, convertView, parent, false);
    }

    public View getViewChung(int position, View convertView,
            ViewGroup parent, boolean drop) {
        // TODO Auto-generated method stub
        View v = convertView;
        ViewWraper mwp;

        if (v == null) {
            LayoutInflater l = getLayoutInflater();
            v = l.inflate(R.layout.layoutcomboxe, null);
            mwp = new ViewWraper(v);
            v.setTag(mwp);
        } else {

            mwp = (ViewWraper) convertView.getTag();
        }
        LinearLayout layout = mwp.getLayoutrow();
        if (drop) {
            layout.setBackgroundResource(R.drawable.down);
        } else {
            layout.setBackgroundResource(R.drawable.up);
        }

        return v;
    }
}

答案 1 :(得分:0)

我知道为时已晚,但最终,我找到了一个无需扩展 Spinner 的解决方案。

只需创建一个如下所示的可绘制选择器并将其设置为微调器的背景,如 android:background="@drawable/bg_spinner"

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true">
        <layer-list>
            <item android:drawable="@drawable/ic_up_arrow" android:gravity="center_vertical|right" android:right="5dp" />
        </layer-list>
    </item>
    <item>
        <layer-list>
            <item android:drawable="@drawable/ic_down_arrow" android:gravity="center_vertical|right" android:right="5dp" />
        </layer-list>
    </item>
</selector>

还有一个 Kotlin 扩展函数作为...

fun AppCompatSpinner.setSpinnerFocusable() {
    isFocusableInTouchMode = true

    onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
        if (hasFocus) {
            if (windowToken != null) {
                performClick()

                viewTreeObserver?.addOnWindowFocusChangeListener(object :
                    ViewTreeObserver.OnWindowFocusChangeListener {
                    override fun onWindowFocusChanged(hasFocus: Boolean) {
                        if (hasFocus) {
                            clearFocus()
                            viewTreeObserver?.removeOnWindowFocusChangeListener(this)
                        }
                    }
                })
            }
        }
    }
}

并从您的活动/片段调用扩展方法,如 spUser.setSpinnerFocusable()

就是这样,无需扩展 Spinner 类或使用任何库。