简体   繁体   中英

PagerAdapter issue with android Fragment

I have one issue with PagerAdapter in which when i load TextView in it it works perfectly code is below:

@Override
public Object instantiateItem(ViewGroup container, int position) {
    LayoutInflater inflater = (LayoutInflater) mActivity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.test, container, false);

    TextView tv = (TextView) view.findViewById(R.id.tvtest);
    tv.setText("" + position);

    container.addView(view);
    return view;
}

But when i use a Fragment instead of TextView like in below code PagerAdapter is only loading first fragment. Please help me to solve this problem.

@Override
public Object instantiateItem(ViewGroup container, int position) {

    LayoutInflater inflater = (LayoutInflater) mActivity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.test, container, false);

    MyFragment fragment = new MyFragment ();
    FragmentTransaction transaction = mActivity.getFragmentManager().beginTransaction();
    transaction.add(R.id.fragment_container, fragment).commit();

    container.addView(view);

    return view;
}

XML code:

When using TextView

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvtest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Test"
android:textSize="30sp" />

When using Fragment

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Thank you.

Just use FragmentPagerAdapter instead of PagerAdapter.

public static class MyPagerAdapter extends FragmentPagerAdapter {
    private static int NUM_ITEMS = 3;

    public MyPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    // Returns total number of pages
    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    // Returns the fragment to display for that page
    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0: // Fragment # 0 - This will show FirstFragment
            return FirstFragment.newInstance(0, "Page # 1");
        case 1: // Fragment # 0 - This will show FirstFragment different title
            return FirstFragment.newInstance(1, "Page # 2");
        case 2: // Fragment # 1 - This will show SecondFragment
            return SecondFragment.newInstance(2, "Page # 3");
        default:
            return null;
        }
    }

    // Returns the page title for the top indicator
    @Override
    public CharSequence getPageTitle(int position) {
        return "Page " + position;
    }

}

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