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

前端 未结 2 1142
故里飘歌
故里飘歌 2020-12-05 02:14

I created a FragmentActivity with this code below

public class Activity_principal1 extends FragmentActivity {

    @Override
    protected void onCreate(Bund         


        
2条回答
  •  情话喂你
    2020-12-05 03:11

    I found a way to my problem.

    Here we go

    First I used DialogFragment instead of 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 as 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 view pager was in another Layout XML he thought being in the main Layout XML.

    Now my app gets the child fragment so now he sees the ViewPager.

    That's it.

    Thanks, everyone.

提交回复
热议问题