问题描述
我想用 viewPager 中的多个片段来实现 SearchView.所有片段都包含列表,我想过滤这些列表并创建一个新的 ListView,它根据它所属的片段对结果进行分类.
但我的 searchView 无法处理一个片段.
这是我的主要活动:
包com.codeon.directory;导入android.app.SearchManager;导入android.content.Context;导入android.os.Bundle;导入 android.support.design.widget.TabLayout;导入android.support.v4.app.Fragment;导入 android.support.v4.app.FragmentManager;导入 android.support.v4.app.FragmentPagerAdapter;导入 android.support.v4.view.ViewPager;导入android.support.v7.app.AppCompatActivity;导入 android.support.v7.widget.SearchView;导入android.support.v7.widget.Toolbar;导入 android.view.Menu;导入 android.view.MenuInflater;导入android.view.View;导入 com.codeon.directory.fragments.FiveFragment;导入 com.codeon.directory.fragments.FourFragment;导入 com.codeon.directory.fragments.OneFragment;导入 com.codeon.directory.fragments.SixFragment;导入 com.codeon.directory.fragments.ThreeFragment;导入 com.codeon.directory.fragments.TwoFragment;导入 java.util.ArrayList;导入 java.util.List;/*** 由 Nikhil Jain 于 2015 年 9 月 21 日创建.*/公共类 TabEffect 扩展 AppCompatActivity {私有 TabLayout tabLayout;私人 ViewPager viewPager;公共字符串 pic[] = {"1","2","3","1","2","3","1","2","3","1","2","3","1","2","3"};公共字符串状态[] = {"A","B","C"};公共字符串城市[] = {"P","Q","R"};@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.tablayout);工具栏工具栏 = (Toolbar) findViewById(R.id.toolbar);setSupportActionBar(工具栏);getSupportActionBar().setDisplayHomeAsUpEnabled(true);getSupportActionBar().setDisplayShowHomeEnabled(true);getSupportActionBar().setHomeButtonEnabled(true);toolbar.setNavigationOnClickListener(new View.OnClickListener() {@覆盖公共无效 onClick(查看 v){onBackPressed();}});viewPager = (ViewPager) findViewById(R.id.viewpager);setupViewPager(viewPager);tabLayout = (TabLayout) findViewById(R.id.tabs);tabLayout.setupWithViewPager(viewPager);}@覆盖公共布尔 onCreateOptionsMenu(菜单菜单){MenuInflater inflater = getMenuInflater();inflater.inflate(R.menu.tab,菜单);//将可搜索配置与 SearchView 关联搜索管理器搜索管理器 =(SearchManager) getSystemService(Context.SEARCH_SERVICE);搜索视图搜索视图 =(SearchView) menu.findItem(R.id.action_search).getActionView();searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));返回真;}私人无效 setupViewPager(ViewPager viewPager) {ViewPagerAdapter 适配器 = new ViewPagerAdapter(getSupportFragmentManager());捆绑捆绑 = 新捆绑();bundle.putStringArray("pic", pic);bundle.putStringArray("区", 城市);bundle.putStringArray("state", state);adapter.addFragment(new OneFragment(), "ONE FRAGMENT", bundle);adapter.addFragment(new TwoFragment(), "TWO FRAGMENT", bundle);adapter.addFragment(new ThreeFragment(), "三个片段", bundle);adapter.addFragment(new FourFragment(), "FOUR FRAGMENT", bundle);adapter.addFragment(new FiveFragment(), "FIVE FRAGMENT", bundle);adapter.addFragment(new SixFragment(), "SIX FRAGMENT", bundle);viewPager.setAdapter(适配器);}类 ViewPagerAdapter 扩展 FragmentPagerAdapter {私有最终列表<片段>mFragmentList = new ArrayList<>();私有最终列表<字符串>mFragmentTitleList = new ArrayList<>();公共 ViewPagerAdapter(FragmentManager 管理器) {超级(经理);}@覆盖公共片段getItem(int位置){返回 mFragmentList.get(位置);}@覆盖公共 int getCount() {返回 mFragmentList.size();}公共无效addFragment(片段片段,字符串标题,捆绑参数){mFragmentList.add(片段);mFragmentTitleList.add(title);片段.setArguments(args);}@覆盖公共 CharSequence getPageTitle(int position) {返回 mFragmentTitleList.get(位置);}}}
这是我的第一个片段:
包com.codeon.directory.fragments;导入android.os.Bundle;导入android.support.v4.app.Fragment;导入 android.support.v7.widget.LinearLayoutManager;导入 android.support.v7.widget.RecyclerView;导入android.util.Log;导入 android.view.LayoutInflater;导入android.view.View;导入android.view.ViewGroup;导入 android.widget.ArrayAdapter;导入 android.widget.ListView;导入 com.codeon.directory.R;导入 com.codeon.directory.RecyclerAdapter;导入 com.codeon.directory.customadapters.Listitems_new;导入 java.util.ArrayList;/*** 由 Nikhil Jain 于 2015 年 9 月 21 日创建.*/公共类 OneFragment 扩展片段 {ListView 列表;私有 ArrayAdapter<String>列表适配器;公共 OneFragment() {//必需的空公共构造函数}@覆盖公共无效 onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@覆盖public View onCreateView(LayoutInflater inflater, ViewGroup 容器,捆绑保存的InstanceState){RecyclerView rootView = (RecyclerView) inflater.inflate(R.layout.fragment_one, container, false);//膨胀这个片段的布局捆绑附加服务 = getArguments();String pic[] = extras.getStringArray("pic");字符串状态[] = extras.getStringArray("state");Log.e("值",pic[0]+pic[1]+pic[2]);rootView.setLayoutManager(new LinearLayoutManager(rootView.getContext()));//listAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, pic);rootView.setAdapter(new RecyclerAdapter(pic));返回根视图;}}
我的搜索活动:
包com.codeon.directory;导入android.app.Activity;导入android.app.SearchManager;导入android.content.Intent;导入android.os.Bundle;导入android.util.Log;导入 android.widget.Toast;/*** 由 Nikhil Jain 于 2015 年 9 月 21 日创建.*/公共类 SearchActivity 扩展 Activity {@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);设置内容视图(R.layout.search);//获取意图,验证操作并获取查询意图意图 = getIntent();if (Intent.ACTION_SEARCH.equals(intent.getAction())) {字符串查询 = intent.getStringExtra(SearchManager.QUERY);doMySearch(查询);Log.e("QUERY", 查询);}}私人无效doMySearch(字符串查询){}}
这是我的 AndroidManifest.xml:
解决方案 @Chintan 是对的.我们需要使用 ViewModel.关注这个.
- 创建 SearchViewModel:
'''
private MutableLiveData查询=新的 MutableLiveData<String>();公共无效 setQuery(字符串查询数据){query.setValue(queryData);}公共 LiveData<String>获取查询(){返回查询;}
'''
- 搜索父片段
'''
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {inflater.inflate(R.menu.search_menu, menu);super.onCreateOptionsMenu(menu, inflater);final MenuItem myMenuItem = menu.findItem(R.id.search_icon);searchViewModel = new ViewModelProvider(requireActivity()).get(SearchViewModel.class);searchView = (SearchView) myMenuItem.getActionView();searchView.setQueryHint(搜索");searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {@覆盖公共布尔 onQueryTextSubmit(字符串查询){返回假;}@覆盖公共布尔 onQueryTextChange(String newText) {searchViewModel.setQuery(newText);返回假;}});}
'''
- 对于每个子片段(标签)
'''
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {super.onViewCreated(view, savedInstanceState);searchViewModel = new ViewModelProvider(requireActivity()).get(SearchViewModel.class);searchViewModel.getQuery().observe(getViewLifecycleOwner(), new Observer<String>() {@覆盖公共无效 onChanged(String s) {如果 (s!=null){adapter.getFilter().filter(s);}});}
'''
单个查询将适用于所有选项卡.)我研究了很长时间,但是这个解决方案对我有用.
I want to implement SearchView with multiple fragments present in a viewPager. All fragemnts contains lists and I want to filter those lists and create a new ListView which has categorization of results based upon which fragment it belongs to.
But my searchView is not working with one fragment.
Here is my main activity:
package com.codeon.directory;
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import com.codeon.directory.fragments.FiveFragment;
import com.codeon.directory.fragments.FourFragment;
import com.codeon.directory.fragments.OneFragment;
import com.codeon.directory.fragments.SixFragment;
import com.codeon.directory.fragments.ThreeFragment;
import com.codeon.directory.fragments.TwoFragment;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Nikhil Jain on 21-Sep-15.
*/
public class TabEffect extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
public String pic[] = {"1","2","3","1","2","3","1","2","3","1","2","3","1","2","3"};
public String state[] = {"A","B","C"};
public String city[] = {"P","Q","R"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablayout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.tab, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
Bundle bundle = new Bundle();
bundle.putStringArray("pic", pic);
bundle.putStringArray("district", city);
bundle.putStringArray("state", state);
adapter.addFragment(new OneFragment(), "ONE FRAGMENT", bundle);
adapter.addFragment(new TwoFragment(), "TWO FRAGMENT", bundle);
adapter.addFragment(new ThreeFragment(), "THREE FRAGMENT", bundle);
adapter.addFragment(new FourFragment(), "FOUR FRAGMENT", bundle);
adapter.addFragment(new FiveFragment(), "FIVE FRAGMENT", bundle);
adapter.addFragment(new SixFragment(), "SIX FRAGMENT", bundle);
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title, Bundle args) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
fragment.setArguments(args);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
This is my first fragment :
package com.codeon.directory.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.codeon.directory.R;
import com.codeon.directory.RecyclerAdapter;
import com.codeon.directory.customadapters.Listitems_new;
import java.util.ArrayList;
/**
* Created by Nikhil Jain on 21-Sep-15.
*/
public class OneFragment extends Fragment {
ListView list;
private ArrayAdapter<String> listAdapter ;
public OneFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RecyclerView rootView = (RecyclerView) inflater.inflate(R.layout.fragment_one, container, false);
// Inflate the layout for this fragment
Bundle extras = getArguments();
String pic[] = extras.getStringArray("pic");
String state[] = extras.getStringArray("state");
Log.e("VALUE",pic[0]+pic[1]+pic[2]);
rootView.setLayoutManager(new LinearLayoutManager(rootView.getContext()));
//listAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, pic);
rootView.setAdapter(new RecyclerAdapter(pic));
return rootView;
}
}
My SearchActivity:
package com.codeon.directory;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
/**
* Created by Nikhil Jain on 21-Sep-15.
*/
public class SearchActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
Log.e("QUERY", query);
}
}
private void doMySearch(String query) {
}
}
This is my AndroidManifest.xml:
<activity
android:name=".TabEffect"
android:label="@string/tablayout"
android:theme="@style/AppTheme" >
</activity>
<activity android:name=".SearchActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@layout/searchable"/>
</activity>
This is my Searchable layout:
<?xml version="1.0" encoding="utf-8"?>
<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
For simple, I want to implement searchView like in Whatsapp.
解决方案 @Chintan is right. We need to use ViewModel.
Follow this.
- Create SearchViewModel:
'''
private MutableLiveData<String> query= new MutableLiveData<String>();
public void setQuery(String queryData)
{
query.setValue(queryData);
}
public LiveData<String> getQuery() {
return query;
}
'''
- SearchParentFragment
'''
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.search_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
final MenuItem myMenuItem = menu.findItem(R.id.search_icon);
searchViewModel = new ViewModelProvider(requireActivity()).get(SearchViewModel.class);
searchView = (SearchView) myMenuItem.getActionView();
searchView.setQueryHint("Search");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
searchViewModel.setQuery(newText);
return false;
}
});
}
'''
- For each Child Fragment (tabs)
'''
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
searchViewModel = new ViewModelProvider(requireActivity()).get(SearchViewModel.class);
searchViewModel.getQuery().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(String s) {
if (s!=null)
{adapter.getFilter().filter(s);
}
});
}
'''
A single query will work for all tabs.)
I studied it for a long time, but this solution works for me.
这篇关于在android中使用viewpager的带有多个片段的SearchView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!