I have an Activity
page with 4 different elements that are fragments which I have created containers for under their respective titles. OnClick
I want the fragment to open and another click on it's title and I would like for that fragment to close.
For some reason, the code that I have written is opening the fragment just as it is supposed to right beneath the title, but the second click on the title which is supposed to close/hide/collapse the fragment isn't doing so.
I wasn't sure if I should be looking for the container in findViewById
or the actual fragment name... Makes sense for it to be the actual container and not the fragment name.
What should I modify to make this work?
SettingsActivity
mPrivacy.setOnClickListener(v -> {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.container_privacy, new PrivacyFragment(), null).addToBackStack(null).commit();
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container_privacy);
if (fragment != null)
if (fragment.isVisible()) {
fragmentTransaction.hide(fragment).commit();
} else {
fragmentTransaction.show(fragment).commit();
}
});
mSecurity.setOnClickListener(v -> {
//Create Security Fragment
});
mHelp.setOnClickListener(v -> {
//Create Help Fragment
});
mAbout.setOnClickListener(v -> {
//Create About Fragment
});
getUserInfo();
}
I've also been trying it like this, but still won't close after it's opened.
Second method
mPrivacy.setOnClickListener(v -> {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.container_privacy, new PrivacyFragment(), null).addToBackStack(null);
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container_privacy);
if (fragment != null) {
if (fragment.isHidden()) {
fragmentTransaction.show(fragment);
} else {
fragmentTransaction.hide(fragment);
}
}
fragmentTransaction.commit();
});
This last method actually works and does what I want it to do, but just wondering if it's a good way to go about it. Have seen some posts that say not to mess with the Visibility
. Was wondering why? Does it matter?
Third method
mPrivacy.setOnClickListener(v -> {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.container_privacy, new PrivacyFragment(), null).addToBackStack(null).commit();
if (mFrameLayout.getVisibility() == View.GONE) {
mFrameLayout.setVisibility(View.VISIBLE);
} else {
mFrameLayout.setVisibility(View.GONE);
}
});
This is the simplest solution I've found. Try to place the fragment in a FrameLayout and simply show/hide the FrameLayout
<FrameLayout
android:layout_width="match_parent"
android:visibility="gone"
android:id="@+id/frame_id"
android:layout_height="wrap_content">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
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.