【问题标题】:Android: Making a Search Activity Single TopAndroid:使搜索活动成为单一顶部
【发布时间】:2018-10-01 21:37:52
【问题描述】:

我有一个Activity,我使用ListView 来显示一些数据,我还想使用SearchView 进行一些过滤,并且我想在相同的Activity 中显示过滤后的结果。

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);

        SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setSubmitButtonEnabled(true);

        return true;
    }

我正在正确地进行搜索,但我注意到每当我进行新的搜索过程时,都会创建一个新的 Activity 实例。因此,我想通过将android:launchMode="singleTop" 放入我的清单来启动Activity 作为singleTop,并尝试在onNewIntent 方法中处理搜索过程。那也没有用。 Activity 仍然会在我进行新搜索时创建。

我怎样才能避免这种情况?任何帮助将不胜感激。

【问题讨论】:

    标签: java android searchview android-search android-searchmanager


    【解决方案1】:

    我会试着举个例子。

    假设我们有一个布局文件activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" >
    
            </android.support.v7.widget.Toolbar>
    
        </android.support.design.widget.AppBarLayout>
    
        <include layout="@layout/content_main" />
    
        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/appbar_layout"></ListView>
    
    </RelativeLayout>
    

    menu_main.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context="com.prateek.search.MainActivity">
        <item
            android:id="@+id/action_settings"
            android:orderInCategory="100"
            android:title="@string/action_settings"
            app:showAsAction="never" />
    
        <item
            android:id="@+id/search"
            android:title="Search"
            app:actionViewClass="android.support.v7.widget.SearchView"
            app:showAsAction="ifRoom"/>
    </menu>
    

    这是 MainActivity.java 文件

    public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
    
        SearchView searchView;
        ListView listView;
        String[] values = {"John", "Jack", "Alfred", "Bruce", "Frank"};
        ArrayAdapter<String> adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            listView = (ListView) findViewById(R.id.listView);
    
            adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
            listView.setAdapter(adapter);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
            searchView = (SearchView) menu.findItem(R.id.search).getActionView();
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            searchView.setSubmitButtonEnabled(true);
            searchView.setOnQueryTextListener(this);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }
    
        @Override
        public boolean onQueryTextChange(String newText) {
            String text = newText;
            adapter.getFilter().filter(text);
            return false;
        }
    }
    

    当您搜索任何元素时,过滤后的结果应显示在同一活动中。希望对您有所帮助!

    styles.xml 文件

    <resources>
    
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>
    
        <style name="AppTheme.NoActionBar">
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</item>
        </style>
    
        <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    
        <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
    
    </resources>
    

    【讨论】:

    • 抱歉,我忘了说我希望 SearchView 成为 ActionBar 的一部分
    • 错误:找不到资源样式/AppTheme.PopupOverlay
    • 你需要稍微改变你的styles.xml。我已经更新了答案。从 styles.xml 文件中复制必要的行(无论您需要什么)。
    猜你喜欢
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    相关资源
    最近更新 更多