Skip to content

Repository files navigation

License

CardSearchView

本项目是受BilibiliSearchView的启发,因此想对其进行完善,打包成库,方便自己以后的使用,也方便大家的快速导入。 在此,感谢原作者

预览

版本更新说明

版本更新说明

获取

先在 build.gradle 的 repositories 添加:

allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}

再在dependencies添加:

dependencies {
implementation 'com.github.limuyang2:CardSearchView:1.1.0.1'
}

使用

在根布局中添加以下代码:

xmlns:app="http://schemas.android.com/apk/res-auto"

在布局中使用:

 <km.lmy.searchview.SearchView
android:id="@+id/searchView"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="4dp"app:cardBackgroundColor="#fff"app:cardCornerRadius="5dp"app:cardElevation="4dp"app:defaultState="open"app:hintText="input name" />

提示

SearchView继承于系统控件CardView,所以可直接使用CardView的属性,例如:
设置背景色(app:cardBackgroundColor)、圆角(app:cardCornerRadius)等等

xml设置属性说明

xml属性类型说明
app:hintTextString输入框提示文本
app:backIconDrawable返回按钮图标
app:oneKeyCleanIconDrawable一键清除图标
app:historyIconDrawable历史纪录左侧图标
app:defaultStateenum控件初始状态是否处于打开状态(默认“close”),值:“open”、“close”
app:historyTextColorColor Int历史纪录文字颜色
app:inputView_heightInt(dp)搜索输入框的高度

Java方法设置属性说明

属性方法传入数据类型说明
setHintTextString输入框提示文本
setBackIconint返回按钮图标
setOneKeyCleanIconint一键清除图标
setHistoryIconint历史纪录左侧图标
defaultStateint控件初始状态是否处于打开状态(默认关闭) SearchView.CLOSE:关闭;SearchView.OPEN:打开
setHistoryTextColorint历史纪录文字颜色
open打开SearchView
close关闭SearchView
autoOpenOrClose自动打开关闭SearchView
isOpen搜索框是否打开
setOneKeyCleanIsVisibleboolean设置一键清除按钮是否显示(true:显示;false:不现实(默认为true))
setSearchEditTextString设置搜索框初始文本
setSearchEditTextEnabledboolean设置输入框处于是否可编辑状态
getEditTextView获取搜索输入框控件
setSearchInputViewHeightint设置搜索输入框的高度

Java方法使用说明

使用方法传入数据类型说明
addOneHistoryString添加一条历史纪录
addHistoryListList添加历史纪录列表
setNewHistoryListList设置全新的历史记录列表
setHistoryItemClickListenerOnHistoryItemClickListener设置历史纪录点击事件
setOnSearchActionListenerOnSearchActionListener设置软键盘搜索按钮点击事件
setOnInputTextChangeListenerOnInputTextChangeListener设置输入文本监听事件
setOnSearchBackIconClickListenerOnSearchBackIconClickListener自定义返回按钮点击监听事件(默认为关闭搜索框)
setOnCleanHistoryClickListenerOnCleanHistoryClickListener设置"清除历史"点击监听

示例参考

MainActivity.java

与AppBarLayout、ToolBar一起使用不显示?

原因

如下布局所示,当在Toolbar外嵌套AppBarLayout后,会有好看的阴影效果,这是在Android5.0后新加入的Material Design效果,强调布局的层级性。由于AppBarLayout会提高布局的Z轴坐标,产生层级,因此,在ToolBar上的其他控件会因为层级低于AppBarLayout而导致被遮盖。

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="@+id/activity_toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:titleTextColor="@android:color/white">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

解决办法如下

只需要将SearchView的层级提高到和AppBarLayout一样即可。在SearchView的XML中加入下面一句话即可。

app:cardElevation="4dp"

示例

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="@+id/activity_toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:titleTextColor="@android:color/white">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<km.lmy.searchview.SearchView
app:cardElevation="4dp"android:id="@+id/searchView"android:layout_width="match_parent"android:layout_height="wrap_content"/>

License

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

🔍一个自定义搜索框

Resources

Stars

420 stars

Watchers

3 watching

Forks

Releases

Packages

Contributors

Languages