搜索栏中有单独的片段

时间:2019-04-19 19:46:56

标签: android android-fragments search searchbar

我在一个Activity(Home.java)中创建了不同的片段,在该Activity上有工具栏和导航抽屉。
现在我想把searchview放在fragment.We通常写所有与searchview相关的代码在oncreateoption菜单中。我做过同样的事情,但是oncreateoption是在片段内部制作的。

我没有收到任何错误,但搜索栏未显示在工具栏上。

package com.example.foody;


import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;

public class fitem extends Fragment implements SearchView.OnQueryTextListener
{
    RecyclerView rv1;
    fitemAdapter adapter;
    ArrayList<All_Data> item;
    Cursor cr;
    Storage sob;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View v=inflater.inflate(R.layout.fragment_fitem, container, false);
        sob=new Storage(getContext());

        Bundle bundle = this.getArguments();
        if (bundle == null)
            cr=sob.allData();                           //if called from home page
        else
            cr=sob.categorisedData(bundle.getString("category"));      // if called from category page
        inData();

        rv1=v.findViewById(R.id.rv1);
        rv1.setHasFixedSize(true);
        rv1.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter=new fitemAdapter(getContext(),item);
        rv1.setAdapter(adapter);
        return v;
    }

    void inData()
    {
        item=new ArrayList<>();
        while(cr.moveToNext())
        {
            item.add(new All_Data(cr.getString(0),cr.getString(2),cr.getString(3)));
        }
    }

    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
    {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.tool_menu, menu);
        MenuItem searchItem = menu.findItem(R.id.search1);
        SearchView searchView = (SearchView) searchItem.getActionView();
        searchView.setOnQueryTextListener(fitem.this);
        searchView.setQueryHint("Search");
    }


    @Override
    public boolean onQueryTextChange(String s)
    {
        String userInput=s.toLowerCase();
        ArrayList<All_Data> filterlist=new ArrayList<>();
        for(int i=0;i<item.size();i++)
        {
            String st=item.get(i).getName().toLowerCase();
            if(st.contains(userInput))
                filterlist.add(item.get(i));
        }
        adapter=new fitemAdapter(getContext(),filterlist);
        rv1.setAdapter(adapter);
        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String s) {
        return false;
    }

}

1 个答案:

答案 0 :(得分:0)

在片段的setHasOptionsMenu(true);调用onCreateOptionsMenu()

这将使您的片段可以显示您在片段的aws.tf中膨胀的菜单项;否则,它将使用主机“活动”菜单。