如何在没有ActionBar的片段中实现SeachView

时间:2018-12-06 05:00:19

标签: android android-fragments searchview

我有BottomNavigationBar,在MainActivity中有3个片段。 MainActivity没有操作栏。

现在,我必须在第二个片段中实现SearchView。我需要类似WhatsApp的Searview。目前,我已经实现了它,但是当我单击searchview时,其EditText的高度和宽度未设置为match_parent

我还希望返回按钮将其折叠。我该如何实现?

这是我的第二个片段

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white"
android:layout_marginBottom="?attr/actionBarSize"
xmlns:shimmer="http://schemas.android.com/apk/res-auto"
tools:context=".StudentAttendance">

<android.support.v7.widget.Toolbar
    android:background="@drawable/bg_list_item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/txtHeader"
        android:text="Student Attendance"
        android:textSize="18sp"
        android:textColor="#767a8c"
        android:fontFamily="@font/circularstdbold"
        android:padding="18dp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <android.support.v7.widget.SearchView
        android:id="@+id/search_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </android.support.v7.widget.SearchView>
</android.support.v7.widget.Toolbar>



<com.facebook.shimmer.ShimmerFrameLayout
    android:id="@+id/shimmer_layout"
    shimmer:duration="80"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <include layout="@layout/placeholder_list"/>
        <include layout="@layout/placeholder_list"/>
        <include layout="@layout/placeholder_list"/>
        <include layout="@layout/placeholder_list"/>
        <include layout="@layout/placeholder_list"/>
        <include layout="@layout/placeholder_list"/>
        <include layout="@layout/placeholder_list"/>
    </LinearLayout>
</com.facebook.shimmer.ShimmerFrameLayout>

<LinearLayout
    android:id="@+id/fitall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"></LinearLayout>
  </LinearLayout>

代码

    final TextView txtHeader = (TextView) view.findViewById(R.id.txtHeader);
    final SearchView searchView = (SearchView) view.findViewById(R.id.search_bar);
    searchView.setQueryHint("Search");

    searchView.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            txtHeader.setVisibility(View.GONE);
        }
    });

     searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            //my code here for search
           return false;
        }
    });

2 个答案:

答案 0 :(得分:0)

只需尝试

  <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        app:popupTheme="@style/AppTheme.PopupOverlay">

    <RelativeLayout
            android:id="@+id/not"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    >
        <android.support.v7.widget.SearchView
                 android:layoutDirection="rtl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    </RelativeLayout>
</android.support.v7.widget.Toolbar>

您正在寻找这个吗?

Image

您将得到类似这样的信息。你想对吗?

答案 1 :(得分:0)

xml

<android.support.v7.widget.Toolbar
    android:background="@drawable/bg_list_item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/not"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <TextView
            android:id="@+id/txtHeader"
            android:text="Student Attendance"
            android:textSize="18sp"
            android:textColor="#767a8c"
            android:fontFamily="@font/circularstdbold"
            android:padding="18dp"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <android.support.v7.widget.SearchView
            android:id="@+id/search_view"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
</android.support.v7.widget.Toolbar>

代码

 final android.support.v7.widget.SearchView searchView = (SearchView) view.findViewById(R.id.search_view);

    searchView.setQueryHint("Search");
    searchView.setMaxWidth(Integer.MAX_VALUE);

    searchView.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            txtHeader.setVisibility(View.GONE);
        }
    });

    searchView.setOnCloseListener(new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {
            txtHeader.setVisibility(View.VISIBLE);
            return false;
        }
    });