Question

I'm stuck with this problem. I'm trying to insert the swipe between the various fragments of my app. The swipe itself works perfectly but the problem, and I do not understand why, is that the actions that I have defined in various fragments are not recognized by the various toggle / buttons etc. In other words it just swipe between the various layouts. Here is my code (removed the unnecessary portion)

public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener {

    CollectionPagerAdapter mCollectionPagerAdapter;
    ViewPager mViewPager;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    {
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec("su");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }

    mCollectionPagerAdapter = new CollectionPagerAdapter(
        getSupportFragmentManager());

    final ActionBar actionBar = getActionBar();
    actionBar.setHomeButtonEnabled(false);

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCollectionPagerAdapter);
    mViewPager
        .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
            }
        });

    for (int i = 0; i < mCollectionPagerAdapter.getCount(); i++) {
        actionBar.addTab(actionBar.newTab()
            .setText(mCollectionPagerAdapter.getPageTitle(i))
            .setTabListener(this));
    }
    }

    public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    }

    public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    mViewPager.setCurrentItem(tab.getPosition());
    }

    public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    }

    public class CollectionPagerAdapter extends FragmentPagerAdapter {

    final int NUM_ITEMS = 10; // number of tabs

    public CollectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new TabFragment();
        Bundle args = new Bundle();
        args.putInt(TabFragment.ARG_OBJECT, i);
        fragment.setArguments(args);
        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        String tabLabel = null;
        switch (position) {
        case 0:
        tabLabel = getString(R.string.tabWelcome);
        break;
        case 1:
        tabLabel = getString(R.string.tabPerformance);
        break;
        case 2:
        tabLabel = getString(R.string.tabBattery);
        break;
        case 3:
        tabLabel = getString(R.string.tabNetwork);
        break;
        case 4:
        tabLabel = getString(R.string.tabImages);
        break;
        case 5:
        tabLabel = getString(R.string.tabAudio);
        break;
        case 6:
        tabLabel = getString(R.string.tabGps);
        break;
        case 7:
        tabLabel = getString(R.string.tabKernel);
        break;
        case 8:
        tabLabel = getString(R.string.tabMisc);
        break;
        case 9:
        tabLabel = getString(R.string.tabTools);
        break;
        }

        return tabLabel;
    }
    }

    public static class TabFragment extends Fragment {

    public static final String ARG_OBJECT = "object";

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

        Bundle args = getArguments();
        int position = args.getInt(ARG_OBJECT);

        int tabLayout = 0;
        switch (position) {
        case 0:
        tabLayout = R.layout.welcome;
        break;
        case 1:
        tabLayout = R.layout.performance;
        break;
        case 2:
        tabLayout = R.layout.battery;
        break;
        case 3:
        tabLayout = R.layout.network;
        break;
        case 4:
        tabLayout = R.layout.images;
        break;
        case 5:
        tabLayout = R.layout.audio;
        break;
        case 6:
        tabLayout = R.layout.gps;
        break;
        case 7:
        tabLayout = R.layout.kernel;
        break;
        case 8:
        tabLayout = R.layout.misc;
        break;
        case 9:
        tabLayout = R.layout.tools;
        break;
        }

        View rootView = inflater.inflate(tabLayout, container, false);

        return rootView;
    }
    }

}

This is my main activty, and as you can see, the tabs are set correctly. Take for an example Performance.java! at the moment, all the various toggle and buttons have a defined function that is not recognized

public class PerformanceActivity extends ListFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.performance, container, false);
                //blablabla all my buttons and toggles (THAT WORKS BEFORE THE IMPLEMENTATION OF THE SWIPE)

return v;
    }

Thanks for the help :)

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top