【问题标题】:using actionbar tabs to switch between fragments使用操作栏选项卡在片段之间切换
【发布时间】:2013-08-22 18:01:07
【问题描述】:

一周后,我仍在尝试创建一个支持 v7 的选项卡式操作栏,以便能够在片段之间切换。这是我在原生代码中的 android 应用程序中的第一次尝试,我是 Java 新手。

我的 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,它们基本相同(用 Grammar/grammar 代替 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() 之后,我很确定发生的情况如下: 在第一个选项卡更改上,onTabUnselected 无法分离/删除任何内容,而 onTabSelected 添加新片段。然后,在连续的选项卡更改时,应该添加的选项卡被删除,而应该删除的选项卡被添加。我很感激任何建议。

【问题讨论】:

    标签: java android tabs android-actionbar


    【解决方案1】:

    希望对你有所帮助..

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

    【讨论】:

    • 感谢 bhkiran,这解决了允许从 on​​Create 方法外部访问 savedInstanceState 的基本问题。但是,我无法通过使用它重新创建碎片来解决混乱。最后,解决方案是在 manifest 文件中的 Activity 中添加 android:configChanges="orientation"。
    • 请忽略以上评论。
    猜你喜欢
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    相关资源
    最近更新 更多