android searchView加ListView共同使用

在一些app内,很常见上面一个edittext 下面是个listview的界面,在edittext中输入东西,就可以达到搜索当前listview的目的.现在我们首先使用searchview + listview的方式来实现这样的功能

主要就是使用了searchview的onQueryTextChange内容改变的时候监听 以及onQueryTextSubmit点击了searchview右侧的确定搜索按钮监听


listview主要用到的是,允许listview进行过滤lv_main.setTextFilterEnabled(true);

以及为listview添加过滤  lv_main.setFilterText(newText);// 设置过滤词组

lv_main.clearTextFilter();// 清除listview的过滤


自我感觉没有edittext + listview的方式好,个人推崇后者





xml:

<RelativeLayout 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"
    tools:context="com.example.testsearchview.MainActivity" >

    <SearchView
        android:id="@+id/searchView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </SearchView>

    <ListView
        android:id="@+id/lv_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/searchView1"
        android:layout_below="@+id/searchView1"
        android:background="#000"
        android:layout_marginTop="14dp" >
    </ListView>

</RelativeLayout>
activity:

package com.example.testsearchview;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity implements OnQueryTextListener {

	private SearchView searchView1;
	private ListView lv_main;
	private final String[] myString = new String[] { "aaaaa", "bbbbb", "ccccc",
			"ddddd" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		searchView1 = (SearchView) findViewById(R.id.searchView1);
		searchView1.setQueryHint("test search");

		//初始化listview 并丰富数据
		lv_main = (ListView) findViewById(R.id.lv_main);
		final ArrayAdapter<String> adapter = new ArrayAdapter<>(
				getApplicationContext(), android.R.layout.simple_list_item_1,
				myString);
		lv_main.setAdapter(adapter);
		lv_main.setTextFilterEnabled(true);

		//listview的点击事件
		lv_main.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getApplicationContext(),
						"点击了"+adapter.getItem(position), Toast.LENGTH_SHORT).show();

			}
		});

		searchView1.setIconifiedByDefault(false);
		searchView1.setOnQueryTextListener(this);
		searchView1.setSubmitButtonEnabled(true);
		searchView1.setQueryHint("搜索");
	}

	@Override
	public boolean onQueryTextSubmit(String query) {
		Toast.makeText(getApplicationContext(), "你点击了:" + query,
				Toast.LENGTH_SHORT).show();
		return false;
	}

	@Override
	public boolean onQueryTextChange(String newText) {
		if (TextUtils.isEmpty(newText)) {
			lv_main.clearTextFilter();// 清除listview的过滤
		} else {
			lv_main.setFilterText(newText);// 设置过滤词组
		}
		Toast.makeText(getApplicationContext(), newText, Toast.LENGTH_SHORT)
				.show();
		return true;
	}
}



你可能感兴趣的:(android searchView加ListView共同使用)