我有一个带有回调实现的MainActivity:
我还添加了一个ActionBar SearchView,最后实现了OnQueryTextListener来改变每个kb类型的listFragment的listView,但是这样可行!此外,单击navDrawer上的项会为新类型的itemsList生成新的/替换FragmentList事务。
现在问题。 ActionBar SearchView实际上过滤了当前创建的itemList。
打开/关闭NavDrawer是使用经典"新的ActionBarDrawerToggle(){...}"在override方法中使用invalidateOptionsMenu()实现。但每次打开/关闭抽屉时,此调用都会触发onQueryTextChange执行...查看日志! 我故意生成一个异常来查看这些调用背后的堆栈跟踪:onRestore实际上是为SearchView调用的,我的问题是为什么以及如何避免SearchView的这种自动触发器?
如果打开抽屉,我可以检查onQueryTextChange内部以避免itemsList的重新生成。这可能是一个解决方案,但我想知道您的想法以及是否存在其他解决方案。
如果不清楚,我可能会上传一些代码。 日志在这里:
应用程序在开始时显示所有项目(抽屉位置0)。
10-30 14:34:47.871: V/com.dav.pc.ui.MainActivity(900): MainActivity.initializeDrawer called!
10-30 14:34:47.880: D/com.dav.pc.ui.DrawerListFragment(900): New Drawer Instance!
10-30 14:34:47.900: D/com.dav.pc.ui.MainActivity(900): MainActivity.initializeActivity NewLaunch=Tutti gli elementi
10-30 14:34:47.920: D/com.dav.pc.ui.MainActivity(900): MainActivity.onDrawerItemSelected List selected: ALL
10-30 14:34:47.920: D/com.dav.pc.ui.MainActivity(900): MainActivity.onDrawerItemSelected Transaction Commit
10-30 14:34:47.920: D/com.dav.pc.ui.MainActivity(900): MainActivity.onDrawerItemSelected Label selected: ALL
10-30 14:34:47.930: V/com.dav.pc.ui.DrawerListFragment(900): DrawerListFragment.onCreate() called!
10-30 14:34:47.930: D/com.dav.pc.ui.DrawerListFragment(900): SetPosition onCreate activated_position=-1
10-30 14:34:47.980: V/com.dav.pc.ui.DrawerListFragment(900): DrawerListFragment.onViewCreated() called!
10-30 14:34:47.980: D/com.dav.pc.ui.DrawerListFragment(900): activated_position=0
10-30 14:34:47.980: V/com.dav.pc.ui.DrawerListFragment(900): DrawerListFragment.setSelection() called!
10-30 14:34:47.980: D/com.dav.pc.ui.DrawerListFragment(900): CurrentPosition=0
10-30 14:34:47.980: V/com.dav.pc.ui.DrawerListFragment(900): DrawerListFragment.onActivityCreated() called!
10-30 14:34:47.990: D/com.dav.pc.ui.ItemListFragment(900): ItemListFragment.onCreate Bundle exists
10-30 14:34:47.990: D/com.dav.pc.ui.ItemListFragment(900): Bundle[{list_id=ALL}]
10-30 14:34:47.990: D/com.dav.pc.ui.ItemListFragment(900): Create List ALL (16 items)
10-30 14:34:48.001: I/System.out(900): [mFirstLetters>Item 0,0]
10-30 14:34:48.001: I/System.out(900): [mFirstLetters>Item 1,1]
10-30 14:34:48.001: I/System.out(900): [mFirstLetters>Item 10,2]
10-30 14:34:48.001: I/System.out(900): [mFirstLetters>Item 11,3]
10-30 14:34:48.010: I/System.out(900): [mFirstLetters>Item 12,4]
10-30 14:34:48.010: I/System.out(900): [mFirstLetters>Item 13,5]
10-30 14:34:48.010: I/System.out(900): [mFirstLetters>Item 14,6]
10-30 14:34:48.020: I/System.out(900): [mFirstLetters>Item 15,7]
10-30 14:34:48.020: I/System.out(900): [mFirstLetters>Item 2,8]
10-30 14:34:48.020: I/System.out(900): [mFirstLetters>Item 3,9]
10-30 14:34:48.030: I/System.out(900): [mFirstLetters>Item 4,10]
10-30 14:34:48.040: I/System.out(900): [mFirstLetters>Item 5,11]
10-30 14:34:48.040: I/System.out(900): [mFirstLetters>Item 6,12]
10-30 14:34:48.051: I/System.out(900): [mFirstLetters>Item 7,13]
10-30 14:34:48.051: I/System.out(900): [mFirstLetters>Item 8,14]
10-30 14:34:48.051: I/System.out(900): [mFirstLetters>Item 9,15]
10-30 14:34:48.090: V/com.dav.pc.ui.MainActivity(900): onPostCreate called!
10-30 14:34:48.101: V/com.dav.pc.ui.ItemListFragment(900): ItemListFragment.onResume() called!
10-30 14:34:48.710: V/com.dav.pc.ui.MainActivity(900): MainActivity.onCreateOptionsMenu called!
I type 0 in the searchView:
10-30 14:34:55.421: V/com.dav.pc.ui.MainActivity(900): MainActivity.onQueryTextChange() newText: 0
10-30 14:34:55.440: I/System.out(900): [mFirstLetters>Item 0,0]
10-30 14:34:55.440: I/System.out(900): [mFirstLetters>Item 10,1]
this is another strange behaviour: getItemId in the ItemListAdapter is called 5 times... always, why?
10-30 14:34:55.470: D/com.dav.pc.ui.ItemListAdapter(900): getItemId position: 0
10-30 14:34:55.470: D/com.dav.pc.ui.ItemListAdapter(900): getItemId position: 0
10-30 14:34:55.490: D/com.dav.pc.ui.ItemListAdapter(900): getItemId position: 0
10-30 14:34:55.490: D/com.dav.pc.ui.ItemListAdapter(900): getItemId position: 0
10-30 14:34:55.540: D/com.dav.pc.ui.ItemListAdapter(900): getItemId position: 0
10-30 14:35:01.940: V/com.dav.pc.ui.MainActivity(900): MainActivity.onCreateOptionsMenu called!
onQueryTextChange is fired.
Here my controlled exception, method triggered when Drawer is opened!
This method creates a new ItemListAdapter and passed to the mItemListFragment:
10-30 14:35:02.080: V/com.dav.pc.ui.MainActivity(900): MainActivity.onQueryTextChange() newText: 0
10-30 14:35:02.090: I/System.out(900): [mFirstLetters>Item 0,0]
10-30 14:35:02.100: I/System.out(900): [mFirstLetters>Item 10,1]
10-30 14:35:01.980: W/System.err(900): at com.dav.pc.ui.MainActivity.onQueryTextChange(MainActivity.java:306)
10-30 14:35:01.990: W/System.err(900): at android.widget.SearchView.onTextChanged(SearchView.java:1081)
10-30 14:35:01.990: W/System.err(900): at android.widget.SearchView.access$1900(SearchView.java:90)
10-30 14:35:01.990: W/System.err(900): at android.widget.SearchView$10.onTextChanged(SearchView.java:1536)
10-30 14:35:01.990: W/System.err(900): at android.widget.TextView.sendOnTextChanged(TextView.java:7577)
10-30 14:35:01.990: W/System.err(900): at android.widget.TextView.setText(TextView.java:3259)
10-30 14:35:02.000: W/System.err(900): at android.widget.TextView.setText(TextView.java:3110)
10-30 14:35:02.000: W/System.err(900): at android.widget.EditText.setText(EditText.java:78)
10-30 14:35:02.000: W/System.err(900): at android.widget.TextView.setText(TextView.java:3085)
10-30 14:35:02.000: W/System.err(900): at android.widget.TextView.onRestoreInstanceState(TextView.java:2985)
10-30 14:35:02.000: W/System.err(900): at android.view.View.dispatchRestoreInstanceState(View.java:9876)
10-30 14:35:02.010: W/System.err(900): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:35:02.010: W/System.err(900): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:35:02.010: W/System.err(900): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:35:02.010: W/System.err(900): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:35:02.020: W/System.err(900): at android.view.View.restoreHierarchyState(View.java:9854)
10-30 14:35:02.020: W/System.err(900): at com.android.internal.view.menu.MenuBuilder.restoreActionViewStates(MenuBuilder.java:358)
10-30 14:35:02.020: W/System.err(900): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:412)
10-30 14:35:02.020: W/System.err(900): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:739)
10-30 14:35:02.030: W/System.err(900): at android.app.Activity.invalidateOptionsMenu(Activity.java:2552)
10-30 14:35:02.030: W/System.err(900): at com.dav.pc.ui.MainActivity$1.onDrawerOpened(MainActivity.java:187)
10-30 14:35:02.030: W/System.err(900): at android.support.v4.widget.DrawerLayout.dispatchOnDrawerOpened(DrawerLayout.java:488)
10-30 14:35:02.040: W/System.err(900): at android.support.v4.widget.DrawerLayout.updateDrawerState(DrawerLayout.java:459)
10-30 14:35:02.040: W/System.err(900): at android.support.v4.widget.DrawerLayout$ViewDragCallback.onViewDragStateChanged(DrawerLayout.java:1355)
...
10-30 14:35:42.040: V/com.dav.pc.ui.MainActivity(900): MainActivity.onCreateOptionsMenu called!
This is my controlled exception, method triggered when Drawer is closed.
Again the execution of the method.
10-30 14:35:42.170: V/com.dav.pc.ui.MainActivity(900): MainActivity.onQueryTextChange() newText: 0
10-30 14:35:42.170: I/System.out(900): [mFirstLetters>Item 0,0]
10-30 14:35:42.180: I/System.out(900): [mFirstLetters>Item 10,1]
10-30 14:35:42.080: W/System.err(900): at com.dav.pc.ui.MainActivity.onQueryTextChange(MainActivity.java:306)
10-30 14:35:42.080: W/System.err(900): at android.widget.SearchView.onTextChanged(SearchView.java:1081)
10-30 14:35:42.090: W/System.err(900): at android.widget.SearchView.access$1900(SearchView.java:90)
10-30 14:35:42.090: W/System.err(900): at android.widget.SearchView$10.onTextChanged(SearchView.java:1536)
10-30 14:35:42.090: W/System.err(900): at android.widget.TextView.sendOnTextChanged(TextView.java:7577)
10-30 14:35:42.090: W/System.err(900): at android.widget.TextView.setText(TextView.java:3259)
10-30 14:35:42.090: W/System.err(900): at android.widget.TextView.setText(TextView.java:3110)
10-30 14:35:42.090: W/System.err(900): at android.widget.EditText.setText(EditText.java:78)
10-30 14:35:42.100: W/System.err(900): at android.widget.TextView.setText(TextView.java:3085)
10-30 14:35:42.100: W/System.err(900): at android.widget.TextView.onRestoreInstanceState(TextView.java:2985)
10-30 14:35:42.100: W/System.err(900): at android.view.View.dispatchRestoreInstanceState(View.java:9876)
10-30 14:35:42.110: W/System.err(900): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:35:42.110: W/System.err(900): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:35:42.110: W/System.err(900): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:35:42.110: W/System.err(900): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:35:42.120: W/System.err(900): at android.view.View.restoreHierarchyState(View.java:9854)
10-30 14:35:42.120: W/System.err(900): at com.android.internal.view.menu.MenuBuilder.restoreActionViewStates(MenuBuilder.java:358)
10-30 14:35:42.120: W/System.err(900): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:412)
10-30 14:35:42.120: W/System.err(900): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:739)
10-30 14:35:42.130: W/System.err(900): at android.app.Activity.invalidateOptionsMenu(Activity.java:2552)
10-30 14:35:42.130: W/System.err(900): at com.dav.pc.ui.MainActivity$1.onDrawerClosed(MainActivity.java:180)
10-30 14:35:42.140: W/System.err(900): at android.support.v4.widget.DrawerLayout.dispatchOnDrawerClosed(DrawerLayout.java:477)
10-30 14:35:42.140: W/System.err(900): at android.support.v4.widget.DrawerLayout.updateDrawerState(DrawerLayout.java:457)
10-30 14:35:42.140: W/System.err(900): at android.support.v4.widget.DrawerLayout$ViewDragCallback.onViewDragStateChanged(DrawerLayout.java:1355)
....
=========================================================================
This is when instead I select a new ItemDrawer to generate the new list with fragment transaction:
10-30 14:37:47.961: D/com.dav.pc.ui.DrawerListAdapter(900): getItemId position: 5
10-30 14:37:48.100: D/com.dav.pc.ui.DrawerListAdapter(900): getItemId position: 5
10-30 14:37:48.100: D/com.dav.pc.ui.DrawerListAdapter(900): getItemId position: 0
10-30 14:37:48.100: D/com.dav.pc.ui.MainActivity(900): MainActivity.onDrawerItemSelected ListId selected: 4
10-30 14:37:48.100: D/com.dav.pc.ui.MainActivity(900): MainActivity.onDrawerItemSelected ClearSearch
10-30 14:37:48.161: V/com.dav.pc.ui.MainActivity(900): MainActivity.onQueryTextChange() newText:
(I Should avoid this useless generation when clearing the searchText)
10-30 14:37:48.170: I/System.out(900): [mFirstLetters>Item 0,0]
10-30 14:37:48.170: I/System.out(900): [mFirstLetters>Item 1,1]
10-30 14:37:48.170: I/System.out(900): [mFirstLetters>Item 10,2]
10-30 14:37:48.170: I/System.out(900): [mFirstLetters>Item 11,3]
10-30 14:37:48.170: I/System.out(900): [mFirstLetters>Item 12,4]
10-30 14:37:48.181: I/System.out(900): [mFirstLetters>Item 13,5]
10-30 14:37:48.181: I/System.out(900): [mFirstLetters>Item 14,6]
10-30 14:37:48.181: I/System.out(900): [mFirstLetters>Item 15,7]
10-30 14:37:48.181: I/System.out(900): [mFirstLetters>Item 2,8]
10-30 14:37:48.181: I/System.out(900): [mFirstLetters>Item 3,9]
10-30 14:37:48.191: I/System.out(900): [mFirstLetters>Item 4,10]
10-30 14:37:48.191: I/System.out(900): [mFirstLetters>Item 5,11]
10-30 14:37:48.191: I/System.out(900): [mFirstLetters>Item 6,12]
10-30 14:37:48.191: I/System.out(900): [mFirstLetters>Item 7,13]
10-30 14:37:48.191: I/System.out(900): [mFirstLetters>Item 8,14]
10-30 14:37:48.191: I/System.out(900): [mFirstLetters>Item 9,15]
Again my controlled exception to see the stacktrace:
10-30 14:37:48.111: W/System.err(900): at com.dav.pc.ui.MainActivity.onQueryTextChange(MainActivity.java:306)
10-30 14:37:48.111: W/System.err(900): at android.widget.SearchView.onTextChanged(SearchView.java:1081)
10-30 14:37:48.111: W/System.err(900): at android.widget.SearchView.access$1900(SearchView.java:90)
10-30 14:37:48.121: W/System.err(900): at android.widget.SearchView$10.onTextChanged(SearchView.java:1536)
10-30 14:37:48.121: W/System.err(900): at android.widget.TextView.sendOnTextChanged(TextView.java:7577)
10-30 14:37:48.121: W/System.err(900): at android.widget.TextView.setText(TextView.java:3259)
10-30 14:37:48.121: W/System.err(900): at android.widget.TextView.setText(TextView.java:3110)
10-30 14:37:48.121: W/System.err(900): at android.widget.EditText.setText(EditText.java:78)
10-30 14:37:48.121: W/System.err(900): at android.widget.TextView.setText(TextView.java:3085)
10-30 14:37:48.131: W/System.err(900): at android.widget.SearchView.setQuery(SearchView.java:481)
10-30 14:37:48.131: W/System.err(900): at com.dav.pc.ui.MainActivity.onDrawerItemSelected(MainActivity.java:210)
10-30 14:37:48.131: W/System.err(900): at com.dav.pc.ui.DrawerListFragment.onListItemClick(DrawerListFragment.java:153)
10-30 14:37:48.131: W/System.err(900): at android.app.ListFragment$2.onItemClick(ListFragment.java:160)
...
10-30 14:37:48.201: D/com.dav.pc.ui.MainActivity(900): MainActivity.onDrawerItemSelected Transaction Commit
10-30 14:37:48.201: D/com.dav.pc.ui.MainActivity(900): MainActivity.onDrawerItemSelected Label selected: 4
10-30 14:37:48.201: D/com.dav.pc.ui.MainActivity(900): MainActivity.onDrawerItemSelected ClosingDrawer
10-30 14:37:48.201: V/com.dav.pc.ui.DrawerListFragment(900): DrawerListFragment.setSelection() called!
10-30 14:37:48.211: D/com.dav.pc.ui.DrawerListFragment(900): CurrentPosition=5
10-30 14:37:48.211: D/com.dav.pc.ui.DrawerListAdapter(900): getItemId position: 0
10-30 14:37:48.211: D/com.dav.pc.ui.DrawerListAdapter(900): getItemId position: 5
10-30 14:37:48.411: V/com.dav.pc.ui.ItemListFragment(900): ItemListFragment.onPause() called!
10-30 14:37:48.431: D/com.dav.pc.ui.ItemListFragment(900): ItemListFragment.onCreate Bundle exists
10-30 14:37:48.431: D/com.dav.pc.ui.ItemListFragment(900): Bundle[{list_id=4}]
10-30 14:37:48.431: D/com.dav.pc.ui.ItemListFragment(900): Create List 4
(The list 4 contains 6 items, we are in onCreate)
10-30 14:37:48.431: I/System.out(900): [mFirstLetters>Item 0,0]
10-30 14:37:48.431: I/System.out(900): [mFirstLetters>Item 1,1]
10-30 14:37:48.431: I/System.out(900): [mFirstLetters>Item 2,2]
10-30 14:37:48.441: I/System.out(900): [mFirstLetters>Item 3,3]
10-30 14:37:48.441: I/System.out(900): [mFirstLetters>Item 4,4]
10-30 14:37:48.441: I/System.out(900): [mFirstLetters>Item 5,5]
10-30 14:37:48.461: V/com.dav.pc.ui.ItemListFragment(900): ItemListFragment.onResume() called!
10-30 14:37:48.721: V/com.dav.pc.ui.MainActivity(900): MainActivity.onCreateOptionsMenu called!
10-30 14:37:48.811: V/com.dav.pc.ui.MainActivity(900): MainActivity.onQueryTextChange() newText:
10-30 14:37:48.811: I/System.out(900): [mFirstLetters>Item 0,0]
10-30 14:37:48.811: I/System.out(900): [mFirstLetters>Item 1,1]
10-30 14:37:48.821: I/System.out(900): [mFirstLetters>Item 2,2]
10-30 14:37:48.821: I/System.out(900): [mFirstLetters>Item 3,3]
10-30 14:37:48.821: I/System.out(900): [mFirstLetters>Item 4,4]
10-30 14:37:48.821: I/System.out(900): [mFirstLetters>Item 5,5]
10-30 14:37:48.761: W/System.err(900): at com.dav.pc.ui.MainActivity.onQueryTextChange(MainActivity.java:306)
10-30 14:37:48.761: W/System.err(900): at android.widget.SearchView.onTextChanged(SearchView.java:1081)
10-30 14:37:48.761: W/System.err(900): at android.widget.SearchView.access$1900(SearchView.java:90)
10-30 14:37:48.761: W/System.err(900): at android.widget.SearchView$10.onTextChanged(SearchView.java:1536)
10-30 14:37:48.761: W/System.err(900): at android.widget.TextView.sendOnTextChanged(TextView.java:7577)
10-30 14:37:48.761: W/System.err(900): at android.widget.TextView.setText(TextView.java:3259)
10-30 14:37:48.761: W/System.err(900): at android.widget.TextView.setText(TextView.java:3110)
10-30 14:37:48.771: W/System.err(900): at android.widget.EditText.setText(EditText.java:78)
10-30 14:37:48.771: W/System.err(900): at android.widget.TextView.setText(TextView.java:3085)
10-30 14:37:48.771: W/System.err(900): at android.widget.TextView.onRestoreInstanceState(TextView.java:2985)
10-30 14:37:48.771: W/System.err(900): at android.view.View.dispatchRestoreInstanceState(View.java:9876)
10-30 14:37:48.771: W/System.err(900): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:37:48.771: W/System.err(900): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:37:48.781: W/System.err(900): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:37:48.781: W/System.err(900): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2330)
10-30 14:37:48.781: W/System.err(900): at android.view.View.restoreHierarchyState(View.java:9854)
10-30 14:37:48.781: W/System.err(900): at com.android.internal.view.menu.MenuBuilder.restoreActionViewStates(MenuBuilder.java:358)
10-30 14:37:48.781: W/System.err(900): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:412)
10-30 14:37:48.781: W/System.err(900): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:739)
10-30 14:37:48.791: W/System.err(900): at android.app.Activity.invalidateOptionsMenu(Activity.java:2552)
10-30 14:37:48.791: W/System.err(900): at com.dav.pc.ui.MainActivity$1.onDrawerClosed(MainActivity.java:180)
...
答案 0 :(得分:2)
我刚刚发现了一个对我很干净的新解决方案,也许其他人感兴趣:
在抽屉的打开/关闭实现中,我直接调用onPrepareOptionsMenu(mMenu)
而不是 invalidateOptionsMenu()
。
如您所见,我在创建时使用了mMenu中保存的菜单实例。调用此方法我避免使searchView无效,仍然可以包含一些文本,因此不能清除。
这是清除/图标化searchView的另一个技巧(总是在onPrepareOptionsMenu
内)我检查当前是否在我的Service类中设置了searchText:
if (!Service.getInstance().hasSearchText()) {
Log.d(MainActivity.class.getSimpleName(), "onPrepareOptionsMenu Clearing SearchView!");
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setIconified(true);// This also clears the text in SearchView widget
}