android – Action Bar Sherlock SearchView点击它不会扩展
作者:互联网
我有一个Sherlock Fragment Activity,我使用片段寻呼机适配器设置不同的sherlock片段.
现在在每个片段中显示searchview我已将此方法放在onCreate of Fragment ::
此方法将在片段的ActionBar中显示菜单项.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
我现在也像这样设置onCreateOptionMenu()::
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu, menu);
final MenuItem searchItem = menu.findItem(R.id.menu_item_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Search Here");
}
现在奇怪的事情发生了,在我的第一个片段中我做了同样的事情,搜索视图成功打开.
如果
>我转到第一个片段,打开searchview然后转到第二个片段
>片段搜索视图将被打开.
第三个片段也是如此
>转到第3片段,
>打开searchview,
>回到第二个片段,searchview开启.
但是,如果我直接转到第二个片段,则不会打开searchview.
希望我很清楚.
关于这个的任何线索?
任何建议/链接将不胜感激..
提前致谢…
编辑::
menu.xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_item_search"
android:icon="@drawable/abs__ic_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
android:title="Search Products">
</item>
<item
android:id="@+id/root_menu"
android:icon="@drawable/abs__ic_menu_moreoverflow_normal_holo_light"
android:showAsAction="always"
android:title="More">
<menu>
<item
android:id="@+id/menu_Home"
android:icon="@drawable/home"
android:showAsAction="never"
android:title="Home"/>
<item
android:id="@+id/menu_favourite"
android:icon="@drawable/favourite"
android:showAsAction="never"
android:title="Favourite"/>
<item
android:id="@+id/menu_Balance"
android:icon="@drawable/balance"
android:showAsAction="never"
android:title="Balance"/>
<item
android:id="@+id/menu_logout"
android:icon="@drawable/btn_logout"
android:showAsAction="never"
android:title="Logout"/>
</menu>
</item>
</menu>
Logcat ::
12-06 16:45:26.401: E/AndroidRuntime(1581): FATAL EXCEPTION: main
12-06 16:45:26.401: E/AndroidRuntime(1581): java.lang.ClassCastException: com.mobifin.subscriber.ButtonPayActivity
12-06 16:45:26.401: E/AndroidRuntime(1581): at com.mobifin.subscriber.home.BillPayFragment.onCreateOptionsMenu(BillPayFragment.java:101)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:55)
12-06 16:45:26.401: E/AndroidRuntime(1581): at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:560)
12-06 16:45:26.401: E/AndroidRuntime(1581): at com.actionbarsherlock.internal.ActionBarSherlockCompat.preparePanel(ActionBarSherlockCompat.java:466)
12-06 16:45:26.401: E/AndroidRuntime(1581): at com.actionbarsherlock.internal.ActionBarSherlockCompat.dispatchInvalidateOptionsMenu(ActionBarSherlockCompat.java:265)
12-06 16:45:26.401: E/AndroidRuntime(1581): at com.actionbarsherlock.app.SherlockFragmentActivity.invalidateOptionsMenu(SherlockFragmentActivity.java:149)
12-06 16:45:26.401: E/AndroidRuntime(1581): at com.actionbarsherlock.app.SherlockFragmentActivity.supportInvalidateOptionsMenu(SherlockFragmentActivity.java:155)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.support.v4.app.Fragment.setMenuVisibility(Fragment.java:726)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:127)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.support.v4.view.ViewPager.populate(ViewPager.java:802)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1280)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.support.v4.view.ViewPager.computeScroll(ViewPager.java:1176)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.drawChild(ViewGroup.java:1562)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.View.draw(View.java:6883)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.View.draw(View.java:6883)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.widget.FrameLayout.draw(FrameLayout.java:357)
12-06 16:45:26.401: E/AndroidRuntime(1581): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewRoot.draw(ViewRoot.java:1522)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.os.Looper.loop(Looper.java:130)
12-06 16:45:26.401: E/AndroidRuntime(1581): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-06 16:45:26.401: E/AndroidRuntime(1581): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 16:45:26.401: E/AndroidRuntime(1581): at java.lang.reflect.Method.invoke(Method.java:507)
12-06 16:45:26.401: E/AndroidRuntime(1581): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-06 16:45:26.401: E/AndroidRuntime(1581): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-06 16:45:26.401: E/AndroidRuntime(1581): at dalvik.system.NativeStart.main(Native Method)
解决方法:
我们通常使用onCreateOptionsMenu()来创建片段和活动的选项菜单.
如果你浏览文档,还有另一个名为onPrepareOptionsMenu()的辅助方法,根据文档说明:
Prepare the Screen’s standard options menu to be displayed. This is
called right before the menu is shown, every time it is shown. You can
use this method to efficiently enable/disable items or otherwise
dynamically modify the contents.
当我们没有在ViewPager中实例化片段或任何类似的选项卡(如切换场景)时,SearchView扩展/折叠工作正常.我曾经遇到过同样的问题.
我想出了使用onPrepareOptionsMenu()来解决我的问题.
只需在每个片段及其内部实现onPrepareOptionsMenu(),调用onQueryTextChange(“”)作为查询字符串传递“”.它将诀窍建议片段容器活动,这个片段想要介意他自己的搜索业务.
或者,不确定,但是对容器活动的invalidateOptionsMenu()调用也可以派上用场.
如果有帮助,请告诉我.
编辑:
我的实施:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
SearchView searchView = new SearchView(getActivity());
searchView.setQueryHint("Search for Events");
searchView.setOnQueryTextListener(this);
menu.add(0, ACTION_SEARCH, 0, "Search")
.setIcon(R.drawable.ic_mailbox_search)
.setActionView(searchView)
.setOnActionExpandListener(menuExpandListener)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
onQueryTextChange("");
}
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
if (mAdapter != null)
mAdapter.getFilter().filter(newText);
return false;
}
标签:android,fragment,actionbarsherlock,searchview 来源: https://codeday.me/bug/20191008/1870069.html