微调将不响应点击...即使onItemSelected叫? [英] Spinner won't respond to clicks...even though onItemSelected is called?

查看:113
本文介绍了微调将不响应点击...即使onItemSelected叫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动实现了一个微调OnItemSelected监听器。它发射出onItemSelected回调活动时显示的有趣的问题。所以我用一个标志黑客来解决它(我恨它,但在这一点上,我只是想应用到工作)。

My Activity implements OnItemSelected listener for a spinner. It has the interesting problem of firing off the onItemSelected callback when the activity shows. So I used a flag hack to solve it (I hate it, but at this point I just want the app to work).

奇怪的是,即使回调得到就在活动开始叫,我实际的触摸选择不工作。我能触摸到列表,打开它,看到来自阵列适配器的琴弦,甚至触摸它,使其解雇 - 但回调不会被调用

Strangely enough, even though the callback gets called right at activity start, my actual touch selections don't work. I can touch the list, open it, see the strings from the array adapter, and even touch it to make it dismiss - but the callback is never called.

我的code:

public class MyActivity extends Activity implements OnItemSelectedListener {
…
private ArrayList<String> mMyTypes = null;
private ArrayAdapter<String> mMyAdapter = null;
private Spinner mMyTypeSpinner = null;
// hack for spinner
boolean isFirstRunWithSpinner = true;

在的onCreate():

In onCreate():

mMyTypeSpinner = (Spinner) findViewById(R.id.my_activity_spinner);

mMyTypes = new ArrayList<String>();
mMyTypes.add("Test string");

mMyAdapter = new ArrayAdapter<String>(this, R.layout.custom_spinner_style, mMyTypes);
mMyAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mMyTypeSpinner.setAdapter(mMyAdapter);

// spinner listener
mMyTypeSpinner.setOnItemSelectedListener(this);

回调:

@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
 Toast
 .makeText(ConfirmEditActivity.this, "Selected", Toast.LENGTH_LONG)
 .show();
if( isFirstRunWithSpinner ) { isFirstRunWithSpinner = false; return; }
…
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
 Toast
 .makeText(ConfirmEditActivity.this, "Nothing", Toast.LENGTH_LONG)
 .show();
}

在吐司如右图所示时显示的活性,但是当我选择微调的项目,微调驳斥,并再次显示没有敬酒(更不用说code的其余部分在回调未能执行)。

The toast is shown right when the activity is shown, but when I select items in the spinner, the spinner dismisses and no toast is displayed again (not to mention the rest of the code in the callback fails to execute).

任何意见?

我真的希望这个简单的东西...

I really hope this something simple...

推荐答案

在选择新项目的微调将只ItemSelected事件。如果只有一个项目显示,你显然只能在下拉列表中重新选择这一项。这是Android的微调的正确的行为。如果您在列表中有超过1项onItemSelected将被解雇,如果点击的项目不是当前选择的项目。

The spinner will only respond to ItemSelected events when a new item is selected. If there's only one item showing, you obviously can only reselect this one item in the dropdown list. That's the correct behaviour of the android spinner. onItemSelected will be fired if you have more than 1 item in your list, and if the clicked item is not the currently selected item.

从Android文档:

From the android docs:

公共抽象无效onItemSelected(适配器视图父母,观景,INT位置,长的ID)

public abstract void onItemSelected (AdapterView parent, View view, int position, long id)

在此视图中的项目已选定要调用的回调方法。这个回调只是调用时的新选择的位置是从$ P $不同pviously选择的位置,或者如果没有选择的项目。

Callback method to be invoked when an item in this view has been selected. This callback is invoked only when the newly selected position is different from the previously selected position or if there was no selected item.

希望我能有所帮助。

这篇关于微调将不响应点击...即使onItemSelected叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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