我正在开发Android版3 +
在我的行动吧,我有一个下拉列表(见how隐藏/取消隐藏动作条列表上的Android 3.0?,在下拉我打算)。现在的问题是,我需要当用户选择的东西做了一定作用,但Android把onNavigationItemSelected()作为soons,因为它吸引的观点,所以没有选择实际发生的情况。
我如何可以检测用户是否确实pressed东西,它不是来自Android的假通话?
公共类ListDittaListener实现OnNavigationListener {
私人BaseActivity活动;
私人ListDittaListener()
{
}
公共ListDittaListener(BaseActivity活动)
{
this.activity =活动;
}
@覆盖
公共布尔onNavigationItemSelected(INT itemPosition,长的itemId)
{
的MyApp appState =((MyApp的)this.activity.getApplicationContext());
appState.setDittaSelezionata((INT)的itemId);
共享preferences设置= this.activity.getShared preferences(MyApp的preFS_NAME,0);
共享preferences.Editor编辑器= settings.edit();
editor.putInt(ditta_id_selezionata,(INT)的itemId);
////////重启活动this.activity.recreate();
返回false;
}
}
解决方案
您可以很容易地忽略到 onNavigationItemSelected
第一个电话,如果你喜欢:
公共类无论实现OnNavigationListener {
私人布尔合成= TRUE;
@覆盖
公共布尔onNavigationItemSelected(INT itemPosition,长的itemId){
如果(合成){
合成= FALSE;
返回true;
}
//做任何你真的想在这里
}
}
I'm developing for android 3+
In my action bar i have a drop-down list(see how to hide/unhide the actionbar list on android 3? for the dropdown i intend). The problem is i need to do a certain action when the user selects something, but Android calls onNavigationItemSelected() as soons as it draws the view, so no selection actually happened.
How can i detect if the user actually pressed something and it is not a fake call from android ?
public class ListDittaListener implements OnNavigationListener{
private BaseActivity activity;
private ListDittaListener()
{
}
public ListDittaListener(BaseActivity activity)
{
this.activity = activity;
}
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId)
{
MyApp appState = ((MyApp)this.activity.getApplicationContext());
appState.setDittaSelezionata( (int) itemId);
SharedPreferences settings = this.activity.getSharedPreferences(MyApp.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("ditta_id_selezionata", (int) itemId);
////////restart activity this.activity.recreate();
return false;
}
}
解决方案
You can easily just ignore the first call to onNavigationItemSelected
if you like:
public class Whatever implements OnNavigationListener {
private boolean synthetic = true;
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
if (synthetic) {
synthetic = false;
return true;
}
// do whatever you really wanted here
}
}