独立搜索查看的片段 [英] Separate searchview for fragments
问题描述
我有持有的TabBar切换两个片段一个简单的活动。这两个片段均为listFragment和实施搜索查看使listfragment内寻找可能的。该搜索查看始终显示在使用TabBar上面的动作条。
I have a simple activity which holds a tabbar to switch two fragments. The two fragments are both a listFragment and implement a searchview to make searching possible within the listfragment. The searchview is always shown in the actionbar above the tabbar.
我的问题是,搜索查看的输入不重置一次我切换选项卡(去其他片段)。因此,第二片段读取和SearchView输入和过滤listfragment据此,这实际上读取本人时输入我还在片段之一输入。
The problem I have is that the input of the searchview isn’t reset once I switch the tab (go to other fragment). Therefore, the second fragment reads the input from the searchview and filters the listfragment accordingly, which actually reads the input I entered when I was still in fragment one.
我要的是搜索查看要为两个片段一个单独的搜索查看。有没有一种方法来实现这一目标?
What I want is the searchview to be a separate searchview for both fragments. Is there a way to achieve this?
下面是我的code:
活动
public class ActivityMainApp extends Activity implements ActionBar.TabListener {
private FragmentManager fragmentManager = getFragmentManager();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.mainapp);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Add tabs
ActionBar.Tab relatieTab = actionBar.newTab().setText("Relaties");
ActionBar.Tab takenTab = actionBar.newTab().setText("Taken");
//ActionBar.Tab urenTab = actionBar.newTab().setText("Uren");
// Listeners
relatieTab.setTabListener(this);
takenTab.setTabListener(this);
// Tabs toevoegen aan actionbar
actionBar.addTab(relatieTab);
actionBar.addTab(takenTab);
// Create fragmentmanager to switch fragments
FragmentTransaction fragmentTransaction = this.fragmentManager.beginTransaction();
Fragment fragment = fragmentManager.findFragmentById(R.id.fragment_content);
if(fragment == null){
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.add(R.id.fragment_content, new FragRelaties());
}
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
FragmentTransaction fragmentTransaction = this.fragmentManager.beginTransaction();
if(tab.getText().equals("Taken")){
FragTaken fragment = new FragTaken();
fragmentTransaction.replace(R.id.fragment_content, fragment);
fragmentTransaction.commit();
}
if(tab.getText().equals("Relaties")){
FragRelaties fragment = new FragRelaties();
fragmentTransaction.replace(R.id.fragment_content, fragment);
fragmentTransaction.commit();
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
片段一个
public class FragRelaties extends ListFragment implements SearchView.OnQueryTextListener {
private LayoutInflater inflater;
private ModelRelatie modelRelatie;
private AdapterRelatie relatieAdapter;
public ArrayList<Relatie> relaties;
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.inflater = inflater;
Activity activity = getActivity();
final Context context = activity.getApplicationContext();
setHasOptionsMenu(true);
new AsyncTask<Void, Void, ArrayList<Relatie>>(){
protected ArrayList<Relatie> doInBackground(Void... params) {
// Get all my contacts
modelRelatie = ModelRelatie.instantiate(context);
ArrayList<Relatie> relaties = modelRelatie.getRelaties();
return relaties;
}
protected void onPostExecute(ArrayList<Relatie> relaties) {
// Initial input of objects in the list
relatieAdapter = new AdapterRelatie(inflater.getContext(), R.layout.relatieslijstitem, relaties);
setListAdapter(relatieAdapter);
}
}.execute();
View view = inflater.inflate(R.layout.relaties, container, false);
return view;
}
@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.relatie_menu, menu);
// Get the searchview
MenuItem zoekveld = menu.findItem(R.id.zoekveld_fragrelatie);
SearchView zoekview = (SearchView) zoekveld.getActionView();
// Execute this when searching
zoekview.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
public void onListItemClick(ListView l, View v, int position, long id) {
// Things that happen when i click on an item in the list
}
public boolean onQueryTextSubmit(String query) {
return true;
}
public boolean onQueryTextChange(String zoekterm) {
Activity activity = getActivity();
Context context = activity.getApplicationContext();
// We start searching for the name entered
ModelRelatie modelRelatie = ModelRelatie.instantiate(context);
ArrayList<Relatie> relaties = modelRelatie.zoekRelatie(zoekterm);
// The returned objects are placed in the list
this.relatieAdapter = new AdapterRelatie(inflater.getContext(), R.layout.relatieslijstitem, relaties);
setListAdapter(relatieAdapter);
return true;
}
片段二
public class FragTaken extends ListFragment implements SearchView.OnQueryTextListener {
private LayoutInflater inflater;
private AdapterTaak adapterTaak;
private ModelTaken modelTaken;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.inflater = inflater;
Activity activity = getActivity();
final Context context = activity.getApplicationContext();
setHasOptionsMenu(true);
this.modelTaken = ModelTaken.instantiate(context);
ArrayList<Taak> taken = modelTaken.getTaken();
// Initial input for the list
adapterTaak = new AdapterTaak(inflater.getContext(), R.layout.takenlijstitem, taken);
setListAdapter(adapterTaak);
View view = inflater.inflate(R.layout.taken, container, false);
return view;
}
@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.taken_menu, menu);
// get the searview
MenuItem zoekveld = menu.findItem(R.id.zoekveld_fragtaken);
SearchView zoekview = (SearchView) zoekveld.getActionView();
// Execute this when searching
zoekview.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
public boolean onQueryTextSubmit(String query) {
return true;
}
public boolean onQueryTextChange(String zoekterm) {
Activity activity = getActivity();
Context context = activity.getApplicationContext();
// Search the task by the inputed value
ModelTaken modelTaken = ModelTaken.instantiate(context);
ArrayList<Taak> taken = modelTaken.zoekTaak(zoekterm);
// Return the found items to the list
this.adapterTaak = new AdapterTaak(inflater.getContext(), R.layout.takenlijstitem, taken);
setListAdapter(adapterTaak);
return true;
}
}
两个片段是除了搜索部分几乎相同。
Both fragments are almost identical except the searching part.
推荐答案
它已经相当一段时间,但还是如果有人发现了同样的问题, 以下是我成功地做到这一点。
Its been quite a while but still if anyone is finding the same problem, here's how i managed to do that .
就在您的每一个片段执行在prepareOptionsMenu()并 这一点,调用之内 onQueryTextChange(); 过客。作为查询字符串
Just implement onPrepareOptionsMenu() in each of your fragments and inside of it, call onQueryTextChange(""); passing "" as query string.
这篇关于独立搜索查看的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!