这是我正在构建的应用程序的主屏幕,我有一个基于json的cardview列表,显示了一些内容。我想要做的是当用户用放大镜点击第一张卡片时,我希望使用searchview开始搜索,但是当我点击该卡片时它只是初始化搜索视图属性但是无法触发听众文字改变。我创建了一个过滤器类,它应该通过列表,但没有运气。我已经注释掉了列表适配器并输入了一个日志语句,但是从未达到此代码。
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;
}
});
}
}
标签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>
答案 0 :(得分:0)
<强> 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>
<强> 2。声明可搜索的活动:在AndroidManifest.xml
中,在HomeScreen
元素中声明ACTION_SEARCH
活动以接受<intent-filter>
意图并指定searchable configuration
在<meta-data>
元素中使用。
<强>的AndroidManifest.xml 强>
<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>
答案 1 :(得分:0)
用户错误我正在使用搜索对话和searchwidget。点击搜索按钮后,用以下方法设置搜索对话
this.onSearchRequested();
但是我的实现是针对搜索视图,监听器连接到searchview而不是搜索对话。