ViewPager with Fragments inside PopupWindow (or DialogFragment) - Error no view found for id for fragment

强颜欢笑 提交于 2019-11-28 05:49:17

I found a way to my problem.

Here we go

First I used DialogFragment instead PopupView.

So in my main activity I only created a Button who calls my DialogFragment.

public class Activity_principal1 extends FragmentActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal1);

        Button abrir = (Button) findViewById(R.id.botao);
        abrir.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                new DialogFragmentWindow().show(getSupportFragmentManager(), "");
            }
        });
    }

}

My adapter still the same like the question.

And here is where the magic occurs.

public class DialogFragmentWindow extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.popup, container);

        ViewPager vp_contentAcoesMusculares_SequenciaExercicios = (ViewPager) view.findViewById(R.id.vp_contentAcoesMusculares_SequenciaExercicios);
        List fragments = getFragments();
        AcoesMuscularesAdapter ama = new AcoesMuscularesAdapter(getChildFragmentManager(), fragments);
        vp_contentAcoesMusculares_SequenciaExercicios.setAdapter(ama);

        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        return view;
    }

    private List getFragments(){
        List fList = new ArrayList();
            fList.add(FragmentAcoesMusculares.newInstance("Fragment 1",1));
            fList.add(FragmentAcoesMusculares.newInstance("Fragment 2",2));
            fList.add(FragmentAcoesMusculares.newInstance("Fragment 3",3));
        return fList;
    }
}

The difference is the getChildFragmentManager(). This little piece of code saved my day.

The explanation to this is when I was using getSupportFragmentManager() and even indicating the viewpager was in another Layout XML he thought being in the main Layout XML.

Now my app get the child fragment so now he see the ViewPager.

That's it.

Thanks everyone.

I had the same issue, I try to create another viewpager in my dialog with another xml template, you know i didnt found information for fix this, but i use another view pager implementation, that works exactly as the viewpager in the supporta library, is called JazzyViewPager, and have multiple animations so cute! I recommend you use, this.

Here is the link: https://github.com/jfeinstein10/JazzyViewPager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!