我正在尝试在片段中实现searchView功能。 searchView位于自定义操作栏中,这是我使用的代码,但它不起作用。如果我将搜索视图移动到片段布局,它可以工作,但它在操作栏中不起作用。
public class TabActivity_adbya extends Fragment implements View.OnTouchListener,AdapterView.OnItemClickListener {
GridView gridView;
FirebaseDatabase firebaseDatabaseInstance;
private DatabaseReference booksInstance;
SearchView sv;
AlbumDBHandler db;
ArrayList<mokdmatData> books = new ArrayList<>();
maKotabAdapter adapter;
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstancestate) {
final View rootView = inflater.inflate(R.layout.activity_tab_adbya,container,false);
((makalat2) getActivity()).getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
((makalat2) getActivity()).getSupportActionBar().setCustomView(R.layout.abs_layout);
((makalat2) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(true);
TextView textViewTitle = (TextView) getActivity().findViewById(R.id.mytext);
textViewTitle.setText("Articles");
sv = (SearchView) getActivity().findViewById(R.id.svZelal);
sv.setQueryHint("Search...");
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String query) {
adapter.getFilter().filter(query);
return true;
}
});
gridView = (GridView) rootView.findViewById(R.id.gridView_adbya);
gridView.setOnItemClickListener(this);
db = new AlbumDBHandler(getActivity());
getData();
init();
return rootView;
}
这是我正在使用的自定义操作栏
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
>
<ImageView
android:layout_width="0dp"
android:layout_height="22dp"
android:src="@drawable/homebutton"
android:layout_gravity="center"
android:layout_weight="0.40"
android:id="@+id/homeButton"
/>
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.60"
/>
<com.mediamer.metwally.saidkotb.helpers.CustomTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@color/TextColor"
android:id="@+id/mytext"
android:textSize="25sp"
android:layout_weight="1"
/>
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.60"
/>
<android.support.v7.widget.SearchView
android:id="@+id/svZelal"
android:layout_width="0dp"
android:searchIcon="@android:drawable/ic_menu_search"
android:layout_height="match_parent"
android:layout_weight="0.40"
/>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
我认为您必须在MainActivity中绑定SearchView并声明compile 'com.android.volley:volley:1.0.0'
然后使用像public static void sendPushToSingleInstance(final Context activity, final HashMap dataValue /*your data from the activity*/, final String instanceIdToken /*firebase instance token you will find in documentation that how to get this*/ ) {
final String url = "https://fcm.googleapis.com/fcm/send";
StringRequest myReq = new StringRequest(Request.Method.POST,url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(activity, "Bingo Success", Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(activity, "Oops error", Toast.LENGTH_SHORT).show();
}
}) {
@Override
public byte[] getBody() throws com.android.volley.AuthFailureError {
Map<String, Object> rawParameters = new Hashtable();
rawParameters.put("data", new JSONObject(dataValue));
rawParameters.put("to", instanceIdToken);
return new JSONObject(rawParameters).toString().getBytes();
};
public String getBodyContentType()
{
return "application/json; charset=utf-8";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "key="+YOUR_LEGACY_SERVER_KEY_FROM_FIREBASE_CONSOLE);
return headers;
}
};
Volley.newRequestQueue(activity).add(myReq);
}
这样的片段
答案 1 :(得分:0)
您需要使用searchView作为菜单项,然后您需要使用以下代码在片段中添加菜单:
private MenuItem mSearchItem;
private SearchView sv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_search, menu); // Put your search menu in "menu_search" menu file.
mSearchItem = menu.findItem(R.id.svZelal);
sv = (SearchView) MenuItemCompat.getActionView(mSearchItem);
sv.setIconified(true);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
sv.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
mSearchView.clearFocus();
return true;
}
@Override
public boolean onQueryTextChange(String query) {
adapter.getFilter().filter(query);
return true;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
我希望它会帮助你。