微调定制适配器上选择不消失 [英] Spinner with custom adapter doesn't disappear on selection

查看:106
本文介绍了微调定制适配器上选择不消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是微调与调用自定义适配器类 AlgorithmAdapter 并原则一切都工作正常,意义微调弹出窗口和所有含有意见适当充气。不过,我无法找出是我如何告诉微调做出选择的时候。当然,我知道 setSelection(),但这并不处置弹出并返回焦点活动。

I'm using a Spinner with a custom adapter class called AlgorithmAdapter and in principle everything's working fine, meaning the spinner popup appears and all containing views are properly inflated. However, what I'm not able to find out is how I "tell" the spinner when the selection is made. Of course I know about setSelection(), but that doesn't dispose the popup and return focus to the Activity.

因此​​,这里的一些相关的code的:

So here's some of the relevant code:

在我的活动,我创建了我的适配器是这样的:

In my activity, I create my adapter like this:

SpinnerAdapter spinnerAdapter = new AlgorithmAdapter(
    this,
    algorithmSpinner,
    R.layout.algo_spinner_item_detail,
    res.getStringArray(R.array.anaglyph_algorithm_titles),
    res.getStringArray(R.array.anaglyph_algorithm_descriptions),
    res.obtainTypedArray(R.array.anaglyph_algorithm_icons),
    algorithmSpinner.getSelectedItemPosition()
);

algorithmSpinner.setAdapter(spinnerAdapter);

AgorithmAdapter 的构造函数具有以下签名:

The constructor of AgorithmAdapter has the following signature:

public AlgorithmAdapter(Context context, Spinner spinner, int viewResourceId, String[] titles,
        String[] descriptions, TypedArray icons, int selectedPosition) {

getDropDownView()方法 AlgorithmAdapter

@Override
public View getDropDownView(final int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(viewResourceId, parent, false);
    }

    final ImageView icon = (ImageView) convertView.findViewById(R.id.algoItemIcon);
    final TextView title = (TextView) convertView.findViewById(R.id.algoItemTitle);
    final TextView description = (TextView) convertView.findViewById(R.id.algoItemDescription);
    final RadioButton radio = (RadioButton) convertView.findViewById(R.id.algoItemRadio);

    icon.setImageDrawable(icons.getDrawable(position));
    title.setText(titles[position]);
    description.setText(descriptions[position]);
    radioGroup.add(radio);

    /* it's essential to uncheck in case the positions do not match, because
     * the system reuses views that could still have a checked radio button */
    radio.setChecked(position == selectedPosition);

    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            // uncheck every other radio button
            for(RadioButton each : radioGroup)
                each.setChecked(false);

            // inform adapter and user that this button is selected now
            radio.setChecked(true);
            selectedPosition = position;

            spinner.setSelection(position);
        }

    });

    return convertView;
}

其中, radioGroup中是所有下拉列表视图中的所有单选按钮,以管理哪一个是目前检查以及哪些必须选中因此清单。

where radioGroup is a list of all RadioButtons from all drop down views in order to manage which one is currently checked and which ones have to be unchecked therefore.

这所有的作品非常好,除了一个事实,即弹出窗口不会关闭在 setSelection()(这本身就是工作虽然)。

That all works very well except for the fact that the popup doesn't close on setSelection() (which itself is working though).

我也试过到 OnItemClickListener 在那里安装到​​微调和管理选择的东西,而是抛出一个异常说 setOnItemClickListener不能用有微调,这实在是烦人,因为它觉得好像框架矗立在我的方式,而不是帮助我在这一点上。但无论如何,这是我必须通过适配器传递微调,我会很高兴我有......一个不同的解决方案的原因。

I also tried to attach an OnItemClickListener to the Spinner and manage the selection stuff in there, but that throws an exception saying setOnItemClickListener cannot be used with a spinner, which is really annoying since it feels as if the framework stands in my way instead of helping me at this point. But anyway, that's the reason I have to pass the Spinner through to the adapter which I would be glad I had a different solution for…

所以,我唯一缺少的就是我如何接近或处理纱厂降了下来。任何人可以帮助我吗?这是甚至任何接​​近的方式做呢?它不能是困难的,因为编写自定义适配器纱厂是pretty基本任务。

So the only thing I'm missing is how I "close" or "dispose" the Spinners drop down. Could anybody help me with that? Is this even anywhere near the way do to it? It cannot be that difficult since writing custom adapters for Spinners is a pretty basic task.

编辑:不知道,如果这会有所帮助,但也许有人在我的下拉视图项的布局文件发现错误。 algo_spinner_item_detail.xml

No idea if this helps, but maybe someone finds an error in the layout file of my drop down view item. algo_spinner_item_detail.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:background="@android:drawable/list_selector_background" >

    <ImageView
        android:id="@+id/algoItemIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:contentDescription="@string/algoIconDescription"
        android:layout_marginRight="8dp" />

    <RadioButton
        android:id="@+id/algoItemRadio"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dp"
        android:layout_centerVertical="false" />

    <TextView
        android:id="@+id/algoItemTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/algoItemIcon"
        android:layout_alignTop="@id/algoItemIcon"
        android:textStyle="bold"
        android:layout_marginBottom="1dp" />

    <TextView
        android:id="@+id/algoItemDescription"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/algoItemTitle"
        android:layout_alignLeft="@id/algoItemTitle"
        android:layout_toLeftOf="@id/algoItemRadio" />

</RelativeLayout>

赫克你怎么关闭/处置的F *** ING集团选择了一个项目后,微调下拉?

How the heck do you close/dispose the f***ing Spinner drop down after you selected an item?

推荐答案

下面就是没有的伎俩,我(警告:不要显示此code你的孩子,你一定去地狱吧)

Here's what did the trick for me (WARNING: don't show this code to your kids, you'd certainly go to hell for it):

在的的onClick 方法我 convertView 我把这个线code的:

In the onClick method of my convertView I put this line of code:

((View) parent.getParent().getParent().getParent().getParent()).setVisibility(View.GONE);

这不仅在该 convertView s的遏制,也使得 com.android.internal.policy摆脱了的LinearLayout的。 impl.PhoneWindow $ DecorView 无形的,这是有变黑活动屏幕,同时一个对话框/显示降下来了。

which not only gets rid of the LinearLayout in which the convertViews are contained, but also makes the com.android.internal.policy.impl.PhoneWindow$DecorView invisible, that was there to darken the Activity screen while a dialog/drop down is shown.

我仍然是一个更好的解决方案了。

I'm still after a better solution.

这篇关于微调定制适配器上选择不消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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