android - 加载片段时,Android SearchView 不起作用
问题描述
我正在创建一个由一个活动和几个片段组成的应用程序,在我的活动中我添加了一个SearchView希望它像在googleplay中一样始终可用。
在下面的代码中,每当我添加loadFragment(new FragmentSearch());时,SearchView 都无法工作 。在 oncreate 方法中。
我需要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));
}
}
活动主页.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" />
解决方案
推荐阅读
- react-testing-library - 错误:无法找到角色为“alert”的元素(react-testing-library 示例)
- r - 如何将 'as.numeric' 应用于 'digit' 字符向量,并让 'alpha' 向量完好无损
- javascript - 如何将数据从一个组件传递到另一个组件
- java - 如何将我的 SWIG 生成的库所依赖的这个 SWIG 生成的共享库用于搜索 dlsym?
- json - 我有一个关于将值发送回主页的问题 [IOS]
- node.js - 无法将节点代码推送到 Heroku Git 存储库
- java - Java 对象连接
- python - 如何修复此语句,我的代码不适用于 tensorflow?
- c++ - ascii 字符时正则表达式意外结束
- sql - 如何从具有相同 id 的多行中选择仅一行的值并用 - 分隔它们?