Add Viewpager To Tabs
I have the code here with tabs and It works when you select each tab but I cannot swipe to go to next tab. Each tab has listview which has its own activity.Only thing I want is to
Solution 1:
Allright I see that you are not using the ViewPager
in the xml. Because I have not tried it the way you are doin it, I recommend it will be a lot easier if you do it like the following examples:
Creating Swipe Views with TabsAndroid Tab Layout with Swipeable Views
Still not useful then you can read this
Hope it Helps you. Cheers :)
Solution 2:
Use fragments for inner pages. Create a class that extends FragmentPagerAdapter for tab buttons.
Example :
activity_main.xml
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
AndroidTabAndListView.java
publicclassAndroidTabAndListViewextendsFragmentActivityimplementsActionBar.TabListener {
TabSpecNames mAppSectionsPagerAdapter;
ViewPager mViewPager;
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three primary sections// of the app.
mAppSectionsPagerAdapter = newTabSpecNames(getSupportFragmentManager());
// Set up the action bar.finalActionBaractionBar= getActionBar();
// Specify that the Home/Up button should not be enabled, since there is no hierarchical// parent.
actionBar.setHomeButtonEnabled(false);
// Specify that we will be displaying tabs in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set up the ViewPager, attaching the adapter and setting up a listener for when the// user swipes between sections.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(newViewPager.SimpleOnPageChangeListener() {
@OverridepublicvoidonPageSelected(int position) {
// When swiping between different app sections, select the corresponding tab.// We can also use ActionBar.Tab#select() to do this if we have a reference to the// Tab.
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.for (inti=0; i < mAppSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by the adapter.// Also specify this Activity object, which implements the TabListener interface, as the// listener for when this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mAppSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
@OverridepublicvoidonTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@OverridepublicvoidonTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@OverridepublicvoidonTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
publicstaticclassTabSpecNamesextendsFragmentPagerAdapter {
publicTabSpecNames(FragmentManager fm) {
super(fm);
}
@Overridepublic Fragment getItem(int i) {
switch (i) {
case0:
returnnewInboxFragment();
case1:
returnnewOutboxFragment();
case2:
returnnewProfileFragment();
default:
break;
}
returnnull;
}
@OverridepublicintgetCount() {
return3;
}
@Overridepublic CharSequence getPageTitle(int position) {
String sections[] = {"Inbox", "Outbox", "Profile"};
return sections[position];
}
}
}
fragment_inbox.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/tv1"android:layout_width="match_parent"android:layout_height="match_parent"android:text="Inbox (99)"/></LinearLayout>
InboxFragment.java
publicclassInboxFragmentextendsFragment {
@Overridepublic View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
ViewrootView= inflater.inflate(R.layout.fragment_inbox,
container, false);
return rootView;
}
}
Post a Comment for "Add Viewpager To Tabs"