Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Matthew Lee
Matthew Lee
6,715 Points

Viewpager is blank when I replace the fragment inside it...


I am trying to replace a fragment which is held inside a ViewHolder. However, when I replace it, the new fragment shows a blank view despite having binded the new layout...

How can I properly display the layout for the new fragment??


1 Answer

Seth Kroger
Seth Kroger
56,412 Points

Are you using the ViewPager with a FragmentPagerAdapter to handle the fragments? It has methods for updating/removing/inserting pages and much like ListAdapters needs to be notified of changes.

Matthew Lee
Matthew Lee
6,715 Points

Hi Seth,

Yes, I have a FragmentPagerAdapter and I am using a ViewPager to hold my fragment. My initial fragment is a RecylerView of images that uses an adapter. And in my adapter, I have an OnClickListener for the view. This is where I am replacing my fragment.

I've attached my code for the Adapter (where I make the transaction) as well as my FragmentPagerAdapter class.

        view.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Toast.makeText(view.getContext(), "You clicked on the " + item.getItemTitle() + " at Position -> " + item.getPosition() + "!", Toast.LENGTH_SHORT).show();

                String name = item.getName();
                String id = item.getId();
                Log.e(TAG, "this is the name: "  + name + " and iD -> " + id);

                Bundle bundle = new Bundle();
                bundle.putString("AIRPORT_CODE", id);

                //create the new VendorFragment -> set Fragment Manager -> Transaction -> replace -> commit
                VendorFragment vendorFragment = new VendorFragment();

                FragmentManager fragmentManager = ((AppCompatActivity) view.getContext()).getFragmentManager();
                //Fragment currentFrag = fragmentManager.findFragmentById(c
                //FragmentManager childFM = currentFrag.getChildFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.container, vendorFragment);


public class SectionsPageAdapter extends FragmentPagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public void addFragment(Fragment fragment, String title) {

    public SectionsPageAdapter(FragmentManager fm) {

    public Fragment getItem(int position) {
        return mFragmentList.get(position);

    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);

    public int getCount() {
        return mFragmentList.size();