从选项卡关闭并替换片段并关闭当前片段

发布于 2025-01-17 07:36:34 字数 1058 浏览 1 评论 0原文

我有选项菜单,我想做的是: 我想使用选项菜单选择一个选项卡,然后替换该选项卡的片段 目标选项卡与另一个片段。

该图像显示与目标选项卡的当前片段重叠的替换片段。

帮助谢谢

在此输入图像描述

@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

enter image description here

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

软糖 2025-01-24 07:36:34

您无需致电

//关闭目标选项卡的片段

      getSupportFragmentManager().beginTransaction().remove(new Frag_First()).commit();

,因为,替换呼叫删除了同一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

      getSupportFragmentManager().beginTransaction().remove(new Frag_First()).commit();

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)

请叫√我孤独 2025-01-24 07:36:34

好的,我明白了,首先我准备两个不同的 Fragment Container,ViewPager 和 FrameLayout。
然后我使用 FameLayout 来处理替换 Fragment,之后我设置可见性
取决于我提出的条件。

    //Fragment Container Visibility method
private void _setvisibility_FragContainers(FrameLayout fl, ViewPager vp,boolean flflag)
    {
        if(vp.getVisibility() == View.INVISIBLE & flflag ==false )
        {
           vp.setVisibility(View.VISIBLE);
           fl.setVisibility(View.INVISIBLE);
        }
        else if(fl.getVisibility() == View.INVISIBLE & flflag == true) {
          vp.setVisibility(View.INVISIBLE);
          fl.setVisibility(View.VISIBLE);
        }
    }


//Option menu
 @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id_item = item.getItemId();



        switch (id_item)
        {
            case R.id.mnutabindx0Frag2: //show first tab and second fragment
              // select tablayout by option menu
                SelectTabUsingOptionMenu(tab_layout,0);
              // show FrameLayout with frame on it
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new Frag_Second()).commit();
                break;

            default:return false;
        }
// Clear the viewpager visibility
        _setvisibility_FragContainers(frameLayout_fragcon,view_pager,true);

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.

    //Fragment Container Visibility method
private void _setvisibility_FragContainers(FrameLayout fl, ViewPager vp,boolean flflag)
    {
        if(vp.getVisibility() == View.INVISIBLE & flflag ==false )
        {
           vp.setVisibility(View.VISIBLE);
           fl.setVisibility(View.INVISIBLE);
        }
        else if(fl.getVisibility() == View.INVISIBLE & flflag == true) {
          vp.setVisibility(View.INVISIBLE);
          fl.setVisibility(View.VISIBLE);
        }
    }


//Option menu
 @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id_item = item.getItemId();



        switch (id_item)
        {
            case R.id.mnutabindx0Frag2: //show first tab and second fragment
              // select tablayout by option menu
                SelectTabUsingOptionMenu(tab_layout,0);
              // show FrameLayout with frame on it
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        new Frag_Second()).commit();
                break;

            default:return false;
        }
// Clear the viewpager visibility
        _setvisibility_FragContainers(frameLayout_fragcon,view_pager,true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文