片段在更改选项卡时在tabhost中重复

时间:2015-03-24 10:13:05

标签: android android-fragments fragment-tab-host

我正在使用FragmentTabHost处理Fragment。我的FragmentTabHost有4个不同的fragments但是当我更改标签时,经过一些更改后,2或3个标签具有相同的片段。像index0 index1 index3有SettingFragment。但他们有不同的fragment。这是我在 MainActivityFragment 中设置标签的代码,它扩展了FragmentActivity

private void setTabs() {
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);



//  TabHost.TabSpec spec;
    //Intent intent;

     mTabHost.addTab(
                mTabHost.newTabSpec("tab1").setIndicator(null, getResources().getDrawable(R.drawable.icon_alert_tab)),
                AlertFragment.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab2").setIndicator(null, getResources().getDrawable(R.drawable.icon_wlmuser_tab)),
                UserFragment.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab3").setIndicator(null, getResources().getDrawable(R.drawable.icon_chat_tab)),
                ChatFragment.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab4").setIndicator(null, getResources().getDrawable(R.drawable.icon_setting_tab)),
                SettingFragment.class, null);

        mTabHost.getTabWidget().setStripEnabled(false);
        mTabHost.setCurrentTab(1);
    }

我的4个片段是AlertFragment,UserFragment,ChatFragment,SettingFragment,它们有按钮。在点击按钮上,另一个片段由FragmentTransaction

打开 UserFragment中的

LIKE 我通过inviteBtn按钮

在FrameLayout中调用ContectFragment
inviteBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // replace with contact fragment
            fragment = new ContactFragment();

            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left);
            ft.replace(android.R.id.tabcontent, fragment);
            ft.addToBackStack(null);
            // Start the animated transition.
            ft.commit();
        }
    });

。我在fragments内打开时会出现问题。

1 个答案:

答案 0 :(得分:1)

您好我已经解决了您的问题,请参阅以下解决方案。理想情况下,您必须创建一个额外的BaseContainerFragment。它有framelayout。接下来,为每个静态片段创建容器片段。假设我有两个片段与它相关的容器片段。见下面的代码。

原始片段:

  1. AlertFragment.java
  2. ChatFragment.java
  3. BaseContainerFragment.java

    公共类BaseContainerFragment扩展了Fragment {

        public BaseContainerFragment() {
            // Required empty public constructor
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        public void replaceFragment(Fragment fragment, boolean addToBackStack) {
            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            if (addToBackStack) {
                transaction.addToBackStack(null);
            }
            transaction.replace(R.id.container_framelayout, fragment);
            transaction.commit();
            getChildFragmentManager().executePendingTransactions();
        }
    
        public boolean popFragment() {
            boolean isPop = false;
            if (getChildFragmentManager().getBackStackEntryCount() > 0) {
                isPop = true;
                getChildFragmentManager().popBackStack();
            }
            return isPop;
        }
    
    }
    

    容器片段扩展了上面的BaseContainerFragment:

    1. AlertContainerFragment.java扩展了BaseContainerFragment.java
    2. ChatContainerFragment.java扩展了BaseContainerFragment.java
    3. 现在在FragmentActivity中,你必须在FragmentTabHost中添加每个片段的ContainerFragment。见下面的代码。

      import android.os.Bundle;
      import android.support.v4.app.FragmentActivity;
      import android.support.v4.app.FragmentTabHost;
      import android.view.Menu;
      import android.view.MenuItem;
      
      
      public class TestSukhwantTabHost extends FragmentActivity  {
      
          private FragmentTabHost mTabHost;
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_test_sukhwant_tab_host);
              mTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
              mTabHost.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout);
      
              mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator(null, getResources().getDrawable(R.drawable.ic_launcher)),AlertContainerFragment.class, null);
              mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator(null, getResources().getDrawable(R.drawable.ic_launcher)),ChatContainerFragment.class, null);
              mTabHost.getTabWidget().setStripEnabled(false);
              mTabHost.setCurrentTab(0);
          }
      }
      

      现在我发布了Container Fragment代码,这个容器片段实际上加载了我们原来的Fragment。要在片段中加载片段,我们使用容器片段方法来替换新片段。

      <强> fragment_base_container.xml

       <?xml version="1.0" encoding="utf-8"?>
       <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/container_framelayout"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
       </FrameLayout>
      

      <强> AlertContainerFragment.java

      import android.os.Bundle;
      import android.support.annotation.Nullable;
      import android.support.v4.app.Fragment;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      
      
      public class AlertContainerFragment extends BaseContainerFragment {
      
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              // Inflate the layout for this fragment
              return inflater.inflate(R.layout.fragment_base_container, container, false);
          }
      
      
          @Override
          public void onActivityCreated(@Nullable Bundle savedInstanceState) {
              super.onActivityCreated(savedInstanceState);
              initView();
          }
      
          private void initView() {
              replaceFragment(new AlertFragment(), false);
          }
      }
      

      <强> ChatContainerFragment.java

      import android.os.Bundle;
      import android.support.annotation.Nullable;
      import android.support.v4.app.Fragment;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      
      
      public class ChatContainerFragment extends BaseContainerFragment {
      
      
          public ChatContainerFragment() {
              // Required empty public constructor
          }
      
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              // Inflate the layout for this fragment
              return inflater.inflate(R.layout.fragment_base_container, container, false);
          }
      
          @Override
          public void onActivityCreated(@Nullable Bundle savedInstanceState) {
              super.onActivityCreated(savedInstanceState);
              initView();
          }
      
          private void initView() {
              replaceFragment(new ChatFragment(), false);
          }
      }
      

      原始片段:

      以下是 AlertFragment.java

      import android.os.Bundle;
      import android.support.v4.app.Fragment;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.Button;
      
      
      public class AlertFragment extends Fragment {
      
          private Button framgnet1_button;
      
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              // Inflate the layout for this fragment
              View mView = inflater.inflate(R.layout.fragment_alert, container, false);
              framgnet1_button = (Button) mView.findViewById(R.id.framgnet1_button);
              framgnet1_button.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      ((BaseContainerFragment)getParentFragment()).replaceFragment(new DynamicAlertFramgnet(), false);
                  }
              });
              return mView;
          }
      }
      

      <强> ChatFragment.java

      import android.os.Bundle;
      import android.support.v4.app.Fragment;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.Button;
      
      
      public class ChatFragment extends Fragment {
      
          private Button button_chart_fragment;
      
          public ChatFragment() {
              // Required empty public constructor
          }
      
      
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {
              View mView = inflater.inflate(R.layout.fragment_chat, container, false);
              button_chart_fragment = (Button)mView.findViewById(R.id.button_chart_fragment);
              button_chart_fragment.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      ((BaseContainerFragment)getParentFragment()).replaceFragment(new DynamicChartFramgnet(), false);
                  }
              });
              return mView;
          }
      }
      

      在我上面的代码实现中,每个原始片段在你点击时都有按钮,它会加载新的片段。

      如果您有任何疑问,请告诉我。请求提出你的建议。

      谢谢