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

fragment Repeat in tabhost when changing the tab

我正在 FragmentTabHostFragment 合作。我的 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 中点赞 我通过 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.

中打开时出现问题

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

原始片段:

  1. AlertFragment.java
  2. ChatFragment.java

BaseContainerFragment.java

public class BaseContainerFragment 扩展片段 {

    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

现在,在您的 FragmentActivity 中,您必须在 FragmentTabHost.see 下面的代码中添加每个片段的 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);
    }
}

现在我post Container Fragment代码,这个容器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;
    }
}

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

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

谢谢