2

我被这个问题困住了。我正在尝试在我swipe的各种fragments应用程序之间插入。滑动本身可以完美运行,但问题是,我不明白为什么,我在各种片段中定义的动作不被各种toggle / buttons等识别。换句话说,它只是在各种layouts. 这是我的代码(删除了不必要的部分)

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;
    }
    }

}

这是我的主要活动,如您所见,选项卡设置正确。举个例子Performance.java!目前,所有各种切换和按钮都有一个无法识别的定义功能

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;
    }

谢谢您的帮助 :)

4

0 回答 0