美文网首页
android的搜索功能

android的搜索功能

作者: 心中有梦丶身边有你 | 来源:发表于2019-11-15 11:30 被阅读0次

现在大部分的有关商城的列表或者其他方便的列表都会包含有搜索功能
如下图:


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()就是你输入的条件

            }
        });

相关文章

网友评论

      本文标题:android的搜索功能

      本文链接:https://www.haomeiwen.com/subject/zlijictx.html