首先,我得到了我的支持库,并且我绝对不想使用sherlock或类似的支持库。
这是我的活动代码:
public class MyClass ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_class);
// Get the message from the intent
Intent intent = getIntent();
}
static ActionBar actionBar;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater upInflater = getMenuInflater();
upInflater.inflate(R.menu.action_bar, menu);
// setup action bar for tabs
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab = actionBar.newTab()
.setIcon(R.drawable.ic_action_home)
.setTabListener(new TabListener<Fragment1>(
this, home, Fragment1.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setIcon(R.drawable.ic_action_cart)
.setTabListener(new TabListener<Fragment2>(
this, cart, Fragment2.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setIcon(R.drawable.ic_action_users)
.setTabListener(new TabListener<Fragment3>(
this, users, Fragment3.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setIcon(R.drawable.ic_action_products)
.setTabListener(new TabListener<Fragment4>(
this, products, Fragment4.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setIcon(R.drawable.ic_action_settings)
.setTabListener(new TabListener<Fragment5>(
this, settings, Fragment5.class));
actionBar.addTab(tab);
return super.onCreateOptionsMenu(menu);
}
正如您所看到的,我使用片段在标签之间移动也是我的TabListener:
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 callbacks */
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
actionBar.setTitle(mTag);// <===
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.
}
}
这完全独立。我也准备好了每个片段的布局和类。但我似乎无法为这种代码风格找到ViewPager
的正确来源。我走错了方向吗?我该怎么办?
提前致谢。
答案 0 :(得分:0)
我通过日食帮助解决了这个问题。我创建了一个具有滑动功能的项目,适用于高于11的api级别。我编辑了整个代码,并在支持库appcompat 7和4的帮助下使其工作。它非常简单且工作得很好。