常用收藏Android告别老套篇

SearchLayout带历史记录的搜索框告别老套的谷歌原生Se

2020-10-13  本文已影响0人  千夜零一

引言

  很多时候我们发现原生的android控件无法满足我们的开发需求,于是很多自定义View,VieGroup横空出世,但你要知道自定义View的绘制过程“无比艰难”,让很多编程小白感觉老大难!所以最方便的方式,就是借助很多的开源框架,引入依赖使用各路大神封装好的组件,达到事半功倍的效果。
  谷歌原生的SearchView并不具备保存历史记录的功能,这需要下很大功夫去了解存储,有人可能想到了一些AndroidStudio中的原生数据库,或者数据存储方式,当然这是一种实现方法,但毋庸置疑很繁琐。本期我们安利的是一行代码带你起飞的github库——SearchLayout,它完美封装了你需要的对于SearchView的样式以及功能,可以满足很多开发应用情景。


效果预览

具有搜索记录的搜索框.gif

用法步骤

第一步:添加依赖app下build.gradle

//带历史记录的搜索框
implementation 'com.carson_ho:SearchLayout:1.0.1'

第二步:布局文件中使用控件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".blog.Case40"
    tools:ignore="MissingConstraints">

    <scut.carson_ho.searchview.SearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:textSizeSearch="5dp"
        app:textColorSearch="#3F51B5"
        app:textHintSearch="输入查询关键字"
        app:searchBlockHeight="150"
        app:searchBlockColor="#ffffff"
         />
</androidx.constraintlayout.widget.ConstraintLayout>

第三步:在Activity中书写逻辑代码(点击事件,搜索操作等)

// 搜索框(带历史记录)
public class Case40 extends AppCompatActivity {
    private SearchView searchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_case40);
        searchView = (SearchView)findViewById(R.id.search_view);
        //设置点击键盘上的搜索按键后的操作(通过回调接口)
        searchView.setOnClickSearch(new ICallBack() {
            @Override
            public void SearchAciton(String string) {
                Log.i("SearchAciton: ","我收到了" + string);
            }
        });
        //设置点击返回键后的操作(通过回调接口)
        searchView.setOnClickBack(new bCallBack() {
            @Override
            public void BackAciton() {
                finish();
            }
        });
    }
}

大功告成!

效果图

搜索框历史.jpeg
上一篇下一篇

猜你喜欢

热点阅读