Я пытаюсь открыть фрагмент из метода onOptionsItemSelected - PullRequest
0 голосов
/ 23 октября 2019

Я пытаюсь вызвать фрагмент из действия. Но не может. Это то, что я пытался.

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if (item.getItemId()==R.id.action_search){
        SearchFragment searchFragment=new SearchFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.container,searchFragment,null).commit();
    }
    return true;
}

И My main_activity.xml

<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
tools:context=".Controllers.MainActivity">

После попытки приложение вылетает, но также не открывается фрагмент, который я хочу.

Фрагмент xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/searchFragment"
tools:context=".SearchFragment">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerViewSearchFrag"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>

И Фрагмент java

public class SearchFragment extends Fragment {

private RecyclerView mRecyclerViewSearch;
private RecyclerViewAdpater mAdpater;

public SearchFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.fragment_search, container, false);
    mRecyclerViewSearch=view.findViewById(R.id.recyclerViewSearchFrag);
    mRecyclerViewSearch.setLayoutManager(new LinearLayoutManager(getContext()));
    mAdpater=new RecyclerViewAdpater(DataSevices.mChineseColors,getActivity());

    return view;
}

}

Пожалуйста, исправьте меня, если я делаю что-то не так

Ответы [ 2 ]

0 голосов
/ 23 октября 2019

Если вы хотите, чтобы в вашем MainActivity были какие-то другие компоненты, создайте для этого фрагмент. и замените его.

в activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/frameLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity" />

в вашем фрагменте xml:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SearchFragment">

      <androidx.recyclerview.widget.RecyclerView
       android:id="@+id/recyclerViewSearchFrag"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>

</RelativeLayout>

создайте новый фрагмент fragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

    ...

</LinearLayout>

и MainFragment.java

public class MainFragment extends Fragment {

  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup 
     container, @Nullable Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_main, container, false);

      //...

      return view;
  }
}
0 голосов
/ 23 октября 2019

Вы можете использовать Framlayout вместо Linear-layout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
...