-1

I have been trying to implement a ViewPager with different fragments.

And the problem is when i run the app, in the ViewPager, out of all the pages, only one page is visible and that page only gets changed when I slide over to the other pages in the ViewPager.

Take a look at my code,(although I checked it many times referring it with online resources).

This is what each of my fragments look like:

public class fragment1 extends Fragment {

/* Variable to store reference to the ACtivity */
Activity mCurrentActivity;

/* Variable storing reference to the ArrayList */
private ArrayList<Word> mDefaultWords;

/**
 * THe empty public Constructor
 */
public fragment1(){

 }

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

    /** Getting reference to the Activity */
    mCurrentActivity = getActivity();

// Populating the ArrayList here
// And later in the onActivityCreated callback I set an adapter on the ArrayList

    return inflater.inflate(R.layout.activity_others, container, false);


}

@Override
public void onActivityCreated(Bundle savedStateInstance){

    super .onActivityCreated(savedStateInstance);

    /**
     * Creating {@link ArrayAdapter} to link the {@link String}
     * from {@link ArrayList}                               {@param
     */
    MyAdapter adaptItems = new MyAdapter(mCurrentActivity, mDefaultWords);

    // Getting the id of the ListView in numberActivity.xml
    ListView myList = (ListView) mCurrentActivity.findViewById(R.id.theList);

    //Chaning background color
    myList.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.holo_purple));

    // Setting the adapter with the {@link ListView}
    myList.setAdapter(adaptItems);
        }
    }
}

My Activity setting the adapter class extending FragmentPagerAdapter as a private inner class and setting the adapter on the ViewPager.

public class Main2Activity extends AppCompatActivity {

private ViewPager mViewPager;
private FragmentPagerAdapter mFragmentStatePagerAdapter;
private FragmentManager mFragmentManager;

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

    mFragmentManager = getSupportFragmentManager();
    mViewPager = (ViewPager) findViewById(R.id.theViewPager);

    mFragmentStatePagerAdapter = new MyFragmentStatePagerAdapter(mFragmentManager);
    /* Setting the apdapter on the pager */
    mViewPager.setAdapter(mFragmentStatePagerAdapter);

}

public class MyFragmentStatePagerAdapter extends FragmentPagerAdapter {

    public MyFragmentStatePagerAdapter(FragmentManager fragmentManager){

        super(fragmentManager);
    }

    @Override
    public int getCount(){

        return 4;
    }

    @Override
    public Fragment getItem(int position) {


        if (position == 0) {
            return new fragment1();
        } else if (position == 1){
            return new fragment2();
        } else if (position == 2) {
            return new fragment3();
        } else {
            return new fragment4();
        }
    }
}
}

And here is the layout with the ViewPager

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/theViewPager"
    android:visibility="visible" />

As I said, when I run the app only one page gets displayed, other pages are present in the ViewPager but they are blank and displays the default background color, And the one page that is displayed is the one that gets changed when I swipe left or right in the ViewPager.

So what's the issue?

Arunava
  • 353
  • 1
  • 4
  • 13
  • check your Fragment imports(whtether its is v4.Fragment) in your Fragments which is not showing up in view pager. – Rajesh.k Mar 06 '17 at 04:56
  • I am not sure if thats all the code you used but I noticed `private ArrayList mDefaultWords;` is not initialized. – Enzokie Mar 06 '17 at 04:57
  • no i initialized that, but didn't showed that here.however, I did solved it, it was some weird problem, as when I used one XML layout per fragment.It worked fine. Gradle and sdktools version were very old. I think it happened because of that. – Arunava Mar 06 '17 at 04:59

1 Answers1

0

dont downvote the question, its a genuine problem. So, I worked my way around, let me say how. What happened is, I was working on a cloned project that had a old gradle version and sdktools version was also not updated and was quite old. and the min API targetted was API 15 And I was testing my application on API 21. So, what I did is I used a different layouts for each of my fragments. That is for each fragment I created its own XML layout. And that worked perfectly. Odd problem, so I updated the gradle and sdktools, to avoid such weird problems.

Arunava
  • 353
  • 1
  • 4
  • 13
  • Yeah Got that. but actually I solved the problem by using one layout per fragment. weird problem the app had. My other apps doesn't have this problem. I guess it was because of the sdktools and gradle version. as they were very old. – Arunava Mar 06 '17 at 05:04