Android 无需按下 Searchview 小部件即可使用

Android Searchview widget is coming without pressed it

// SearchFragment.java

 public class SearchFragment extends Fragment{
    Activity titleChange;
    SearchView searchView;
    public SearchFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.search_fragment, container, false);
        titleChange.setTitle("Search Fragment");

            searchView =(SearchView) view.findViewById(R.id.searchView);
            searchView.setQueryHint("Search View");
            searchView.setIconified(false);
            searchView.setSubmitButtonEnabled(true);
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

                @Override
                public boolean onQueryTextSubmit(String query) {
                    Toast.makeText(getContext(), query, Toast.LENGTH_LONG).show();
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                 //   Toast.makeText(getContext(), newText, Toast.LENGTH_LONG).show();
                    return false;
                }

            });
            searchView.setOnSearchClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getContext(), "opened", Toast.LENGTH_LONG).show();
                }
            });
            searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                //your code here
                Toast.makeText(getContext(), "closed", Toast.LENGTH_LONG).show();
                return false;
            }
            });

                return view;
    }

// search_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:orientation="vertical">

    <android.support.v7.widget.SearchView
        android:id="@+id/searchView"
        android:layout_width="wrap_content"
        android:background="#fff"
        android:layout_height="wrap_content">
    </android.support.v7.widget.SearchView>

</LinearLayout>

搜索视图默认触发,但我想在按下它时使用它

我还需要一个反压功能,如果当 searchview 打开时,它必须关闭,即使它是打开的。

所以你能帮我 Android 开发人员吗?

Screenshot

查看 SearchView documentation

在您的 onCreateView() 方法中,您正在调用 searchView.setIconified(false)。这 "opens" 搜索视图。将其替换为 searchView.setIconified(true).

关于你的第二个问题,请自己做一些研究尝试解决。然后提交第二个问题,解释您目前所做的尝试和结果。