Problem with displaying top app and contextual action bar in fragment

403 Views Asked by At

Initially, I tried to make the context menu appear after a long press on the RecyclerView element of the first fragment. Then I decided to make it at least appear. My application consists of three chunks. I am using material design components. The top menu is displayed on the activity layout, and for now it should only change to the contextual menu of the first fragment. Now the contextual menu is displayed by clicking on the RecyclerView element of the first fragment, but it appears, and the top menu does not disappear. I need to display Toolbar Material separately on each fragment and somehow replace it with contextual one? Explain, please.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/q1"
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"
tools:context=".MainActivity">

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/topmenu"
            style="@style/Widget.MaterialComponents.Toolbar.Primary"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:menu="@menu/top_app_bar"
            app:navigationIcon="@drawable/ic_baseline_format_list_bulleted_24"
            app:title="Главная" />

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

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

<fragment
    android:id="@+id/fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
    android:layout_marginTop="?attr/actionBarSize"
    android:layout_marginBottom="?attr/actionBarSize"
    app:navGraph="@navigation/my_nav" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottomNavigationView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:menu="@menu/bottom_menu"
    />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

MaterialToolbar topBar;
MaterialAlertDialogBuilder madb;
CharSequence sortItems[] = {"По дате", "По важности", "По алфавиту"} ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
    NavController navController = Navigation.findNavController(this, R.id.fragment);
    //AppBarConfiguration appBarConfiguration = AppBarConfiguration(setOf(R.id.firstFragment, R.id.secondFragment, R.id.thirdFragment));
    NavigationUI.setupWithNavController(bottomNavigationView, navController);

    topBar = findViewById(R.id.topmenu);
    madb = new MaterialAlertDialogBuilder(MainActivity.this)
            .setTitle("ItsTime")
            .setSingleChoiceItems(sortItems, 1, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    switch (i)
                    {
                        case (0):
                            Toast.makeText(MainActivity.this, "Кнопка 0", Toast.LENGTH_SHORT);
                        case (1):
                            Toast.makeText(MainActivity.this, "Кнопка 1", Toast.LENGTH_SHORT);
                        case (2):
                            Toast.makeText(MainActivity.this, "Кнопка 2", Toast.LENGTH_SHORT);

                    }
                }
            })
            .setPositiveButton("Okay, Boomer", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(MainActivity.this, "Кнопка ОК", Toast.LENGTH_SHORT);
                }
            });

    topBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId())
            {
                case (R.id.itemShow):
                            madb.show();
                            return true;
            }
            return false;
        }
    });
}}

Fragment_First.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"

>
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="4dp"
    android:scrollbars="vertical"
    android:background="@color/colorPrimaryDark"

    />


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

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/newEventBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_gravity="bottom|end"
            android:layout_margin="32dp"
            android:src="@drawable/ic_baseline_add_24"
            android:textAllCaps="false"
            android:textSize="20sp"
            android:textStyle="bold"
            app:tint="#FFFFFF">

        </com.google.android.material.floatingactionbutton.FloatingActionButton>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

And importants part of FirstFragment.java

android.view.ActionMode actionMode = null;
Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        //mParam1 = getArguments().getString("name");
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_first_new, container, false);



    rvEvent = view.findViewById(R.id.recyclerview);
    rvEvent.setHasFixedSize(true);
    lmEvent = new LinearLayoutManager(this.getContext());
    adapterEvent = new EventAdapter(eventsProcess);

    rvEvent.setLayoutManager(lmEvent);


    adapterEvent.setOnItemClickListener(new EventAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(int position) {
            eventsProcess.remove(position);
            rvEvent.removeViewAt(position);
            adapterEvent.notifyItemRemoved(position);
            write(getContext(), eventsProcess, PROCESSED_EVENTS);

            if (actionMode == null){
                actionMode = ((AppCompatActivity) getActivity()).startActionMode(ContextualActionMode);
            }

        }
    });


   

    rvEvent.setAdapter(adapterEvent);

 


    return view;
}


android.view.ActionMode.Callback ContextualActionMode = new ActionMode.Callback() {
    @Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
        MenuInflater menuInflater = actionMode.getMenuInflater();
        menuInflater.inflate(R.menu.contextual_top_app_bar, menu);
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
        switch (menuItem.getItemId()){
            case R.id.deleteIcon:
                Toast.makeText(getContext(), "себевскрой", Toast.LENGTH_SHORT).show();
                return true;
        }
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode actionMode) {

    }
};

Result of ItemClick of RecyclerView: item was deleted (that's ok and this problem with Contextual Action Bar)

1

There are 1 best solutions below

0
Кирилл Химов On

Ok, I fixed this by adding this <item name="windowActionModeOverlay">true</item> to the style. But the question remains the same - am I doing the right thing by displaying the top menu in the activity, and displaying the contextual action bar in the fragment?