android的搜索功能

現在大部分的有關商城的列表或者其他方便的列表都會包含有搜索功能
如下圖:


image.png

我這里用的是SearcheditText

導入依賴:

 //搜索
    implementation 'com.wenwenwen888:searchedittext:1.0.0'

xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorBlue">

        <ImageView
            android:id="@+id/collection_back"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:padding="15dp"
            android:src="@mipmap/layout_back" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="我的收藏"
            android:textColor="@color/white"
            android:textSize="18sp" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#f1f1f1"
        android:padding="5dp"
        android:focusable="true"
        android:focusableInTouchMode="true">

        <com.wyt.searchedittext.SearchEditText
            android:id="@+id/searchEditText"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@drawable/search_edit_bg"
            android:drawableLeft="@mipmap/sousuoblack"
            android:drawablePadding="5dp"
            android:gravity="center_vertical"
            android:hint="搜索"
            android:imeOptions="actionNone"
            android:padding="5dp"
            android:singleLine="true"
            android:textSize="14sp"
            app:drawableDel="@mipmap/edit_delete"
            android:focusable="true"/>
    </RelativeLayout>

<--當list為空提示暫無數據,有的時候就隱藏-->
    <RelativeLayout
        android:id="@+id/error_view"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="暫無數據!"/>
    </RelativeLayout>

    <Recyclerview
        android:id="@+id/collectionview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp" />

</LinearLayout>

在activity中的使用


        searchEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) { }

            @Override
            public void afterTextChanged(Editable s) {

                   initData(s.toString());//獲取列表的接口方法,s.toString()就是你輸入的條件

            }
        });

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容