编辑:找到解决方案!对于任何想做同样事情的人,我在下面找到了一个更好的教程: http ://comfreek.blogspot.com/2012/06/actionbars-and-tabs-under-android-2x.html 它非常基础,展示了如何使用 ABS 设置带有片段的选项卡,以防其他人感兴趣!
我正在按照这里的教程来实现选项卡和片段:http ://www.lucazanini.eu/2012/android/tab-layout-in-android-with-actionbar-and-fragment/?lang=en
我在将其转换为与旧版本的 Android 一起使用时遇到了一些麻烦;我的目标是最低水平的姜饼,所以我必须使用 ActionBarSherlock。即使我尝试复制它说要创建的 TabActionBarActivity.java 类,我也会遇到错误。(我也不确定如何处理片段的其他两个类,但我稍后会处理这些)。
import android.app.FragmentTransaction;
import android.os.Bundle;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
public class TabActionBarActivity extends SherlockActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
String label1 = getResources().getString(R.string.label1);
Tab tab = actionBar.newTab();
tab.setText(label1);
TabListener<Tab1Fragment> t1 = new TabListener<Tab1Fragment>(this, label1, Tab1Fragment.class);
tab.setTabListener(t1);
actionBar.addTab(tab);
String label2 = getResources().getString(R.string.label2);
Tab tab = actionBar.newTab();
tab.setText(label2);
TabListener<Tab1Fragment> t2 = new TabListener<Tab1Fragment>(this, label2, Tab2Fragment.class);
tab.setTabListener(t2);
actionBar.addTab(tab);
}
private class TabListener<T extends SherlockFragment>
implements com.actionbarsherlock.app.ActionBar.TabListener{
private SherlockFragment mFragment;
private final SherlockActivity mActivity;
private final String mTag;
private final Class<T> mClass;
public TabListener(SherlockActivity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
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());
ft.add(android.R.id.content, 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.
}
}
}
我在两行出现错误,如下所示:
TabListener<Tab1Fragment> t1 = new TabListener<Tab1Fragment>(this, label1, Tab1Fragment.class);
声明“TabListener t1 = new TabListener(this, label1, Tab1Fragment.class);” 我假设当后面的代码部分修复时,这将被清除。
与选择/取消选择选项卡有关的方法中有错误,我不确定如何修复它们。任何指示或建议?