在ViewPager上多次调用碎片

时间:2016-07-30 19:49:48

标签: android android-fragments

我在我的活动中使用简单的ViewPager,其中我将一些片段附加到其中,当第一个片段附加到其中时,片段称为多个

我要测试的简单代码:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


public class MainActivity extends FragmentActivity {
    private static final int NUM_PAGES = 3;
    private ViewPager    pager;
    private PagerAdapter adapter;

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

        // Instantiate Pager & Adapter
        pager = (ViewPager) findViewById(R.id.viewpager);
        adapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);
    }

    @Override
    public void onBackPressed() {
        if(pager.getCurrentItem() == 0) {
            super.onBackPressed();
        }
        else {
            pager.setCurrentItem(pager.getCurrentItem() -1);
        }
    }

    // PagerAdapter SubClass
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            Fragment fragment;
            switch(position) {
                case 0:
                    fragment = new TutorialPage1();
                    break;
                case 1:
                    fragment = new TutorialPage1();
                    break;
                case 2:
                    fragment = new TutorialPage1();
                    break;
                default:
                    fragment = new TutorialPage1();
                    break;
            }
            return fragment;
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

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

    } /* EOC */

    public static class TutorialPage1 extends Fragment {

        private static int pageIndex;
        private static final String TAG = "TAG";

        public TutorialPage1() {
        }

        public void TutorialPage1(int id) {
            pageIndex = id;
            Log.v(TAG, "Page Index is " + pageIndex); // this logs twice
        }

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

            Log.e(TAG, "Res Index is " );
            ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.myfragment_layout, container, false);
            Toast.makeText(getActivity(), "----->", Toast.LENGTH_SHORT).show();
            return rootView;
        }
    }
} /* EOC */

1 个答案:

答案 0 :(得分:0)

思考:

<强>#1

在你的getItem()调用中,你有TutorialPage1()为每个案例调用...

<强>#2

ViewPagers确实唤醒了它们两侧的碎片,并将它们存储在内存中,以便在碎片之间快速切换,这样就可以看到你所看到的......但是你除非你能够验证修复#1