Spinner onItemSelected()执行不当 [英] Spinner onItemSelected() executes inappropriately

查看:227
本文介绍了Spinner onItemSelected()执行不当的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Android Spinner OnItemSelected错误地调用(没有用户操作打开微调器)

有人知道如何在实例化布局时阻止onItemSelected()(OnItemSelectedListener接口)方法运行吗?我需要知道是否有办法做到这一点,因为我想保持我如何实例化我的布局与该侦听器分开。

Does anyone know how to prevent the onItemSelected() (OnItemSelectedListener interface) method from running when the layout is instantiated? I need to know if there is a way to do this because I want to keep how I instantiate my layout separate from this listener.

我尝试在重写方法内的所有代码周围创建一个最初设置为false的if语句,但是没有办法知道何时将其设置为true,因为重写的方法每次都在onCreate(),onStart()和onResume()方法之后运行。

I have tried creating an if statement initially set to false around all the code inside of the overridden method, but there is no way of knowing when to set it to true because the overridden method runs after the onCreate(), onStart(), and onResume() methods everytime.

我没有找到任何明确的答案。任何明确的解决方案都将不胜感激。

I have not found any clear cut answers on this. Any clear cut solutions would be greatly appreciated.

推荐答案

David,这是我为这个问题写的教程...

David, here is a tutorial I wrote up for this problem...

问题陈述

在图库中触发不良的onItemSelected()(或Spinner)正在初始化。
这意味着代码过早执行;代码仅在用户实际进行选择时执行。

an undesirable onItemSelected() is triggered whilst the Gallery (or Spinner) is initializing. This means that code is prematurely executed; code which is intended to execute ONLY when a user physically makes a selection.

解决方案


  1. ,计算视图中有多少个Gallery(或Spinner)小部件。 (mGalleryCount)
  2. onItemSelected()中的
  3. ,计算它触发的频率。 (mGalleryInitializedCount)

  4. 当(mGalleryInitializedCount< mGalleryCount)== false时,然后执行适合用户的代码

  1. in onCreate(), count how many Gallery (or Spinner) widgets you have in the view. (mGalleryCount)
  2. in onItemSelected(), count how often it has triggered. (mGalleryInitializedCount)
  3. when (mGalleryInitializedCount < mGalleryCount) == false, then execute the code meant for the user

代码示例

public class myActivity extends Activity implements OnItemSelectedListener
{
    //this counts how many Gallery's are on the UI
    private int mGalleryCount=0;

    //this counts how many Gallery's have been initialized
    private int mGalleryInitializedCount=0;

    //UI reference
    private Gallery mGallery;


    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.myxmllayout);

        //get references to UI components
        mGallery = (Gallery) findViewById(R.id.mygallery);

        //trap selection events from gallery
        mGallery.setOnItemSelectedListener(this);

        //trap only selection when no flinging is taking place
        mGallery.setCallbackDuringFling(false);

        //
        //do other stuff like load images, setAdapter(), etc
        //

        //define how many Gallery's are in this view
        //note: this could be counted dynamically if you are programmatically creating the view
        mGalleryCount=1;

    }


    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {

        if (mGalleryInitializedCount < mGalleryCount)
        {
            mGalleryInitializedCount++;
        }
        else
        {
            //only detect selection events that are not done whilst initializing
            Log.i(TAG, "selected item position = " + String.valueOf(position) );
        }

    }

}

为什么会这样?

此解决方案有效,因为图库在用户实际进行选择之前很久就完成了初始化。

this solution works because the Gallery finishes initialization long before a user is physically able to make a selection.

这篇关于Spinner onItemSelected()执行不当的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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