从选项卡关闭并替换片段并关闭当前片段
我有选项菜单,我想做的是: 我想使用选项菜单选择一个选项卡,然后替换该选项卡的片段 目标选项卡与另一个片段。
该图像显示与目标选项卡的当前片段重叠的替换片段。
帮助谢谢
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int _id = item.getItemId();
// Select tablayout using option Menu
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
TabLayout.Tab _tab;
switch (_id)
{
case R.id.mnutabindx0Frag2: //show first tab with second fragment
//Set the Tab index to show
_tab = tabLayout.getTabAt(index:0);
_tab.select();
//Close the fragment of the target tab
getSupportFragmentManager().beginTransaction().remove(new Frag_First()).commit();
// set the Fragment to show
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new Frag__Second()).commit();
break;
case
I have option menu, what im trying to do is:
I want to select a tab using option menu then replace the Fragment of the
target tab with another fragment.
the Image show the replacement fragment overlapped to the current fragment of the target tab.
help thanks
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int _id = item.getItemId();
// Select tablayout using option Menu
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
TabLayout.Tab _tab;
switch (_id)
{
case R.id.mnutabindx0Frag2: //show first tab with second fragment
//Set the Tab index to show
_tab = tabLayout.getTabAt(index:0);
_tab.select();
//Close the fragment of the target tab
getSupportFragmentManager().beginTransaction().remove(new Frag_First()).commit();
// set the Fragment to show
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new Frag__Second()).commit();
break;
case
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无需致电
//关闭目标选项卡的片段
,因为,替换呼叫删除了同一contaveerviewid
https://develover.android.android.com/reference/reference/reference/android/android/App/fragmentTransAction#replace(int,int,%20android) .App.Fragment,%20Java.lang.String)
You do not need to call
//Close the fragment of the target tab
Since, replace call removes other added fragments of a same containerViewId
https://developer.android.com/reference/android/app/FragmentTransaction#replace(int,%20android.app.Fragment,%20java.lang.String)
好的,我明白了,首先我准备两个不同的 Fragment Container,ViewPager 和 FrameLayout。
然后我使用 FameLayout 来处理替换 Fragment,之后我设置可见性
取决于我提出的条件。
Ok I got it, First I preapare two different Fragment Container, ViewPager and FrameLayout.
then I used FameLayout to handle the replacement Fragment, after that I set the visibility
depend on the condition I made.