自定义 SearchView
效果:
代码:
SearchActivity.java
package com.jialianjia.bzw.activity;import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.annotation.SuppressLint;
import android.widget.ListView;
import android.widget.SearchView;
import com.jialianjia.bzw.BaseActivity;
import com.jialianjia.bzw.R;
import com.lidroid.xutils.ViewUtils;
import java.util.ArrayList;/*** 搜索* Created by Gxs on 2016/5/5.*/public class SearchActivity extends BaseActivity implements SearchView.OnQueryTextListener{private SearchView searchView;private ListView listView;private ArrayAdapter<String> arrayAdapter;private ArrayList<String> arrayList = new ArrayList<String>();private Object[] names;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_search);names = loadData();ViewUtils.inject(this);searchView = (SearchView) findViewById(R.id.searchView);listView = (ListView) findViewById(R.id.listView);listView.setAdapter(new ArrayAdapter<Object>(getApplicationContext(),android.R.layout.simple_expandable_list_item_1, names));searchView.setOnQueryTextListener(this);searchView.setSubmitButtonEnabled(false);}@Overridepublic boolean onQueryTextSubmit(String query) {return false;}@Overridepublic boolean onQueryTextChange(String newText) {Object[] obj = searchItem(newText);updateLayout(obj);return false;}public Object[] searchItem(String name) {ArrayList<String> mSearchList = new ArrayList<String>();for (int i = 0; i < arrayList.size(); i++) {int index = arrayList.get(i).indexOf(name);// 存在匹配的数据if (index != -1) {mSearchList.add(arrayList.get(i));}}return mSearchList.toArray();}// 更新数据public void updateLayout(Object[] obj) {listView.setAdapter(new ArrayAdapter<Object>(getApplicationContext(),android.R.layout.simple_expandable_list_item_1, obj));}// 测试数据public Object[] loadData() {arrayList.add("aaa");arrayList.add("aab");arrayList.add("aac");arrayList.add("aad");arrayList.add("abc");arrayList.add("abcd");arrayList.add("cdf");arrayList.add("eda");arrayList.add("sdfa");arrayList.add("ddda");arrayList.add("sssa");return arrayList.toArray();}
}
布局
activity_search.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><android.support.v7.widget.Toolbar
android:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="50dp"android:background="?attr/colorPrimary"android:theme="@style/AppTheme.AppBarOverlay"android:fitsSystemWindows="true" /><LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginTop="5dp"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"><SearchView
android:id="@+id/searchView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:iconifiedByDefault="false"android:background="@drawable/shape_search"android:queryHint="请输入您要查找的内容"/><Button
android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="返回"/></LinearLayout><TableRow
android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="right"></TableRow><ListView
android:id="@+id/listView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#969696"/></LinearLayout>
自定义 SearchView相关推荐
- 自定义SearchView的搜索提示
2019独角兽企业重金招聘Python工程师标准>>> 1. SearchRecentSuggestionsProvider 首先要创建自己的SearchRecentSuggesti ...
- 自定义searchview包括修改图标样式
2019独角兽企业重金招聘Python工程师标准>>> 做的很粗糙.为啥不能上传gif格式?csdn很强大. . 背景是自己定义的白色边框,直接定义一个xml.下面是更改图标和字 ...
- android 自定义searchview,自定义searchview包括修改图标样式
做的很粗糙.为啥不能上传gif格式?csdn很强大. . 背景是自己定义的白色边框,直接定义一个xml.下面是更改图标和字体颜色. 直接看代码: package com.example.liuxinq ...
- android 自定义searchview,android自定义view--SearchView
前言 上一篇Path特效功臣----PathMesure我们讲了PathMesure中api的详细方法和测试.本文就用我们学到的PathMeasure实现一个动态效果的SearchView,先瞄一下好 ...
- 自定义Toolbar的一些小技巧
目录 1.改变Toolbar高度(解决图标不垂直居中) 2.改变toolbar弹窗菜单样式 3.改变toolbar整体样式 4.改变searchview样式 1.改变Toolbar高度(解决图标不垂直 ...
- android仿网易云搜索,MaterialDesign之SearchView解锁 仿网易云音乐搜索
原标题:MaterialDesign之SearchView解锁 仿网易云音乐搜索 本文作者 本文由CSDN_LQR投稿 CSDN_LQR的博客地址: http://www.jianshu.com/u/ ...
- html字体播放,html-字体
字体大小 复习 /*字体可以写多个,优先使用前面的字体,如果前面的字体在浏览器解析时不存在,使用后面一个*/ .box { font-family: 华文彩云, 楷书; } .box1{ font-s ...
- Android编程示例:创建机场计划模拟器应用程序
在本文中,我们将演示如何使用Android Studio和Java编程语言创建一个示例Android应用程序,从"临时"实现高级响应用户界面的功能.本文中讨论的应用程序将实现机场航 ...
- android自定义搜索栏,Android自定义View实现搜索框(SearchView)功能
概述 在Android开发中,当系统数据项比较多时,常常会在app添加搜索功能,方便用户能快速获得需要的数据.搜索栏对于我们并不陌生,在许多app都能见到它,比如豌豆荚 在某些情况下,我们希望我们的自 ...
最新文章
- c语言格式化写入文件大小,利用C语言格式化读写文件
- 电网电压波形是否失真?
- mac u盘文件过大 拷贝不进去_使用mac时文件太大无法拷贝怎么办
- 《C++面向对象高效编程(第2版)》——2.16 识别成员函数的目标对象
- CSP认证202012-2	期末预测之最佳阈值[C++题解]:遍历、前缀和
- OpenCV学习笔记(一) 环境配置
- pyecharts第十节、地图(二、中国地图)
- C++学习之路 | PTA乙级—— 1017 A除以B (20分)(精简)
- Linux ulimit命令(控制shell执行程序的资源、并发)
- 若依的框架怎么样_若依框架的功能代码
- java生成三角网_源码:基于离散点的构TIN算法(三角网)
- Mac 系统 go-kit安装
- 打包deb时,实际上是两层包名目录的结构
- JMeter之压力测试
- 轻灵高效的WizTree树
- 2012考研数学二第(11)题——链式法则偏导数
- 春天里,阳光下,无限的哀思和想念
- 数据库的四种隔离级别及对应解决的脏读、不可重复读、幻读问题
- 企业/公司如何初创建一个小程序
- npm install 报错:no such file or directory, scandir '..\node-sass\vendor'