简体   繁体   中英

how to add a fragment to the navigation drawer menu item fragment

I have a store item fragment in the navigation drawer item menu in this fragment I will like to known how to add another fragment to it if the user click on the Imageview icon then still maintain my navigation drawer below is my try

package com.example.entertainmentlab.ui.store;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.entertainmentlab.R;
import com.example.entertainmentlab.ui.setting.SettingViewModel;

public class StoreFragment extends Fragment {

    private StoreViewModel StoreViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        StoreViewModel =
                ViewModelProviders.of(this).get(StoreViewModel.class);
        View root = inflater.inflate(R.layout.fragment_store, container, false);
       final ImageView MusicButton = root.findViewById(R.id.music_btn);





     //I want to move to the next Fragment if the user click the music icon

        MusicButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               try {

                   Fragment fragment = new BlankFragment();
                   FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                   FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                   fragmentTransaction.replace(R.id.nav_store, fragment);
                   fragmentTransaction.addToBackStack(null);
                   fragmentTransaction.commit();
               }catch (Exception e ){
                   Toast.makeText(getActivity(), "erro  "+e, Toast.LENGTH_SHORT).show();
               }
           }
       });
//        StoreViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
//            @Override
//            public void onChanged(@Nullable String s) {
//                textView.setText(s);
//            }
//        });
        return root;
    }
} 

在此处输入图像描述

if the user click the image icon I want to send he/she to the music fragment and then still maintaining the drawer and allow him to return back to the previous fragment

i suggest to you use a AlertDialog for do this work but you can make full screen AlertDialog ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM