0

I manage to implement this answer. Using only single XML for viewpager but what i want is to implement this inside the fragment instead of Activity. I tried implementing it in Fragment but viewpager displayed nothing.

This is my PagerAdapter:

class WizardPagerAdapter extends PagerAdapter {

    public Object instantiateItem(ViewGroup collection, int position) {
        Log.i("instantiateItem", "instantiateItem");
        int resId = 0;
        switch (position) {
            case 0:
                resId = R.id.llPageOne;
                break;
            case 1:
                resId = R.id.llPageTwo;
                break;
        }
        return collection.findViewById(resId); // I tried replacing this with pager.findViewById(resId)
    }

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup parent, int position, Object object) {
        View view = (View) object;
        parent.removeView(view);
    }
} 

This is how i implemented it:

    WizardPagerAdapter adapter = new WizardPagerAdapter();
    pager = (ViewPager) view.findViewById(R.id.pager);
    pager.setAdapter(adapter);

And this is my XML layout:

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <LinearLayout
        android:id="@+id/llPageOne"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="PAGE ONE IN" />

        <EditText
            android:id="@+id/etSample"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:id="@+id/etSample2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/llPageTwo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="PAGE TWO IN" />

        <EditText
            android:id="@+id/etTwo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</android.support.v4.view.ViewPager>

So Basically what I want is to implement a single XML file for all the views of ViewPager but inside the Fragment. Its only been a few months since I learn to code for android so i'm having a hard to time to make it work. Thanks in advance.

Note: If possible not to use nested Fragment

Community
  • 1
  • 1
philip
  • 1
  • 2
  • Take a look at this, http://stackoverflow.com/questions/7723964/replace-fragment-inside-a-viewpager?rq=1 – Felipe Lopez Dec 22 '16 at 03:38
  • Thanks for the quick response. But is it possible not to use another fragment for the views/page? As you can see in the link that you've provided, the OP is using fragments as its views/page, though were similar in a way that we are both implementing viewpager inside the fragment. – philip Dec 22 '16 at 03:56

1 Answers1

0

i think it will useful for you: https://www.bignerdranch.com/blog/viewpager-without-fragments/

Noi Vu Duc
  • 15
  • 6
  • Thanks, I've already read that blog. I manage to implement single xml layout ViewPager but just like that blog using activity. – philip Dec 22 '16 at 05:04