解决了我无法访问片段中的菜单吗?

时间:2019-05-06 12:07:07

标签: android menu rx-android


UPD:

此链接帮助我解决了此问题 https://riptutorial.com/android/example/19832/searchview-in-toolbar-with-fragment


我想通过onOptionsItemSelected访问菜单项,但从未调用过。我尝试了很多方法,并停止了该解决方案,但并没有帮助。

HomeFragment.java

   public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
       Log.d(TAG, "onCreateView: ");
       View view = inflater.inflate(R.layout.fragment_home, container, false);
       ButterKnife.bind(this, view);
       setUpRecyclerView();
       setupAuthorsSelectionRV();
       setUpToolbar();


       HomeViewModel model = ViewModelProviders.of(this).get(HomeViewModel.class);

       disposable = model.getSongs()
               .subscribeOn(Schedulers.io())
               .observeOn(AndroidSchedulers.mainThread())
               .subscribe(songs -> {
                   ((SongRVAdapter) songRV.getAdapter()).setSongList(songs);
                   songRV.getAdapter().notifyDataSetChanged();
                   Log.d(TAG, "UI updated! |Song list| - ");
               }, throwable -> {
                   Toast.makeText(getContext(),
                           "An error occurred - " + throwable,
                           Toast.LENGTH_SHORT).show();
                   Log.e(TAG, "Error while loading songs:\n", throwable);
               }, () -> Log.i(TAG, "Songs loaded!"));

       setHasOptionsMenu(true);
       return view;
   }
@Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        inflater.inflate(R.menu.toolbar_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }
@Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        Log.d(TAG, "onOptionsItemSelected: ");
        if (item.getItemId() == R.id.searchBtn) {
            Log.d(TAG, "onOptionsItemSelected: inside IF");
            ((SearchView) item.getActionView())
                    .setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                        @Override
                        public boolean onQueryTextSubmit(String query) {
                            Log.d(TAG, "onQueryTextSubmit: ");
                            Completable.fromAction(() -> adapter.filter(query))
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(() -> songRV.getAdapter().notifyDataSetChanged());
                            return true;
                        }

                        @Override
                        public boolean onQueryTextChange(String newText) {
                            Log.d(TAG, "onQueryTextChange: ");
                            Completable.fromAction(() -> adapter.filter(newText))
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(() -> songRV.getAdapter().notifyDataSetChanged());
                            return true;
                        }
                    });

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    tools:context=".ui.homescreen.HomeFragment">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBar"
        style="@style/wideStyle"
        android:background="@android:color/white">


        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            app:contentScrim="@android:color/transparent"
            app:expandedTitleMarginBottom="70dp"
            app:expandedTitleTextAppearance="@android:color/white"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <androidx.viewpager.widget.ViewPager
                    android:id="@+id/authorsSelectionsVP"
                    android:layout_width="wrap_content"
                    android:layout_height="100dp"
                    android:layout_marginTop="10dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/header" />

                <TextView
                    android:id="@+id/header"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10dp"
                    android:layout_marginTop="40dp"
                    android:ellipsize="end"
                    android:maxLines="1"
                    android:paddingBottom="5dp"
                    android:text="Авторская подборка"
                    android:textColor="@android:color/black"
                    android:textSize="25sp"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toTopOf="@id/authorsSelectionsVP"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </com.google.android.material.appbar.CollapsingToolbarLayout>

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolBar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent" />



    </com.google.android.material.appbar.AppBarLayout>


    <androidx.core.widget.NestedScrollView
        android:id="@+id/nestedScroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:paddingBottom="100dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/allSongsLabel">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/allSongsLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingStart="15dp"
                android:paddingBottom="12dp"
                android:text="@string/all_songs"
                android:textColor="@android:color/black"
                android:textSize="20sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toTopOf="@id/songRV"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/songRV"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@id/allSongsLabel"
                tools:listitem="@layout/song_item" />


        </androidx.constraintlayout.widget.ConstraintLayout>


    </androidx.core.widget.NestedScrollView>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

如果它支持我该怎么办?


UPD:

此链接帮助我解决了此问题 https://riptutorial.com/android/example/19832/searchview-in-toolbar-with-fragment


1 个答案:

答案 0 :(得分:0)

在您的片段onCreateView中添加:

setHasOptionsMenu(true);

在“活动”中,确保您的onOptionsItemSelected没有返回false ID上的R.id.searchBtn