在ViewPager中使用相同的Fragment类

时间:2015-06-03 05:20:25

标签: android android-fragments android-viewpager

我有一个ViewPager我希望对Fragment所有Views使用相同的View类型,但我遇到了问题。实际上只更新了第一个Fragment。如果我向左/向右滑动,我会得到一个空的Fragment,如果我向后滑动,我可以看到应该在第一次滑动中实例化的public class HymnSwipe extends ActionBarActivity { private static final int NUM_PAGES = 900; private ViewPager mPager; private PagerAdapter mPagerAdapter; PrefManager prefs; int startNumber; @Override protected void onCreate(Bundle savedInstanceState) { BarColorizer bar = new BarColorizer(this); prefs = new PrefManager(this); if (prefs.getNightMode()) { setTheme(R.style.Theme_AppCompat); bar.setColor(R.color.grey_900); } else { bar.setTheme(); } super.onCreate(savedInstanceState); setContentView(R.layout.fragment_hymn_slider); if (android.os.Build.VERSION.SDK_INT >= 11) { bar.setActivity(this); } startNumber = getIntent().getExtras().getInt("number"); // Instantiate a ViewPager and a PagerAdapter. mPager = (ViewPager) findViewById(R.id.vpHymnPager); mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); mPager.setAdapter(mPagerAdapter); mPager.setCurrentItem(startNumber, true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } private class ScreenSlidePagerAdapter extends FragmentPagerAdapter { public ScreenSlidePagerAdapter(FragmentManager fm) { super(fm); } @Override public int getCount() { return NUM_PAGES; } @Override public Fragment getItem(int position) { return FragmentShowHymn.newInstance(position); } } } 在那里被实例化,我有点困惑。这是我的代码: 活动:

public class FragmentShowHymn extends Fragment implements View.OnTouchListener, View.OnKeyListener {
    private int mHymnNumber;

    TextView tvTitle, tvHymn, tvCategory, tvGama;
    ImageView ivSheet, ivMP3;
    SeekBar hymnTextSize;
    LinearLayout llImageButtons;

    ImageButton ibPlayPause, ibStop;
    Bitmap bmpPlay, bmpPause, bmpStop, heart;
    boolean ibPlayPauseIsOnPlay = true;
    SoundProcessor sound;
    public static ProgressDialog progress;
    Menu menu;
    boolean isFavorited = false;

    String hymnNumber;

    DatabaseHelper database;

    boolean textReady = false;

    ImageProcessor ip;
    File musicSheet;

    int mediaButtons, sheetAndMediaButtons;

    Intent sharingIntent = null;
    public static boolean isMusicPlaying = false;

    PrefManager prefs;

    // pinchZoom
    final static float STEP = 200;
    float mRatio = 1.0f;
    int mBaseDist;
    float mBaseRatio;
    float maxFontSize = 50;

    GestureDetector gestureDetector;

    ScrollView swScroll;

    PowerManager.WakeLock wakeLock;

    public FragmentShowHymn() {

    }

    public FragmentShowHymn(int hymnNumber) {
        mHymnNumber = hymnNumber;
    }

    public static FragmentShowHymn newInstance(int position) {
        FragmentShowHymn fragmentShowHymn = new FragmentShowHymn();
        Bundle args = new Bundle();
        args.putInt("hymnNumber", position);
        fragmentShowHymn.setArguments(args);
        return fragmentShowHymn;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_show_hymn, container, false);
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;

        mediaButtons = width / 6;
        sheetAndMediaButtons = width / 6;

        prefs = new PrefManager(getActivity());

        mHymnNumber = getArguments().getInt("hymnNumber");

        init();
        showText();
        initShare();
    }
    //other methods for initializing views
}

片段:

{{1}}

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

我已经解决了这个问题。问题是我使用的是getActivity().findViewById(),而且始终在ViewPager上显示视图。我从onViewCreated切换到onActivityCreated,现在我正在使用onViewCreated提供的视图作为参数,通过id view.findViewById()获取我的观看次数......现在它完美的工作..谢谢!

答案 1 :(得分:1)

如果您尝试使用onViewCreated()代替onActivityCreated(),该怎么办?这是我在代码中看到的唯一不寻常的东西。

答案 2 :(得分:0)

您正在" getItem(int position)"中创建单个片段。方法。在ScreenSlidePagerAdapter构造函数中传递一些片段数组,并在getItem方法中从该数组中获取片段。 看看这个: enter link description here