Java Searchview侦听器不处理文本更改
这是应用程序Im构建的主屏幕,我有一个基于json的CardView列表,其中显示了一些内容。我想做的是,当用户用放大镜点击第一张卡片时,我希望使用searchview启动搜索,但当我点击该卡片时,它只是初始化搜索视图属性,但无法触发文本更改的侦听器。我已经创建了一个过滤器类,它应该遍历列表,但没有任何运气。我已经注释掉了列表适配器,并将其放入日志语句中,但从未访问过此代码Java Searchview侦听器不处理文本更改,java,android,listener,searchview,Java,Android,Listener,Searchview,这是应用程序Im构建的主屏幕,我有一个基于json的CardView列表,其中显示了一些内容。我想做的是,当用户用放大镜点击第一张卡片时,我希望使用searchview启动搜索,但当我点击该卡片时,它只是初始化搜索视图属性,但无法触发文本更改的侦听器。我已经创建了一个过滤器类,它应该遍历列表,但没有任何运气。我已经注释掉了列表适配器,并将其放入日志语句中,但从未访问过此代码 public class HomeScreen extends AppCompatActivity { Sear
public class HomeScreen extends AppCompatActivity {
SearchView searchView;
private static final String TAG = "HomeScreen";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
//tabLayout.addTab(tabLayout.newTab().setText("Search"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final DreamsFragmentAdapter adapter = new DreamsFragmentAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Log.v(TAG,"position: "+tab.getPosition());
if(tab.getPosition()==0){
}
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
public void SearchButton(View view){
Log.v(TAG,"search card pressed");
setupSearchView();
this.onSearchRequested();
}
public void openGallery(View view) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
public void startService(View view){
startActivityForResult(new Intent(Settings.ACTION_DREAM_SETTINGS),0);
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public boolean onCreateOptionsMenu(Menu menu){
return false;
}
private void setupSearchView(){
Log.v(TAG,"searchViewCreated");
SearchManager searchManager= (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if(searchManager !=null){
List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch();
SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
for (SearchableInfo inf : searchables) {
if (inf.getSuggestAuthority() != null
&& inf.getSuggestAuthority().startsWith("applications")) {
info = inf;
}
}
searchView = new SearchView(this);
searchView.setSubmitButtonEnabled(false);
searchView.setSearchableInfo(info);
}
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
//Search Results activity
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if(newText.length() > 0){
Log.v(TAG,"Text box changed");
//ListAdapter.getFilter().filter(newText);
}
return false;
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
return false;
}
});
}
}
公共类主屏幕扩展应用程序活动{
搜索视图搜索视图;
私有静态最终字符串标记=“主屏幕”;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局主屏幕);
TabLayout TabLayout=(TabLayout)findViewById(R.id.tab_布局);
tabLayout.addTab(tabLayout.newTab().setText(“tab1”));
//tabLayout.addTab(tabLayout.newTab().setText(“搜索”));
tabLayout.addTab(tabLayout.newTab().setText(“tab2”));
tabLayout.addTab(tabLayout.newTab().setText(“Tab 3”));
tabLayout.setTabGravity(tabLayout.GRAVITY\u-FILL);
final ViewPager ViewPager=(ViewPager)findViewById(R.id.pager);
最终的DreamsFragmentAdapter=新的DreamsFragmentAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(适配器);
viewPager.addOnPageChangeListener(新建TabLayout.TabLayoutOnPageChangeListener(TabLayout));
tabLayout.setOnTabSelectedListener(新的tabLayout.OnTabSelectedListener(){
@凌驾
已选择的公共选项卡(TabLayout.Tab){
Log.v(标记“position:+tab.getPosition());
如果(tab.getPosition()==0){
}
setCurrentItem(tab.getPosition());
}
@凌驾
已选择的公共选项卡(TabLayout.Tab){
}
@凌驾
已重新选择公共选项卡(TabLayout.Tab){
}
});
}
公共无效搜索按钮(视图){
Log.v(标签“按下搜索卡”);
setupSearchView();
this.onSearchRequested();
}
公共void openGallery(视图){
Intent Intent=新Intent(Intent.ACTION\u PICK,android.provider.MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI);
startActivityForResult(意向,0);
}
公共作废startService(视图){
startActivityForResult(新意图(设置、行动、梦想、设置),0);
}
公共布尔值onOptionsItemSelected(菜单项项){
int id=item.getItemId();
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
公共布尔onCreateOptions菜单(菜单){
返回false;
}
私有void setupSearchView(){
Log.v(标记“searchViewCreated”);
SearchManager SearchManager=(SearchManager)getSystemService(Context.SEARCH\u服务);
if(searchManager!=null){
List searchables=searchManager.getSearchablesInGlobalSearch();
SearchableInfo=searchManager.getSearchableInfo(getComponentName());
for(SearchableInfo-inf:searchables){
如果(inf.getSuggestAuthority()!=null
&&inf.getSuggestAuthority().startsWith(“应用程序”)){
info=inf;
}
}
searchView=新的searchView(此);
searchView.setSubmitButtonEnabled(false);
searchView.setSearchableInfo(信息);
}
searchView.setOnQueryTextListener(新的searchView.OnQueryTextListener(){
//搜索结果活动
@凌驾
公共布尔值onQueryTextSubmit(字符串查询){
返回false;
}
@凌驾
公共布尔onQueryTextChange(字符串newText){
如果(newText.length()>0){
Log.v(标记“文本框已更改”);
//ListAdapter.getFilter().filter(新文本);
}
返回false;
}
});
searchView.setOnCloseListener(新的searchView.OnCloseListener(){
@凌驾
公共布尔onClose(){
返回false;
}
});
}
}
表1的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:id="@+id/linearlayout">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:onClick="SearchButton"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageSearchView"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:background="@drawable/action_search_black"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textTitle"
android:layout_alignParentTop="true"
android:textSize="10sp"
android:textStyle="normal|bold"
android:layout_toRightOf="@+id/imageView"
android:textColor="@color/abc_input_method_navigation_guard" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textArticle"
android:layout_toRightOf="@+id/imageView"
android:layout_below="@+id/textTitle"
android:textSize="10sp"
android:textColor="@color/abc_input_method_navigation_guard" />
</RelativeLayout>
</android.support.v7.widget.CardView>
1。创建一个可搜索的配置:首先需要一个名为可搜索配置的XML
文件。该文件通常命名为searchable.xml
,必须保存在res/xml/
项目目录中
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_label"
android:hint="@string/search_hint" >
</searchable>
<application ... >
<activity android:name=".HomeScreen" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
................
.......................
</application>
用户错误我同时使用搜索对话框和搜索小部件。单击搜索按钮后,我使用以下方法设置搜索对话框
this.onSearchRequested();
然而,我的实现是针对搜索视图的,侦听器连接到了搜索视图,而不是搜索对话框 我已经在我的android清单中指定了这一点,并创建了可搜索的XML,但我仍然不明白为什么没有启动搜索侦听器。