微调onItemSelected()执行不当
可能重复:
Android微调OnItemSelected调用错误(没有用户操作打开微调)
有谁知道如何防止onItemSelected()(OnItemSelectedListener接口)方法在布局实例化时运行吗? 我需要知道是否有办法做到这一点,因为我想保持如何实例化我的布局从这个监听器分开。
我已经尝试创build一个if语句最初设置为重写的方法内的所有代码周围的假,但没有办法知道什么时候将其设置为true,因为重写的方法在onCreate(),onStart()之后运行和onResume()方法。
我还没有find明确的答案。 任何明确的解决scheme将不胜感激。
大卫,这是我为这个问题写的一个教程…
问题陈述
当Gallery(或Spinner)正在初始化时触发不希望的onItemSelected()。 这意味着代码被提前执行; 只有在用户物理地进行select时才打算执行的代码。
解
- 在onCreate()中,计算视图中有多less个Gallery(或Spinner)小部件。 (mGalleryCount)
- 在onItemSelected()中,计算它触发的频率。 (mGalleryInitializedCount)
- 当(mGalleryInitializedCount <mGalleryCount)== false时,则执行用于该用户的代码
代码示例
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) ); } } }
为什么这个工作
此解决scheme的工作原理是,在用户完全可以进行select之前,Gallery完成初始化。
这里是“Someone Somewhere”代码的修改版本。 你可以使用它,如果你有一个单一的看法。
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) ); } } }
同样的解决
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 } }
我昨天遇到了一个OnCheckedChangedListener的问题。 我最终添加了一个布尔实例variables,初始化为我的适配器类中的一个存取方法isListenerEnabled()。 然后,在布局代码中将variables设置为false,并在布局代码的末尾再次将其设置为true。 在我的监听器中,我检查variables的值来决定是否执行监听器代码。
每次onPause
被调用时,您都可以将variables设置为false
。 至于何时将其设置为true
,您可以在onResume
之后为第一个动作/键/轨迹球事件执行此操作。
我也在互联网上寻找一个很好的解决scheme,但没有发现任何满足我的需求。 所以我在Spinner类上写了这个扩展,所以你可以设置一个简单的OnItemClickListener,它和ListView具有相同的行为。
只有当某个项目被选中时,才会调用onItemClickListener。
玩得开心!
public class MySpinner extends Spinner { private OnItemClickListener onItemClickListener; public MySpinner(Context context) { super(context); } public MySpinner(Context context, AttributeSet attrs) { super(context, attrs); } public MySpinner(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void setOnItemClickListener(android.widget.AdapterView.OnItemClickListener inOnItemClickListener) { this.onItemClickListener = inOnItemClickListener; } @Override public void onClick(DialogInterface dialog, int which) { super.onClick(dialog, which); if (this.onItemClickListener != null) { this.onItemClickListener.onItemClick(this, this.getSelectedView(), which, this.getSelectedItemId()); } } }