使用操作栏选项卡在片段之间切换

时间:2013-08-22 18:01:08

标签: java android tabs android-actionbar

一周后,我仍在努力创建一个带有v7支持的标签式操作栏,以便能够在片段之间切换。这是我在本机代码中的Android应用程序的第一次努力,我是Java新手。

My Main.Activity.java看起来像这样

package com.example.appcompattest;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main);
    // setup action bar for tabs
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayShowTitleEnabled(true);

    Tab tab = actionBar.newTab()
                       .setText(R.string.grammar)
                       .setTabListener(new TabListener<GrammarFragment>(
                               this, "GRAMMAR", GrammarFragment.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab()
                   .setText(R.string.lexis)
                   .setTabListener(new TabListener<LexisFragment>(
                           this, "LEXIS", LexisFragment.class));
    actionBar.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;
}


public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    /** Constructor used each time a new tab is created.
      * @param activity  The host Activity, used to instantiate the fragment
      * @param tag  The identifier tag for the fragment
      * @param clz  The fragment's Class, used to instantiate the fragment
      */

    public TabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    /* The following are each of the ActionBar.TabListener call backs */
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // Check if the fragment is already initialized
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            // Commit the transaction
            ft.add(R.id.fragment_holder, mFragment, mTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach( mFragment);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(mFragment);
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // User selected the already selected tab. Usually do nothing.
    }
}

}

除此之外,我在activity_main.xml中有一个名为fragment_holder的Framelayout,用于交换选项卡的输入和输出,并为我的两个片段分隔xml文件。我还有两个类,LexisFragment.java和GrammarFragment.java基本相同(用语法/语法代替Lexis / lexis),看起来像这样。

public class LexisFragment extends Fragment {
    @Override   
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.lexisfragment, container, false);
    }

当我运行活动时,会添加第一个片段。选择另一个选项卡会导致添加第二个片段,但不会破坏第一个片段。在随后的选项卡更改中,片段将保持叠加。

在每一步记录变量(mTag和mFragment)并尝试使用ft.remove()后,我很确定发生的情况如下: 在onTabSelected添加新片段时,在第一个选项卡上更改onTabUnselected无法分离/删除任何内容。然后,在更改选项卡更改时,将删除应添加的选项卡,同时添加应删除的选项卡。我很感激任何建议。

1 个答案:

答案 0 :(得分:2)

希望这可以帮助你..

public class AboutActivity extends Activity {
boolean mIsFromMem;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mIsFromMem=savedInstanceState!=null;
}}