0

I have a ViewPager and I want to use the same Fragment type for all it's Views but I have a problem. Only the first View is actually updated. If I swipe to left/right I get an empty Fragment and if I swipe back I can see that the Fragment that should have instantiated in the first swipe is instantiated there and I'm kind of confused.. This is my code : Activity :

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

Fragment :

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
}

How can I fix this?

4
  • 1
    What if you try to use onViewCreated() instead of inActivityCreated()? This is the only unusual thing I can see in your code. Commented Jun 3, 2015 at 6:35
  • Show your activity_show_hymn layout Commented Jun 3, 2015 at 9:22
  • 1
    Thanks @beworker.. I managed to fix the issue using part of the solution you provided! Commented Jun 3, 2015 at 9:46
  • Great! I added the comment as answer. Feel free to accept it if you want to. Commented Jun 3, 2015 at 9:49

3 Answers 3

3

I have fixed the problem. The issue was I was using getActivity().findViewById() and in was always getting the views visible on the ViewPager. I switched to onViewCreated from onActivityCreated and now I'm using the view provided by onViewCreated as a parameter to get my views by id like this view.findViewById()... Now it works perfectly.. Thank you!

Sign up to request clarification or add additional context in comments.

Comments

1

What if you try to use onViewCreated() instead of onActivityCreated()? This is the only unusual thing I can see in your code.

1 Comment

Using your suggestion I've adapted this.. Thanks again
0

You are creating single fragment in "getItem(int position)" method. Pass some array of fragments in ScreenSlidePagerAdapter constructor and get fragment from that array in getItem method. Have a look in this : enter link description here

2 Comments

Yes but I have 900 fragments.. Should I instantiate them all and pass array as parameter?
Then you should use FragmentStatePagerAdapter, this will destroys fragments as soon as, they are not visible to user. An example is here: truiton.com/2013/05/android-fragmentstatepageradapter-example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.