如何在更改页面时更改视图寻呼机中的片段或页面

时间:2013-03-11 18:21:01

标签: android android-fragments android-viewpager

我正在开发一个在ViewPager中有3个页面的应用程序。但我在刷页面时无法更改片段。请任何人帮助我理解如何做到这一点....

public class HomePage extends FragmentActivity {

    ViewPager viewPager;
    PagerTabStrip pagerTabStrip;
    PagerAdapter adapter;
    TextView pageTag;

    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;

    /*
     * ActionBar Fragment Class
     */

    ActionBar actionBar;

    Fragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homepage);

        /*
         * Setting adapter for viewPager
         */
        viewPager = (ViewPager) findViewById(R.id.pager);
        adapter = new TabView(getSupportFragmentManager());
        viewPager.setAdapter(adapter);

        /*
         * Setting coustom Animation for ViewPager
         */

        viewPager.setPageTransformer(true, new CoustomPageTransform());

        /*
         * All properties for pagerTabStrip //
         */
        pagerTabStrip = (PagerTabStrip) findViewById(R.id.tabstrip);
        pagerTabStrip.setDrawFullUnderline(true);
        pagerTabStrip.setTabIndicatorColor(Color.parseColor("#33B5E5"));
        pagerTabStrip.setTextSpacing(80);
        pagerTabStrip.setTextColor(Color.parseColor("#666666"));

        /*
         * Setting Up ActionBar Fragment
         */

        if (savedInstanceState != null) {

            actionBar = new ActionBar();
            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.fragment_container, actionBar)
                    .commit();

        }

    }

    public class TabView extends FragmentStatePagerAdapter {

        public TabView(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        @Override
        public Fragment getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {

            switch (position) {
            case 0:

                return "TASKS";

            case 1:
                return "WORKBOOK";

            case 2:
                return "ITINERARY";
            default:
                break;
            }
            return null;
        }

    }

    public class CoustomPageTransform implements ViewPager.PageTransformer {

        float MIN_SCALE = 0.85f;
        float MIN_ALPHA = 0.5f;

        @Override
        public void transformPage(View view, float position) {

            int pageHeight = view.getHeight();
            int pageWidth = view.getWidth();

            if (position < -1) {
                view.setAlpha(0);
            } else if (position <= 1) {
                float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
                float vertMargin = pageHeight * (1 - scaleFactor) / 2;
                float horMargin = pageWidth * (1 - scaleFactor) / 2;

                if (position < 0) {

                    view.setTranslationX(horMargin - vertMargin / 2);

                } else {

                    view.setTranslationX(-horMargin + vertMargin / 2);

                }

                view.setScaleX(scaleFactor);
                view.setScaleY(scaleFactor);

                view.setAlpha(MIN_ALPHA + scaleFactor - MIN_SCALE
                        / (1 - MIN_SCALE) * (1 - MIN_ALPHA));
            } else {
                view.setAlpha(0);
            }

        }
    }

}

2 个答案:

答案 0 :(得分:1)

您没有返回任何片段......您正在适配器中返回null,所以当然您不会看到/做任何事情......

在您的适配器中,您应该保留所需片段的列表/集合,并根据该位置返回它们。

答案 1 :(得分:0)

  

尝试这种方式!我不太确定你想做什么,但是使用这段代码你可以更改并滑动&#34;页面&#34;

 public class MainActivityEPG extends FragmentActivity {

    SectionsPagerAdapter mSectionsPagerAdapter;
    static Context c;
    PagerTitleStrip pagerTabStrip;

    static ViewPager mViewPager;

    @Override
    public void onBackPressed() {
        MainActivity2.deleteBackground(false);
        finish();
        super.onBackPressed();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.epg_ui);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        pagerTabStrip = (PagerTitleStrip) findViewById(R.id.pager_title_strip);
            mViewPager.setAdapter(mSectionsPagerAdapter);
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        }

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


        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            pagerTabStrip.setAlpha(150f);
        pagerTabStrip.setBackgroundColor(Color.parseColor("#80000000"));                

                    switch (position) {
            case 0:
                    return getString(R.string.title_section1);
            case 1:
                return getString(R.string.title_section2);
            case 2:
                return getString(R.string.title_section3);
            case 3:
                return getString(R.string.title_section4);
            case 4:
                return getString(R.string.title_section5);
            case 5:
                return getString(R.string.title_section6);
            }
            return null;
        }
    }

    public static class DummySectionFragment extends Fragment {

        public static final String ARG_SECTION_NUMBER = "section_number";

        public DummySectionFragment() {

        }

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

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


            return rootView;
        }
    }

}