如何将片段添加到导航抽屉菜单项片段

时间:2021-02-28 04:18:06

标签: java android

我在这个片段的导航抽屉项目菜单中有一个商店项目片段我想知道如果用户点击 Imageview 图标然后仍然保持我的导航抽屉下面是我的尝试,我想知道如何向它添加另一个片段

>
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;
    }
} 

enter image description here

如果用户单击图像图标,我想将他/她发送到音乐片段,然后仍然保持抽屉并允许他返回上一个片段

1 个答案:

答案 0 :(得分:0)

我建议您使用 AlertDialog 来完成这项工作,但您可以制作全屏 AlertDialog...