如何设置MenuItemClickListener要搜索查看在辅助工具栏 [英] How to set the MenuItemClickListener for a SearchView at a secondary Toolbar

查看:504
本文介绍了如何设置MenuItemClickListener要搜索查看在辅助工具栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图几个小时设置在​​底部(二级)android.support.v7.widget.ToolBar简单android.support.v7.widget.SearchView。
我已经使用另一个工具栏往上顶设置为一个动作条,所以我不能用在活动 onCreateOptionsMenu 回调,相反,我必须建立一个 OnMenuItemClickListener()。我已阅读 Android的搜索查看教程,的本教程了解工具栏。

i am trying for hours to setup a simple android.support.v7.widget.SearchView in a bottom (secondary) android.support.v7.widget.ToolBar. I am already using another Toolbar up top set up as an ActionBar, so i cannot use the onCreateOptionsMenu callback on activity, instead i have to set up an OnMenuItemClickListener() . I have read the Android SearchView Tutorial, this tutorial about Toolbars.

相关code,
简化HomepageActivity

Related Code, simplified HomepageActivity

package something.somethingelse.activities;

import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.OnCloseListener;
import android.support.v7.widget.SearchView.OnQueryTextListener;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;


public class HomepageActivity extends AppCompatActivity {

protected static final String TAG = "HomepageActivity";
private Toolbar mTopToolbar;
private Toolbar mBottomToolbar;
private FloatingActionButton mFab;
private Context mContext;

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mTopToolbar = (Toolbar) findViewById(R.id.topToolbar);
    setSupportActionBar(mTopToolbar);


    mBottomToolbar = (Toolbar) findViewById(R.id.bottomToolbar);

    mBottomToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {

            SearchManager searchManager = (SearchManager) HomepageActivity.this.getSystemService(Context.SEARCH_SERVICE);
            SearchView searchView = null;

            if (menuItem != null) {
                Log.d(TAG, "menuItem != null");
                searchView = (SearchView) menuItem.getActionView();
            }

            if (searchView != null) {
                Log.d(TAG, "searchView != null");
                searchView.setSearchableInfo(searchManager.getSearchableInfo(HomepageActivity.this.getComponentName()));
            }

            searchView.setOnCloseListener(new OnCloseListener() {

                @Override
                public boolean onClose() {
                    mBottomToolbar.setVisibility(View.GONE);
                    return false;
                }
            });

            searchView.setOnQueryTextListener(new OnQueryTextListener() {

                @Override
                public boolean onQueryTextSubmit(String arg0) {
                    Log.d(TAG, "Submited: "+arg0);
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String arg0) {
                    Log.d(TAG, "Changed: "+arg0);
                    return false;
                }
            });
            return false;
        }
    });

    mBottomToolbar.inflateMenu(R.menu.bottom_toolbar);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.action_bar, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items

    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

简化首页布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:guide="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v7.widget.Toolbar
    android:id="@+id/topToolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@color/white"
    guide:elevation="4dp"
    android:minHeight="?attr/actionBarSize"
    guide:theme="@style/Toolbar_Theme" >

    <ImageView
        android:id="@+id/ivAb_logo"
        android:layout_width="wrap_content"
        android:layout_height="?attr/actionBarSize"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="58dp"
        android:paddingTop="10dp"
        android:src="@drawable/generic_logo" />
</android.support.v7.widget.Toolbar>

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/topToolbar" >

    <RelativeLayout
        android:id="@+id/homepage_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/homepage_background"
        android:orientation="vertical" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/bottomToolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/primaryDark"
            guide:elevation="4dp" />


    </RelativeLayout>

    <!-- The navigation drawer -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="220dp"
        android:layout_height="fill_parent"
        android:layout_gravity="start"
        android:background="#60BBBBBB"
        android:choiceMode="singleChoice"
        android:divider="#FFFFFF"
        android:dividerHeight="1px" />
</android.support.v4.widget.DrawerLayout>

菜单bottom_toolbar.xml

menu bottom_toolbar.xml

<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:guide="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/action_search"
    android:title="@string/search"
    android:icon="@drawable/ic_search_white"
    guide:actionViewClass="android.support.v7.widget.SearchView"
    guide:showAsAction="always">
 </item>

searchable.xml

searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search"
android:label="@string/app_name" 
/>

AndroidManifest.xml中相关的部分。

AndroidManifest.xml related part

<activity
        android:name="something.somethingelse.activities.HomepageActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.Default" >
        <intent-filter>
            <action     android:name="something.somethingelse.activities.HomepageActivity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

如果我喜欢的运行它,我碰到一个搜索查看,但没有我的安卓图标=@绘制/ ic_search_white,而不是我得到的谷歌搜索查看图标,如果我点击它,我得到一个很好的编辑文本,明确和搜索功能,但无论怎样我输入或preSS,里面的Log.d没有在 Toolbar.OnMenuItemClickListener 被解雇了。 (我不能发布,因为口碑的图像)。
如果我使用的android:actionViewClass =android.support.v7.widget.SearchView而不是指南:actionViewClass =android.support.v7。 widget.SearchView让我的安卓图标=@绘制/ ic_search_white,如果我点击它,我得到上NPE searchView.setOnCloseListener(新OnCloseListener(){,因为搜索查看为null。

If i run it like that, i get a SearchView but without my android:icon="@drawable/ic_search_white", instead i get the google SearchView icon, if i tap it i get a nice edit text, with clear and search functions, but no matter what i input or press, NONE of the Log.d inside the Toolbar.OnMenuItemClickListenergets fired up. (I cannot post images because of reputation). If i use android:actionViewClass="android.support.v7.widget.SearchView" instead of guide:actionViewClass="android.support.v7.widget.SearchView" i get my android:icon="@drawable/ic_search_white" , if i tap it, i get an NPE on searchView.setOnCloseListener(new OnCloseListener() { because searchView is null .

我试着做什么在这里这个的相关问题, <一href=\"http://stackoverflow.com/questions/18832890/android-nullpointerexception-on-searchview-in-action-bar\">this但没有成功为止。

I tried doing whats on this related question here, and this but no success so far.

推荐答案

更新23/09/15
我找不到什么好的解决办法,所以,因为我只是想获得搜索查看项目,我决定潜入它像这样:

Updated 23/09/15 I could not find any good solution, so, since i only wanted to get the SearchView item i decided to hack into it like so:

protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(savedInstanceState==null){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.dlHomepageSpace, HomepageFragment.newInstance(mLanguage), HOMEPAGE_BUTTONS_TAG);
        fragmentTransaction.commit();
    }

    mSearchState = SearchState.NOSEARCH;

    mBottomToolbar = (Toolbar) findViewById(R.id.bottomToolbar);
    mBottomToolbar.inflateMenu(R.menu.bottom_toolbar);

    // Here we set the search view toolbar functions
    if(mBottomToolbar.getMenu().size() > 0){

        SearchManager searchManager = (SearchManager) HomepageActivity.this.getSystemService(Context.SEARCH_SERVICE);
        MenuItem searchMenuItem = mBottomToolbar.getMenu().getItem(mBottomToolbar.getMenu().size()-1);
        mSearchView = (SearchView) searchMenuItem.getActionView();
        if (mSearchView != null) {
            mSearchView.setSearchableInfo(searchManager.getSearchableInfo(HomepageActivity.this.getComponentName()));
        }

        mSearchView.setOnCloseListener(new OnCloseListener() {

            @Override
            public boolean onClose() {
                Log.d(TAG, "onClose");
                   mBottomToolbar.getMenu().getItem(mBottomToolbar.getMenu().size()-1).collapseActionView();
                switchToFab();
                return false;
            }
        });

        MenuItemCompat.setOnActionExpandListener(searchMenuItem, new       MenuItemCompat.OnActionExpandListener() {

            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                Log.d(TAG, "onMenuItemActionExpand");
                return true; // KEEP IT TO TRUE OR IT DOESN'T OPEN !!
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                Log.d(TAG, "onMenuItemActionCollapse");
                switchToFab();
                return true;
            }
        });
    }

}

@Override
    protected void onNewIntent(Intent intent) {
    Log.d(TAG, "onNewIntent");
    if(isSearchAction(intent)){

        ArrayList<Detail> list = (ArrayList<Detail>) getListData(intent.getStringExtra(SearchManager.QUERY));

        if(list.isEmpty()){
                Toast.makeText(this, R.string.no_search_results_en, Toast.LENGTH_LONG).show();

            }
        } else {

            ExtendedListFragment searchFragment = (ExtendedListFragment) getSupportFragmentManager().findFragmentByTag(HOMEPAGE_SEARCH_TAG);
            mResultsList = list;
            mSearchState = SearchState.SEARCH_LIST_SCREEN;

            if(searchFragment==null){
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.addToBackStack(null);

                fragmentTransaction.replace(R.id.dlHomepageSpace, ExtendedListFragment.newInstance(list), HOMEPAGE_SEARCH_TAG);
                fragmentTransaction.commit();
            } else {
                searchFragment.setDetailList(list);
            }
        }
    }
}

private boolean isSearchAction(Intent intent) {
    Log.d(TAG, "handleIntent");
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        Log.d(TAG, "ACTION_SEARCH");
        return true;
    }
    return false;
}

在本质上,我只看到工具栏,并获得独一无二的项目进去,我投它作为一个菜单项,然后我得到的搜索查看的菜单项的而ActionView。
喜欢它的工作原理和我没有使用 setOnMenuItemClickListener()。当我在视图中轻点,我可以输入进去(我不需要 mSearchView.OnQueryTextListener 不过,我已经看到了它的工作),当我$ P $的PSS键盘我得到的 onNewIntent()调用工作放大镜。

In essence, i just look into the toolbar, and obtain the one and only Item into it, i cast it as a MenuItem and then i get the SearchView as the ActionView of the MenuItem. Like that it works and i do not have to use setOnMenuItemClickListener() . When i tap in the view, i can type into it (i do not need a mSearchView.OnQueryTextListener but, i have seen it working) , when i press the magnifying glass on the keyboard i get the onNewIntent() call working.

这篇关于如何设置MenuItemClickListener要搜索查看在辅助工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆