微调onItemSelected()执行时,它不是假设不是、onItemSelected

2023-09-11 12:45:10 作者:乐言

可能重复:   Android微调OnItemSelected调用错误地(无需用户操作上打开微调)

有谁知道如何prevent的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.

我曾尝试创建一个if语句最初设置为false,周围的一切code重写的方法里面,但也没有办法知道什么时候,因为被覆盖的方法的onCreate运行后设置为true() ,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.

我还没有发现任何明确的答案,在此。任何明确的解决方案,将极大地AP preciated。

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

推荐答案

大卫,这里是一个教程,我写了这个问题...

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

问题陈述

这是不可取的onItemSelected()被触发,而画廊(或微调)正在初始化。 这意味着,code是prematurely执行; code,其目的是执行只有当用户物理地作出选择。

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.

解决方案

在的onCreate(),计数许多画廊(或微调)怎么部件必须在视图中。 (mGalleryCount) 在onItemSelected(),怎么算往往引发。 (mGalleryInitializedCount) 时(mGalleryInitializedCount< mGalleryCount)== false,那么执行code意味着用户

C例程的 $ C $

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.