Search code examples
androidandroid-viewpager

ViewPager, dynamically adding fragments is not working


In my application I'm trying to open an excel file and create one tab per row of data. Opening the file and getting the data works fine, but if I try to add the pages I get the following exception:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: dsv.testanwendung, PID: 19699
    java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

I've already tried to only add the view in instantiateItem if currently has no parent, but then, somehow, the view gets removed instantly when I swipe to its page.

So this is my PagerAdapter class:

package dsv.testanwendung;

import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

public class PagerAdapterContent extends PagerAdapter {
    private ArrayList<View> views = new ArrayList<View>();

    @Override
    public int getItemPosition(@NonNull Object object) {
        int index = views.indexOf(object);
        if (index == -1) {
            return POSITION_NONE;
        } else {
            return index;
        }
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View v = views.get(position);
        container.addView(v);
        return v;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(views.get(position));
    }

    @Override
    public int getCount() {
        return views.size();
    }

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

    public int addView(View v) {
        return addView(v, views.size());
    }

    public int addView(View v, int position) {
        views.add(position, v);
        notifyDataSetChanged();
        return position;
    }

    public int removeView(ViewPager pager, View v) {
        return removeView(pager, views.indexOf(v));
    }

    public int removeView(ViewPager pager, int position) {
        pager.setAdapter(null);
        views.remove(position);
        pager.setAdapter(this);
        return position;
    }

    public View getView(int position) {
        return views.get(position);
    }
}

And here is how I try to add stuff through my main activity:

viewpageContent = findViewById(R.id.vpContent);
adapterContent = new PagerAdapterContent();
vpContent.setAdapter(adapterContent);
LayoutInflater inflater = getLayoutInflater();
FrameLayout fl = (FrameLayout)inflater.inflate(R.layout.fragment_content, null, false);
while (rowIterator.hasNext()) {
    HSSFRow row = (HSSFRow)rowIterator.next();
    ((InputUserDefined)f).addButton(row.getCell(0).toString());
    int pageIndex = adapterContent.addView(fl);
}

Solution

  • try this

    while (rowIterator.hasNext()) {
      LayoutInflater inflater = getLayoutInflater();
    FrameLayout fl = (FrameLayout)inflater.inflate(R.layout.fragment_content, null, false);
        HSSFRow row = (HSSFRow)rowIterator.next();
        ((InputUserDefined)f).addButton(row.getCell(0).toString());
        int pageIndex = adapterContent.addView(fl);
    }