【问题标题】:Separate searchview for fragments片段的单独搜索视图
【发布时间】:2012-11-09 17:10:39
【问题描述】:

我有一个简单的活动,它拥有一个标签栏来切换两个片段。这两个片段都是一个 listFragment 并实现了一个 searchview 以使在 listfragment 中进行搜索成为可能。搜索视图始终显示在选项卡栏上方的操作栏中。

我遇到的问题是,一旦我切换选项卡(转到其他片段),搜索视图的输入不会重置。因此,第二个片段从 searchview 读取输入并相应地过滤 listfragment,它实际上读取了我在片段 1 中输入的输入。

我想要的是将搜索视图作为两个片段的单独搜索视图。有没有办法做到这一点?

这是我的代码:

活动

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;

}

}

除了搜索部分之外,两个片段几乎相同。

【问题讨论】:

  • 遇到同样的问题。时间有点长,如果您有任何答案,请发布。

标签: android fragment searchview


【解决方案1】:

已经有一段时间了,但如果有人发现同样的问题, 这就是我如何做到的。

只需在每个片段中实现 onPrepareOptionsMenu() 并 在里面,打电话 onQueryTextChange(""); 将 "" 作为查询字符串传递。

【讨论】:

  • 我们最终放弃了使用搜索视图的想法。相反,我们在状态栏下方添加了我们自己的编辑文本以实现类似的结果。无论如何感谢您的回答!
  • 您好,这似乎很有帮助,请您详细说明一下。
  • 谢谢,不过应该把 onQueryTextChange("") 改成 searchView.setQuery("", true)
  • 你是如何在navigation drwer中访问的?
  • @frank1fr 如果使用edittext而不是searchview,可以点击软键盘上的搜索按钮提交我们的搜索吗?
猜你喜欢
  • 2019-09-10
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2015-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多