首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spinner ()执行不当

Spinner ()执行不当
EN

Stack Overflow用户
提问于 2011-04-11 17:02:27
回答 6查看 32.2K关注 0票数 46

可能重复:

Android Spinner OnItemSelected Called Erroneously (without user action on opening spinner)

当布局被实例化时,有人知道如何防止onItemSelected() (OnItemSelectedListener接口)方法运行吗?我需要知道是否有办法做到这一点,因为我希望将我的布局实例化方式与这个侦听器分开。

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

关于这一点,我没有找到明确的答案。任何明确的解决方案都将不胜感激。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-05-07 00:00:09

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

问题陈述

当库(或旋转器)正在初始化时,会触发一个不受欢迎的onItemSelected()。这意味着代码是过早地执行的;代码只在用户实际做出选择时才会执行。

溶液

  1. in onCreate(),计算视图中有多少图库(或旋转器)小部件。(mGalleryCount)
  2. in onItemSelected(),计算它触发的频率。(mGalleryInitializedCount)
  3. when (mGalleryInitializedCount < mGalleryCount) == false,然后执行用于用户

的代码

代码示例

代码语言:javascript
复制
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) );
        }

    }

}

为什么这样做,

此解决方案之所以有效,是因为库在用户进行物理选择之前很久就完成了初始化。

票数 66
EN

Stack Overflow用户

发布于 2011-09-16 23:51:23

这里是一个修改版本的“某人某处”的代码。如果您有一个视图,您可以使用它。

代码语言:javascript
复制
public class myActivity extends Activity implements OnItemSelectedListener
{
// Set view initialization to false while the it is being built
private boolean initializedView = false;

//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
    //


}


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

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

}
}
票数 15
EN

Stack Overflow用户

发布于 2011-10-05 10:41:53

同样的解决办法:

代码语言:javascript
复制
private int m_intSpinnerInitiCount = 0;
private static final int NO_OF_EVENTS = 1;

...

m_spnTemplates.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, 
                    View selectedItemView, int position, long id) { 

                //trying to avoid undesired spinner selection changed event, a known problem
                    if (m_intSpinnerInitiCount < NO_OF_EVENTS) {
                        m_intSpinnerInitiCount++;
                    } else {                
                        //YOUR CODE HERE
                    }                           
            }
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5624825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档