我在我的应用程序中有几个搜索视图,我想在另一个片段中实现它,我复制并粘贴它所在的其他片段中的所有内容,显然更改变量,因此,它不会导致冲突或一些东西和我的应用程序运行正常,工具栏显示但不是我的searchview,一旦我从tab更改为tab并返回到此片段现在它显示,我不知道为什么,请任何帮助将是伟大的
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ManageFragments.MainActivity">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
</menu>
Search_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/GrayBackground"
tools:context=".HomeFragments.SearchFragment">
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/Theme.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold"
android:textColor="@android:color/black"
android:layout_gravity="clip_vertical"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:visibility="invisible" />
</RelativeLayout>
</ScrollView>
</LinearLayout>
片段
final MenuItem mSearchMenuItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) mSearchMenuItem.getActionView();
//final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setQueryHint(getText(R.string.search));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextSubmit( String query)
{
final GifTextView loading = (GifTextView)FeedsFragment.this.getActivity().findViewById(R.id.loadingSearch);
final TextView loadingText = (TextView)FeedsFragment.this.getActivity().findViewById(R.id.loadingTextSearch);
final ImageView loadingBackground = (ImageView)FeedsFragment.this.getActivity().findViewById(R.id.imgGrayBackgroundSearch);
loading.setVisibility(View.VISIBLE);
loadingText.setVisibility(View.VISIBLE);
loadingBackground.setVisibility(View.VISIBLE);
//Toast.makeText(SearchFragment.this.getActivity(), R.string.buscado, Toast.LENGTH_SHORT).show();
searchView.setQuery("", false);
searchView.setIconified(true);
new PlifRequestBase(getActivity()){
@Override
public JsonObject onHttpOk(JsonObject response) throws JSONException
{
JsonObject data, pagination_details = null, businesses, cover_premium, cv, iurl;
JsonArray premiums, basics;
if (response.get("pagination") == null)
{
data = response;
}else {
pagination_details = response.get("pagination").getAsJsonObject();
data = response.get("data").getAsJsonObject();
businesses = data.get("businesses").getAsJsonObject();
premiums = businesses.get("premiums").getAsJsonArray();
basics = businesses.get("basics").getAsJsonArray();
gson = new Gson();
arrayBusiness = new ArrayList<Business>();
arrayBasics= new ArrayList<Business>();
arrayBusiness = gson.fromJson(premiums, BUSINESS_TYPE);
arrayBasics=gson.fromJson(basics, BUSINESS_TYPE);
Log.d("size", String.valueOf(arrayBusiness.size()));
//Log.d("", String.valueOf(images_premiums));
if (getActivity() == null)
return response;
FeedsFragment.this.getActivity().runOnUiThread(new Runnable()
{
@Override
public void run() {
RecyclerView recycler = (RecyclerView) FeedsFragment.this.getActivity().findViewById(R.id.high_recycler_view);
RecyclerView recyclerbasics=(RecyclerView) FeedsFragment.this.getActivity().findViewById(R.id.lower_recycler_view);
SearchHorizontalAdapter adapter = new SearchHorizontalAdapter(getActivity(), arrayBusiness, R.layout.search_high_layout);
SearchVerticalAdapter adapterbasics=new SearchVerticalAdapter(getActivity(),arrayBasics,R.layout.search_low_layout);
recyclerbasics.setNestedScrollingEnabled(false);
recycler.setNestedScrollingEnabled(false);
mLowerLayoutManager = new GridLayoutManager(FeedsFragment.this.getActivity(),2);
recyclerbasics.setLayoutManager(mLowerLayoutManager);
recyclerbasics.setAdapter(adapterbasics);
mLayoutManagera = new LinearLayoutManager(FeedsFragment.this.getActivity(), LinearLayoutManager.HORIZONTAL, false);
recycler.setLayoutManager(mLayoutManagera);
recycler.setAdapter(adapter);
for (int i = 0; i < arrayBusiness.size(); i++)
{
Log.d("Imprime", arrayBusiness.get(i).getName());
}
for (int i = 0; i < arrayBasics.size(); i++)
{
Log.d("Imprime", arrayBasics.get(i).getName());
}
loading.setVisibility(View.GONE);
loadingText.setVisibility(View.GONE);
loadingBackground.setVisibility(View.GONE);
}
});
}
if (pagination_details.isJsonNull()){
Log.d("Paginacion", pagination_details.toString());
}
return data;
}
@Override
public void onHttpCreate(JsonObject response) throws JSONException
{
}
@Override
public void onHttpUnprocessableEntity(JsonObject response) throws JSONException {
this.cancel(true);
final String error = response.get("errors").toString();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity().getApplicationContext(), error, Toast.LENGTH_LONG).show();
}
});
}
}.execute("businesses/search", "GET", "q", "\'"+query.replaceAll(" ","&").trim()+"\'");
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
textView.setText(newText);
return true;
}
});
View searchPlate = (View) searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
searchPlate.setBackgroundResource(R.mipmap.textfield_custom);
//return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
default:
return super.onOptionsItemSelected(item);
}
}
修改 http://imgur.com/a/WPnsq这是我遇到问题的地方,它没有显示
http://imgur.com/a/WcxNy在这里,我从一个标签移动到另一个标签并返回,现在它正在显示