How to implement the drag function in one of the Fragment tab? ** No TAB + SWIPE **

advertisements

I'm very new to Android environment and hence this question may be simple to some of you. Please try not to be too harsh on me. Thanks.

Following the tutorial on this site: http://www.linux.com/learn/tutorials/761642-android-app-development-for-beginners-navigation-with-tabs , I've successfully created an app, having three tabs that having it's own layout.

Then I found another tutorial which is about ViewPager: http://www.androidbegin.com/tutorial/android-viewpager-gallery-images-and-texts-tutorial/

I'm trying to combine this two application to become one, which means to place the ViewPager into my tab3.

As shown on the link, tab3 need to setTabListener and expecting a Fragment class as below:

ActionBar.Tab tab3;
Fragment fragmentTab3 = new FragmentTab3();

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

tab3.setTabListener(new MyTabListener(fragmentTab3));
actionBar.addTab(tab3);

I'm totally running out of clue on how to modify the code so that it accept a class that extends PagerAdapter and allow tab3 to perform as a ViewPager


I have built a similar application where all the three tabs have viewpagers inside them.

The tab implementation is same as you mentioned:

final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);
    setContentView(R.layout.activity_main);

    actionBar.addTab(actionBar.newTab().setIcon(R.drawable.ic_action_file)
            .setTabListener(this));
    actionBar.addTab(actionBar.newTab().setIcon(R.drawable.ic_action_music)
            .setTabListener(this));
    actionBar.addTab(actionBar.newTab().setIcon(R.drawable.ic_action_video)
            .setTabListener(this));

Assign fragments to individual tabs like this:

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    Fragment mFragment;
    Log.d(TAG, "Inside on tab selected");
    switch (tab.getPosition()) {
    case 0:
        mFragment = Fragment1.newInstance(null);
        ft.replace(R.id.content, mFragment, "fragment1");
        break;

    case 1:
        mFragment = Fragment2.newInstance();
        ft.replace(R.id.content, mFragment, "fragment2");
        break;

    case 2:
        mFragment = Fragment3.newInstance();
        ft.replace(R.id.content, mFragment, "fragment3");
        break;
    default:
        break;
    }
}

Now fragment3 need not be extend pageradapter. Inside layout for 3rd fragment, add a viewpager

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_music_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/colorBackground"
tools:context="com.gridants.cubeit.ui.MusicFragment" >
    <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" >

        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/pager_title_strip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="#33b5e5"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"
            android:textColor="#fff" />
    </android.support.v4.view.ViewPager>
</FrameLayout>

Inside your fragment3 code, declare the viewpager and your custom fragmentStatePagerAdapter

    mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(
            getChildFragmentManager());
    mViewPager = (ViewPager) view.findViewById(R.id.pager);
    mViewPager.setAdapter(mDemoCollectionPagerAdapter);

Here is sample code for implementation of custom adapter

    public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
    private final FragmentManager mFragmentManager;
    public Fragment mFragmentAtPos0;
    private Context context;

    public DemoCollectionPagerAdapter(FragmentManager fm) {
        super(fm);
        mFragmentManager = fm;
    }

    @Override
    public Fragment getItem(int i) {
        Fragment mFragment;
        Bundle args;
        Log.d(TAG, "Inside on tab selected");
        switch (i) {
        case 0:
            mFragment = new InnerFragment1();
            break;

        case 1:
            mFragment = new InnerFragment2();
            break;

        case 2:
            mFragment = new InnerFragment3();
            break;
        }
        return mFragment;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        String title;
        switch (position) {
        case 0:
            title = "OBJECT " + (position + 1);
            break;

        case 1:
            title = "OBJECT " + (position + 1);
            break;

        default:
            title = "OBJECT " + (position + 1);
            break;
        }
        return title;
    }
}

Hope it helps.