2

I have Activity for tabLayout using ViewPager.

Code :

public class Home extends FragmentActivity implements ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;

private String[] tabs = { "Instant Opportunity", "Events", "Experts" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

} And another activity for Slider, code is as below :

private void displayView(int position) {
    // update the main content by replacing fragments

    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new Home();
        break;
    case 1:
        fragment = new Gallery();
        break;
    default:
        break;
    }

Now, here at case 0: I want to call that Home activity. But It is showing error. How to call this ?

When I take cursor on new Home(), It says can't convert from Home to Fragment.

Jeeten Parmar
  • 5,568
  • 15
  • 62
  • 111
  • what error you got? post it. – M D Apr 13 '14 at 11:25
  • 1
    `Home` must to be a Fragment and not an Activity. You **cannot** put an Activity as an item inside a ViewPager! Make sure that `Home` is a Fragment. – Blo Apr 13 '14 at 11:29
  • Go to this [http://www.truiton.com/2013/05/android-fragmentpageradapter-example/](http://www.truiton.com/2013/05/android-fragmentpageradapter-example/) – M D Apr 13 '14 at 11:32
  • The error appears because indeed `Home` is not a Fragment. As I understand, `Home` is a Fragment which have an Inner ViewPager, isn't it? – Blo Apr 13 '14 at 11:55
  • yes, but how to prevent this problem ? I want to show ViewPager in home activity. – Jeeten Parmar Apr 13 '14 at 12:02

1 Answers1

1

You can use a ViewPager inside a Fragment. You have to use inner fragments with the nested fragments method, as you can read on the Documentation:

You can now embed fragments inside fragments. This is useful for a variety of situations in which you want to place dynamic and re-usable UI components into a UI component that is itself dynamic and re-usable. For example, if you use ViewPager to create fragments that swipe left and right and consume a majority of the screen space, you can now insert fragments into each fragment page.

You need to change Home as extends Fragment and use getChildFragmentManager() method for your adapter. There are some revelant posts on this kind of behaviour:

Hope this helps.

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99