简体   繁体   中英

ViewPager inside Fragment without using nested fragment

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM