我正在创建一个由一个活动和几个片段组成的应用程序,我在活动中添加了一个 SearchView ,希望它像 googleplay 一样一直可用。 >。
在下面的代码中,每当我在oncreate方法中添加 loadFragment(new FragmentSearch()); 时,SearchView均无法工作。
我需要 loadFragment(new FragmentSearch()); ,以便在创建应用程序时加载我的片段。
请提供解决方案并启发我为什么会发生此错误。
public class HomeActivity extends AppCompatActivity implements
BottomNavigationView.OnNavigationItemSelectedListener, SearchView.OnQueryTextListener {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(this);
loadFragment(new FragmentSearch());//this code causes error
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu,menu);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
public boolean loadFragment(Fragment fragment) {
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frameContainer, fragment)
.commit();
return true;
}
return false;
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.navigation_search:
fragment = new FragmentSearch();
break;
case R.id.navigation_graph:
fragment = new FragmentGraph();
break;
}
return loadFragment(fragment);
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
}
这是片段。我认为错误是在片段代码中,尽管我找不到它。
public class FragmentSearch extends Fragment {
private static final String TAG = "FragmentSearch";
RecyclerView productsRecyclerView;
RecyclerViewAdapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Product> productArrayList = new ArrayList<>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.d(TAG, "onCreateView: ");
View v = inflater.inflate(R.layout.fragment_search, null);
productsRecyclerView = v.findViewById(R.id.recyclerview_search);
layoutManager = new LinearLayoutManager(getContext());
productsRecyclerView.setLayoutManager(layoutManager);
productsRecyclerView.setHasFixedSize(true);
TESTinitializeValueForRecyclerview();
adapter = new RecyclerViewAdapter(productArrayList);
productsRecyclerView.setAdapter(adapter);
return v;
}
private void TESTinitializeValueForRecyclerview() {
String date = "date";
String name = "name";
int sellingPrice = 0;
for (int i = 0; i < 30; i++)
productArrayList.add(new Product(date + i, name + i, sellingPrice + i));
}
}
activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity">
<include layout="@layout/toolbar_layout" />
<FrameLayout
android:paddingTop="?attr/actionBarSize"
android:id="@+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation">
</android.support.design.widget.BottomNavigationView>
<com.leinardi.android.speeddial.SpeedDialView
android:id="@+id/speedDial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="72dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />