在framelayout中实现fragment / viewPager

时间:2014-08-25 15:28:56

标签: android android-fragments android-viewpager

我有一个由3个布局组成的常规活动。我想要做的是在三个布局的 ONE 中实现 FragmentActivity ,而不是整个活动中的 FragmentActivity 。显然,当我尝试做

FragmentActivity fragment = new FragmentActivity();

它会导致活动崩溃,因为它尝试在另一个活动中创建活动("活动已被销毁" )。

所以我想做的是在框架布局中实现这个系统(滑动),到目前为止我有这个代码

onCreate

FragmentActivity fragment = new FragmentActivity(); //THIS LINE CRASHES
final ViewPager viewPager = (ViewPager) findViewById(R.id.mainPager);
final ActionBar actionBar = getActionBar();
customTabsAdapter tabsAdapter = new customTabsAdapter(fragment.getSupportFragmentManager());

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

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

                    ActionBar.TabListener tab_listener = new ActionBar.TabListener() {
                        @Override
                        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
                            viewPager.setCurrentItem(tab.getPosition());
                        }

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

                        }

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

                        }
                    };

customTabsAdapter:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class customTabsAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int index) {

        switch (index) {
            case 0:
                // Top Rated fragment activity
                return new type1();
            case 1:
                // Games fragment activity
                return new type2();
            case 2:
                // Movies fragment activity
                return new type3();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 3;
    }

}

型" X":

import android.os.Bundle;
        import android.support.v4.app.Fragment;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;

public class typeX extends Fragment {

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

        View rootView = inflater.inflate(R.layout.typeX_layout, container, false);

        return rootView;

    }
}

如果有人能帮我理解如何做到这一点,我将深表感激......

0 个答案:

没有答案