![](/img/trans.png)
[英]Menu item click in a navigation drawer intent to activity hosting a fragment
[英]how to add a fragment to the navigation drawer menu item fragment
我在這個片段的導航抽屜項目菜單中有一個商店項目片段我想知道如果用戶點擊 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;
}
}
如果用戶單擊圖像圖標,我想將他/她發送到音樂片段,然后仍然保持抽屜並允許他返回到上一個片段
我建議您使用AlertDialog
來完成這項工作,但您可以制作全屏AlertDialog
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.