我无法通过Android应用程序中的按钮从一个片段导航到另一个片段。我已经考虑了几个关于这个问题的问题,但提供的解决方案并没有解决我的问题。这是我的代码,我不知道我做错了什么。
public class fragment_profile extends Fragment {
TextView txtFname, txtLname, txtGender, txtAge, txtPhone, txtEmail;
Button btImages, btVideos;
ImageButton btProfilePic;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_profile, container, false);
btProfilePic = (ImageButton)rootView.findViewById(R.id.ProfilePic);
txtFname = (TextView)rootView.findViewById(R.id.tvFName);
txtLname = (TextView)rootView.findViewById(R.id.tvLName);
txtGender = (TextView)rootView.findViewById(R.id.tvGender);
txtAge = (TextView)rootView.findViewById(R.id.tvAge);
txtPhone = (TextView)rootView.findViewById(R.id.tvPhone);
txtEmail = (TextView)rootView.findViewById(R.id.tvEmail);
btImages = (Button)rootView.findViewById(R.id.btnImages);
btVideos = (Button)rootView.findViewById(R.id.btnVideos);
//The code to replace fragment is not good, the clicklistener is working fine as I have tested it with a toast message
btImages.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//An object of the fragment tree is created
fragmentImages ImageGallery = new fragmentImages();
//The fragment is finally added
fragmentTransaction.replace(R.id.fragment_profile, ImageGallery, "Image Gallery").commit();
//Set title of action bar = title of fragment
getActivity().setTitle(getTag());
}
});
btVideos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//An object of the fragment tree is created
fragmentVideos VideoGallery = new fragmentVideos();
//The fragment is finally added
fragmentTransaction.replace(R.id.fragment_profile, VideoGallery, "Video Gallery").commit();
//Set title of action bar = title of fragment
getActivity().setTitle(getTag());
}
});
return rootView;
}
}
问题出在onClickListener
内的代码中。谁能告诉我我做错了什么?谢谢。
答案 0 :(得分:1)
您想使用supportFragmentManager
。在onClickListener
(s)中,进行以下交易:
//An object of the fragment tree is created
fragmentImages ImageGallery = new fragmentImages();
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_profile, ImageGallery).commit();
//rest of your setting the activity title below
最佳做法是将您的类编写为FragmentImages
,然后将变量编写为imageGallery
,例如fragmentImages
,以便了解对象是什么。
答案 1 :(得分:0)
我认为你必须将你的fragment_profile
片段添加到backstack中,这样你就可以在触发其他片段之后再调用它。请尝试以下方法:
fragmentTransaction.replace(R.id.fragment_profile, ImageGallery, "Image Gallery").addToBackStack(null).commit();
与后者相同:
fragmentTransaction.replace(R.id.fragment_profile, VideoGallery, "Video Gallery").addToBackStack(null).commit();
我从这里得到了信息:Fragment Replacing Existing Fragment