Android searchView和listview实现搜索

searchView是一个为用户提供输入搜索查询和提交请求给搜索提供者的用户界面部件,显示查询建议或结果列表!本篇介绍一下将searchView加入到自定义标题栏布局中,结合listview实现搜索,首先看一下自定义布局:
 
1.自定义标题栏布局:custom_action_bar_layout.xml

<span><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:background="#565758"
    android:orientation="horizontal" >

<!--
    iconifiedByDefault="false" //false:searchview初始即是展开的,true:searchview初始即是关闭的,仅显示一个放大镜
   
    queryHint="@string/title_search_hint" :默认显示的提示文字
   
      以上属性都可以在代码中通过方法:setIconifiedByDefault()和setQueryHint()直接设置

-->

<SearchView
        android:id="@+id/search_view"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:iconifiedByDefault="false"
        android:inputType="textFilter"
        android:queryHint="@string/title_search_hint" >
    </SearchView>

</LinearLayout></span>

2.主页布局:activity_main.xml :

<span><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" >

<ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#969696" />
</LinearLayout></span>

3. MainActivity.java

package com.example.searchview2;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;

public class MainActivity extends Activity implements
  SearchView.OnQueryTextListener {
 ListView listView;
 SearchView searchView;
 Object[] names;
 ArrayAdapter<String> adapter;
 ArrayList<String> mAllList = new ArrayList<String>();

@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initActionbar();
  names = loadData();
  listView = (ListView) findViewById(R.id.list);
  listView.setAdapter(new ArrayAdapter<Object>(getApplicationContext(),
    android.R.layout.simple_expandable_list_item_1, names));

listView.setTextFilterEnabled(true);
  searchView.setOnQueryTextListener(this);
  searchView.setSubmitButtonEnabled(false);
 }

public void initActionbar() {
  // 自定义标题栏
  getActionBar().setDisplayShowHomeEnabled(false);
  getActionBar().setDisplayShowTitleEnabled(false);
  getActionBar().setDisplayShowCustomEnabled(true);
  LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View mTitleView = mInflater.inflate(R.layout.custom_action_bar_layout,
    null);
  getActionBar().setCustomView(
    mTitleView,
    new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT,
      LayoutParams.WRAP_CONTENT));
  searchView = (SearchView) mTitleView.findViewById(R.id.search_view);
 }

public Object[] loadData() {
  mAllList.add("aa");
  mAllList.add("ddfa");
  mAllList.add("qw");
  mAllList.add("sd");
  mAllList.add("fd");
  mAllList.add("cf");
  mAllList.add("re");
  return mAllList.toArray();
 }

@Override
 public boolean onQueryTextChange(String newText) {
  if (TextUtils.isEmpty(newText)) {
   // Clear the text filter.
   listView.clearTextFilter();
  } else {
   // Sets the initial value for the text filter.
   listView.setFilterText(newText.toString());
  }
  return false;
 }

@Override
 public boolean onQueryTextSubmit(String query) {
  // TODO Auto-generated method stub
  return false;
 }
}

运行一下:

Android searchView和listview实现搜索

Android searchView和listview实现搜索

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/05f62ae2fcc77eb972ae134cb0aeb078.html