我想实现实现搜索的最佳方式。 在我的Activity中,我有两个带有列表结果的选项卡,我在工具栏中添加了一个searchView,用户可以在其中键入他要查找的内容。 有没有办法在我的活动上方显示列表结果,如whatsapp中的结果?
下面是我的代码,但它基本上是一个searchView。
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_subs, menu); MenuItem searchItem = menu.findItem(R.id.action_search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){ @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { return false; } }); return true; }I want to implement the best way to implement a search. In my Activity I have two tabs with a list results and I have added a searchView in the toolbar where the user can type what he is looking for. is there a way to display a list result above my activity whit the results as in whatsapp?
Below is the code I have but its basically a searchView.
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_subs, menu); MenuItem searchItem = menu.findItem(R.id.action_search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){ @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { return false; } }); return true; }最满意答案
我使用这种依赖MaterialSearchView实现了它
步骤1
将依赖项添加到您的gradle
compile 'com.miguelcatalan:materialsearchview:1.4.0'第2步
将MaterialSearchView与工具栏一起添加到布局文件中
<!— Must be last for right layering display —> <FrameLayout android:id="@+id/toolbar_container" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/theme_primary" /> <com.miguelcatalan.materialsearchview.MaterialSearchView android:id="@+id/search_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> </FrameLayout>第3步
将搜索项添加到菜单文件中:
<item android:id="@+id/action_search" android:icon="@drawable/ic_action_action_search" android:orderInCategory="100" android:title="@string/abc_search_hint" app:showAsAction="always" />步骤4
添加在onCreateOptionsMenu中定义它:
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); MenuItem item = menu.findItem(R.id.action_search); searchView.setMenuItem(item); return true; }第5步
设置OnItemClickListener
MaterialSearchView searchView = (MaterialSearchView) findViewById(R.id.search_view); searchView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //Do some magic } });I have implemented it using this dependence MaterialSearchView
Step 1
Add the dependencies to your gradle
compile 'com.miguelcatalan:materialsearchview:1.4.0'Step 2
Add MaterialSearchView to your layout file along with the Toolbar
<!— Must be last for right layering display —> <FrameLayout android:id="@+id/toolbar_container" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/theme_primary" /> <com.miguelcatalan.materialsearchview.MaterialSearchView android:id="@+id/search_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> </FrameLayout>Step 3
Add the search item into the menu file:
<item android:id="@+id/action_search" android:icon="@drawable/ic_action_action_search" android:orderInCategory="100" android:title="@string/abc_search_hint" app:showAsAction="always" />Step 4
Add define it in the onCreateOptionsMenu:
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); MenuItem item = menu.findItem(R.id.action_search); searchView.setMenuItem(item); return true; }Step 5
Set OnItemClickListener
MaterialSearchView searchView = (MaterialSearchView) findViewById(R.id.search_view); searchView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //Do some magic } });更多推荐
发布评论