我想在片段中添加搜索属性

时间:2019-09-13 16:26:20

标签: android

我是Android新手,我想向片段添加一个搜索属性,并执行了以下代码,我已经运行了它,并且在logcat中未显示任何错误,但未显示“搜索”图标。我在片段中的工具栏上设置了“ action_bar”(请参考代码)。 请帮我。 非常感谢您的帮助。

package com.example.myapplication;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;



public class CategoryFragment extends Fragment {
private Toolbar toolbar;
 ListView listView;
 ArrayAdapter mAdapter;
 SearchView searchView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View v= inflater.inflate(R.layout.fragment_category, container, false);
    listView=v.findViewById(R.id.list);
    toolbar=v.findViewById(R.id.tool);
    ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
    searchView=v.findViewById(R.id.item_search);

    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


    toolbar.setTitle("Category");

    mAdapter = new ArrayAdapter<String>(getContext(),
            android.R.layout.simple_list_item_1,
            getResources().getStringArray(R.array.category));
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = new Intent(getContext(), cate_list.class);
            intent.putExtra("SongName", listView.getItemAtPosition(i).toString());
            startActivity(intent);
        }
    });
    listView.setAdapter(mAdapter);
}

@Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu, menu);
    MenuItem mSearch = menu.findItem(R.id.item_search);
    SearchView mSearchView = (SearchView) mSearch.getActionView();
    mSearchView.setQueryHint("Search your lyrics");
    mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }
        @Override
        public boolean onQueryTextChange(String newText) {
            mAdapter.getFilter().filter(newText);
            return true;
        }
    });
}
}

1 个答案:

答案 0 :(得分:0)

您需要在代码中添加以下几行(在Kotlin中,一旦您掌握了逻辑,就可以用Java编写它了。)

override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setHasOptionsMenu(true) }

override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
return if (id == R.id.action_search) {
true
} else super.onOptionsItemSelected(item) }