Skip to content Skip to sidebar Skip to footer

How Can I Implementing Swipe View In Actiontab And Fragments Within Multiple Fragments?

I am working on Actionbar.TabListener and Fragments Tab. Fragments contains multiple Fragments that are generated via clickable buttons on each tabbed Fragments. My MainActivity a

Solution 1:

After some research on the google i found that we can achieve swipe view in fragments with Tab with the use of ViewPager

Go to Android Studio and select TabActivity (Swipe with ViewPager template)

first we have to extend fragmentAcitvity with MainActivity, and that should be implemented with ActionBar.TabListener

The Code is as follows..

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

@SuppressWarnings("ALL")
publicclassMainActivityextendsFragmentActivityimplementsActionBar.TabListener{
    ActionBar actionbar;
    publicstaticViewPager viewpager;
    FragmentPageAdapter ft;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewpager = (ViewPager) findViewById(R.id.pager);
        ft = newFragmentPageAdapter(getSupportFragmentManager());
        actionbar = getActionBar();
        viewpager.setAdapter(ft);

        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionbar.addTab(actionbar.newTab().setText("TAB1").setTabListener(this));
        actionbar.addTab(actionbar.newTab().setText("TAB2").setTabListener(this));
        actionbar.addTab(actionbar.newTab().setText("TAB3").setTabListener(this));
        actionbar.addTab(actionbar.newTab().setText("TAB").setTabListener(this));
        viewpager.setOnPageChangeListener(newViewPager.OnPageChangeListener() {
            @OverridepublicvoidonPageSelected(int arg0) {
                actionbar.setSelectedNavigationItem(arg0);
            }
            @OverridepublicvoidonPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }
            @OverridepublicvoidonPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub
            }
        });
    }
    @OverridepublicvoidonTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
    }
    @OverridepublicvoidonTabSelected(Tab tab, FragmentTransaction ft) {
        viewpager.setCurrentItem(tab.getPosition());
    }
    @OverridepublicvoidonTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
    }
}

XML for MainActivity .... activity_mail.xml will be...

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".MainActivity" />

FragmentpageAdapter

publicclassFragmentPageAdapterextendsFragmentPagerAdapter {

    publicFragmentPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Overridepublic Fragment getItem(int arg0) {
        switch (arg0){
            case0: returnnewTab1Fragment();

            case1: returnnewTab2Fragment();

            case2: returnnewTab3Fragment();

            case3: returnnewTab4Fragment();

            default:break;
        }
        returnnull;
    }

    @OverridepublicintgetCount() {
        return4;
    }

    @Overridepublic CharSequence getPageTitle(int position) {
        return getPageTitle(4);
    }
}

If you take By default Template in Android Studio as mentioned above there will be an fragment_main file also ...

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity$PlaceholderFragment">

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/toolbar"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:popupTheme="@style/ThemeOverlay.AppCompat.Light">
        </android.support.v7.widget.Toolbar>

    </RelativeLayout>

After that implement all Fragments with related Tabs that you declared and added in FragmentPagerAdapter and MainActivity.

You have to set activity theme in Androidmenifest.xml

"@android:style/Theme.Holo.Light"

and also in style.xml

<stylename="AppTheme"parent="Theme.AppCompat">

Post a Comment for "How Can I Implementing Swipe View In Actiontab And Fragments Within Multiple Fragments?"