I resolved this issue by eclipses help. I created a project with swipe ability which works for api level HIGHER than 11. I edited the whole code and made it work by the help of support libraries appcompat 7 and 4. It's pretty easy and works pretty well.
Swiping between action bar tabs on android version lower than 3.0
-
30-06-2022 - |
Question
First of all I got my support libraries and all and I absolutely don't want to use sherlock or some support library like that.
Here's my activity code:
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);
}
As you can see I use fragments to travel between tabs also here is my 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.
}
}
This works perfectly alone. I have my layouts and classes for every fragment ready too. But I just can't seem to find a proper source for ViewPager
for such style of code. Am I going in the wrong direction? What should I do?
Thanks in advance.
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow