0

I have already created an app that has one activity which has many fragments implemented with tablistener.

I now want to add horizontal swipe gestures between the fragments but its proving very difficult.

My first question is, is there a way to just detect the swipe within the base activity (even with a fragment overlayed)?

Or what is the simplest way to retro-fit the app with a simple swipe gestures?

Skeleton code below:

<------------------Main Activity--------------->

public class MainActivity extends FragmentActivity implements TabListener {

ActionBar bar = getActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        Tab tab = bar.newTab();
        tab.setText("TAB1");
        tab.setTabListener(this);
        bar.addTab(tab);

        tab = bar.newTab();
        tab.setText("TAB2");
        tab.setTabListener(this);
        bar.addTab(tab);


}

public void onTabSelected(Tab tab, FragmentTransaction ft) {

        if (tab.getPosition() == 0) {

            Fragment newFragment = new Tab1Fragment();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(android.R.id.content, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
            vb.vibrate(50);
}

<------------------Tab 1 Fragment--------------->

public class Tab1Fragment extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        v = inflater.inflate(R.layout.tab1_fragment, null);
}
}
user2229747
  • 221
  • 2
  • 20

2 Answers2

0

Have a ExampleFragment class defined that extends Fragment. Something like shown below

public class ExampleFragment extends Fragment {

    private View categoryView;
    private ViewPager mViewPager;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        categoryView = inflater.inflate(R.layout.viewpager, container, false);
        mViewPager = (ViewPager) categoryView.findViewById(R.id.pager);
        return categoryView;
    }

    public ViewPager returnViewPager() {
        return mViewPager;
    }
}

main_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    tools:context=".ExampleFragmentActivity" >

    <fragment
        android:id="@+id/main_fragment"
        android:name="somepackage.ExampleFragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

And then add this in the onCreate() of ExampleFragmentActivity

ExampleFragment fragment = (ExampleFragment) getSupportFragmentManager().findFragmentById(R.id.main_fragment);
mViewPager = fragment.returnViewPager();

fragmentManager = getSupportFragmentManager();
VikramV
  • 1,111
  • 2
  • 13
  • 31
0

The proper way to do it is using FragmentPagerAdapter. You can find the docs with examples here: http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html

Ripityom
  • 426
  • 1
  • 5
  • 12