[英]Swiping between action bar tabs on android version lower than 3.0
首先,我獲得了我的支持庫以及所有支持庫,我絕對不想使用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
找到合適的源。 我走錯了方向嗎? 我該怎么辦?
提前致謝。
我通過Eclipse幫助解決了這個問題。 我創建了一個具有滑動功能的項目,該項目適用於api級別高於11的api。我編輯了整個代碼,並在支持程序appcompat 7和4的幫助下使其正常工作。這非常簡單,並且效果很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.