一周后,我仍在尝试创建一个支持 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,它们基本相同(语法/语法代替了 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 添加新分段。然后,在连续的选项卡更改时,应该添加的选项卡被删除,而应该删除的选项卡被添加。我很感激任何建议。