美文网首页
如何在DialogFragment创建ViewPager+Fra

如何在DialogFragment创建ViewPager+Fra

作者: 山东好汉Tim | 来源:发表于2018-03-15 22:28 被阅读61次

    解决问题:fragment does not have a view
    要点:要在onViewCreated()中使用viewpager,不能在onCreateView()中使用。

    public class TestDialog extends DialogFragment {
       View view;
    
       ViewPager vp ;
       MyAdapter myAdapter;
    
       @Override
       public void onCreate(@Nullable Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setStyle(DialogFragment.STYLE_NO_FRAME,R.style.Mdialog);
       }
    
       @Override
       public void onStart() {
           super.onStart();
    
           Window win = getDialog().getWindow();
           win.setBackgroundDrawable( new ColorDrawable(Color.TRANSPARENT));
           DisplayMetrics dm = new DisplayMetrics();
           getActivity().getWindowManager().getDefaultDisplay().getMetrics( dm );
           WindowManager.LayoutParams params = win.getAttributes();
           params.gravity = Gravity.BOTTOM;
           params.width =  ViewGroup.LayoutParams.MATCH_PARENT;
           params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
           win.setAttributes(params);
       }
    
       @Nullable
       @Override
       public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
           view = inflater.inflate(R.layout.test2, container);
           return view;
       }
    
       @Override
       public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
           super.onViewCreated(view, savedInstanceState);
    
           myAdapter = new MyAdapter(getChildFragmentManager());
           vp = (ViewPager) view.findViewById(R.id.vp);
           vp.setAdapter(myAdapter);
       }
    }
    

    相关文章

      网友评论

          本文标题:如何在DialogFragment创建ViewPager+Fra

          本文链接:https://www.haomeiwen.com/subject/boukqftx.html