Android在线直播系统源码实现简单的动态搜索

Android在线直播系统源码实现简单的动态搜索相关代码
一、addTextChangedListener
使用这种方式的思路简述就是,当监听到文本改变时,就用Handler post一个Runnable去做相应的改变,动态修改ListView的显示。
二、本文案例
1.介绍一下SearchView的一些方法

setIconified():设置搜索框直接展开显示。左侧有放大镜(在搜索框中) 右侧有叉叉 可以关闭搜索框

setIconifiedByDefault():设置搜索框直接展开显示。左侧有放大镜(在搜索框外) 右侧无X样式点击按钮 有输入内容后有X样式点击按钮 不能关闭搜索框

onActionViewExpanded():设置搜索框直接展开显示。左侧有无放大镜(在搜索框中) 右侧无叉叉 有输入内容后有X样式点击按钮, 不能关闭搜索框

setOnQueryTextListener():为 SearchView 中的用户操作设置侦听器。

setSubmitButtonEnabled():当查询非空时启用显示提交按钮。

setQueryHint():查询提示语句

2.准备数据
本案例使用一个String数组

private final String[] mStrings = Cheeses.sCheeseStrings;

 

3.初始化以及填充数据

mSearchView = (SearchView) findViewById(R.id.search_view);
mListView = (ListView) findViewById(R.id.list_view);
mListView.setAdapter(mAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1,
mStrings));
//设置是否可以通过键盘输入的字符来过滤掉不需要的选项,定位到需要的选项。
mListView.setTextFilterEnabled(true);
setupSearchView()

 

private void setupSearchView() {
//设置搜索框直接展开显示。左侧有放大镜(在搜索框中) 右侧有叉叉 可以关闭搜索框
//mSearchView.setIconified(false);
//设置搜索框直接展开显示。左侧有放大镜(在搜索框外) 右侧无叉叉 有输入内容后有叉叉 不能关闭搜索框
//mSearchView.setIconifiedByDefault(false);
//设置搜索框直接展开显示。左侧有无放大镜(在搜索框中) 右侧无叉叉 有输入内容后有叉叉 不能关闭搜索框
mSearchView.onActionViewExpanded();
//为 SearchView 中的用户操作设置侦听器。
mSearchView.setOnQueryTextListener(this);
//当查询非空时启用显示提交按钮。
mSearchView.setSubmitButtonEnabled(false);
//查询提示语句
mSearchView.setQueryHint(getString(R.string.cheese_hunt_hint));
}

 

4.在SearchView中用户输入字符时激发方法里写入简单逻辑

//用户输入字符时激发该方法
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
mListView.clearTextFilter();
} else {
mListView.setFilterText(newText.toString());
}
return true;
}

 

三、源码

JimengSearchView.java
public class JimengSearchView extends Activity implements SearchView.OnQueryTextListener {
private SearchView mSearchView;
private ListView mListView;
private ArrayAdapter<String> mAdapter;

private final String[] mStrings = Cheeses.sCheeseStrings;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

setContentView(R.layout.searchview_filter);

mSearchView = (SearchView) findViewById(R.id.search_view);
mListView = (ListView) findViewById(R.id.list_view);
mListView.setAdapter(mAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1,
mStrings));
//设置是否可以通过键盘输入的字符来过滤掉不需要的选项,定位到需要的选项。
mListView.setTextFilterEnabled(true);
setupSearchView();
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String str = (String)((TextView) view).getText();
Toast.makeText(JimengSearchView.this,str,Toast.LENGTH_SHORT).show();
}
});
}

private void setupSearchView() {
//设置搜索框直接展开显示。左侧有放大镜(在搜索框中) 右侧有叉叉 可以关闭搜索框
//mSearchView.setIconified(false);
//设置搜索框直接展开显示。左侧有放大镜(在搜索框外) 右侧无叉叉 有输入内容后有叉叉 不能关闭搜索框
//mSearchView.setIconifiedByDefault(false);
//设置搜索框直接展开显示。左侧有无放大镜(在搜索框中) 右侧无叉叉 有输入内容后有叉叉 不能关闭搜索框
mSearchView.onActionViewExpanded();
//为 SearchView 中的用户操作设置侦听器。
mSearchView.setOnQueryTextListener(this);
//当查询非空时启用显示提交按钮。
mSearchView.setSubmitButtonEnabled(false);
//查询提示语句
mSearchView.setQueryHint(getString(R.string.cheese_hunt_hint));
}
//用户输入字符时激发该方法
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
mListView.clearTextFilter();
} else {
mListView.setFilterText(newText.toString());
}
return true;
}

public boolean onQueryTextSubmit(String query) {
return false;
}
} 

 

布局文件:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>

</LinearLayout>

 

以上就是Android在线直播系统源码实现简单的动态搜索相关代码, 更多内容欢迎关注之后的文章

posted @ 2021-09-13 14:18  云豹科技-苏凌霄  阅读(78)  评论(0编辑  收藏  举报