片段没有附加到相应的片段

时间:2014-02-09 10:28:52

标签: android android-fragments tabs fragment android-tabs

我正在尝试将ActionBar Tabs附加到尊重片段,但它不起作用。我不是没有我错的地方。

这是我的FragmentActivity

   public class MainActivity extends FragmentActivity implements TabListener {

    String cPlusPlusFrag = "CPP", javaFrag = "Java";

    CPlusPlusFragment cPlusPlusFragment;
    JavaFragment javaFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar mAction = getActionBar();
        mAction.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        createTab(mAction, cPlusPlusFrag);
        createTab(mAction, javaFrag);
    }

    public void createTab(ActionBar mActionBar, String tabName) {
        Tab tab = mActionBar.newTab();
        tab.setText(tabName);
        tab.setTabListener(this);
        mActionBar.addTab(tab);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        Toast.makeText(this, tab.getText() + "Re-Selected", Toast.LENGTH_LONG)
                .show();
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        if (cPlusPlusFrag.equals(tab.getText())) {
            if (cPlusPlusFragment == null) {            
                cPlusPlusFragment = new CPlusPlusFragment();                
                ft.add(android.R.id.content, cPlusPlusFragment);                
            }
        } else if (javaFrag.equals(tab.getText())) {
            if (javaFragment == null) {
                javaFragment = new JavaFragment();  
                ft.add(android.R.id.content, javaFragment);     
            } 
        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        Toast.makeText(this, tab.getText() + " Un-Selected", Toast.LENGTH_LONG)
                .show();
        if (cPlusPlusFrag.equals(tab.getText().toString())) {
            ft.detach(cPlusPlusFragment);
        } else if (javaFrag.equals(tab.getText().toString())) {
            ft.detach(javaFragment);
        }
    }

}

Java Fragment代码。

public class JavaFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View viewToInflate = inflater.inflate(R.layout.java_frag_layout,
                container);
        return viewToInflate;
    };
}

C ++片段代码。

public class CPlusPlusFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View viewToInflate = inflater.inflate(R.layout.cplusplus_frag_layout,
                    container);     

            return viewToInflate;
        }
    }

2 个答案:

答案 0 :(得分:2)

除了您没有提交片段更改这一事实外,我认为您的代码一见不错。

您拨打getFragmentManager().beginTransaction().的每次都必须在此行的末尾拨打.commit(),如下所示:

getFragmentManager().beginTransaction().replace(R.id.main_layout, javaFragment).commit();

希望有所帮助:)

答案 1 :(得分:2)

问题出在

方法: onTabSelectedonTabUnSelected

和变量: cPlusPlusFragmentjavaFrag(您未在任何地方声明/创建过它们)

*溶液*

// The following variables are at Class level (data members)
String cPlusPlusFragment = "C++";
String javaFrag = "Java"

// Pass them in the method createTab accordingly
createTab(mActionBar, cPlusPlusFragment);
createTab(mActionBar, javaFrag);

// Modify you onTabSelected and onTabUnselected methods as follows:

@Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        if (cPlusPlusFrag.equals(tab.getText().toString)) {
            if (cPlusPlusFragment == null) {
                cPlusPlusFragment = new CPlusPlusFragment();

                // No need to Create custom FragmentTransaction, use 
                // one that is passed in the argument

                 ft.add(android.R.id.content, cPlusPlusFragment);
            } else {
                ft.attach(cPlusPlusFragment);
            }

        } else if (javaFrag.equals(tab.getText().toString)) {
            if (javaFragment == null) {
                javaFragment = new JavaFragment();
                ft.add(android.R.id.content, javaFragment);
            } else {
                ft.attach(javaFragment);
            }
        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        Toast.makeText(this, tab.getText() + " Un-Selected", Toast.LENGTH_LONG)
                .show();
        if (cPlusPlusFrag.equals(tab.getText().toString)) {
            ft.detach(cPlusPlusFragment);
        } else if (javaFrag.equals(tab.getText().toString)) {
            ft.detach(javaFragment);
        }
    }

我100%确定这会解决您的问题:)