Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何仅在一个片段中创建searchview_Java_Android_Android Studio_Android Fragments_Searchview - Fatal编程技术网

Java 如何仅在一个片段中创建searchview

Java 如何仅在一个片段中创建searchview,java,android,android-studio,android-fragments,searchview,Java,Android,Android Studio,Android Fragments,Searchview,我表示,我最近尝试开发Android应用程序,但我肯定缺乏一些概念。我正在尝试创建一个带有导航抽屉和底部导航的应用程序。在我从菜单打开的其中一个片段上,我想插入一个类似以下内容的搜索视图: 这个搜索视图必须只出现在片段中,片段可以从菜单中打开,称为SearchFragment,所以我的问题是在片段本身中设置这个条件 SearchFragment.java public class SearchFragment extends Fragment { public View onCreat

我表示,我最近尝试开发Android应用程序,但我肯定缺乏一些概念。我正在尝试创建一个带有导航抽屉和底部导航的应用程序。在我从菜单打开的其中一个片段上,我想插入一个类似以下内容的搜索视图

这个搜索视图必须只出现在片段中,片段可以从菜单中打开,称为SearchFragment,所以我的问题是在片段本身中设置这个条件

SearchFragment.java

 public class SearchFragment extends Fragment {
 
 public View onCreateView(@NonNull LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_search, container, false);
    ListView listview = (ListView) view.findViewById(R.id.listSearch);
    String[] items = new String[] { "Item One", "Item Two", "Item Three" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
listview.setAdapter(adapter);
    
return view;
        }
    }
public class MainActivity extends AppCompatActivity {

 private AppBarConfiguration mAppBarConfiguration;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 Toaolbar toolbar = findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);

 DrawerLayout drawer = findViewById(R.id.drawer_layout);
 NavigationView navigationView = findViewById(R.id.nav_view);

mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);

BottomNavigationView bottom_nav_view = findViewById( R.id.bottom_nav_view );
NavigationUI.setupWithNavController( bottom_nav_view, navController );
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
活动\u main\u drawer.xml(菜单)


activity_main.xml、app_bar_main.xml、nav_header_main.xml和mobile_navigation.xml:标准导航抽屉活动

content\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">

<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/activity_main_drawer"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">

<androidx.appcompat.widget.SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listSearch"
android:layout_below="@+id/edit"/>
</LinearLayout>

fragment\u search.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">

<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/activity_main_drawer"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">

<androidx.appcompat.widget.SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listSearch"
android:layout_below="@+id/edit"/>
</LinearLayout>

基本上我需要知道的是:

  • 如何插入带有文本过滤器的SearchView
  • 如何使筛选结果“可点击”,以便它打开另一个片段
  • 所有这些在片段内都可行吗,从选择中排除其他活动/片段

  • 谢谢。

    对于第3项,请从主要活动中删除搜索,并将其包含在该片段中。