FragmentPagerAdapter与getChildFragmentManager()

时间:2018-05-19 14:37:54

标签: android android-viewpager

这是制作ViewPager的代码。错误是找不到视图,尽管视图在那里。

View attachmentBottomSheetView = LayoutInflater.from(container.getContext()).inflate(R.layout.attachment_view_pager_with_tabs, null);
            BottomSheetDialog attachmentBottomSheetDialog = new BottomSheetDialog(getActivity());
            attachmentBottomSheetDialog.setCanceledOnTouchOutside(true);
            attachmentBottomSheetDialog.setCancelable(true);
            DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
            ConstraintLayout.LayoutParams attachmentSheetParams = new ConstraintLayout.LayoutParams(
                    ConstraintLayout.LayoutParams.MATCH_PARENT,
                    displayMetrics.heightPixels);

        ViewPager viewPager = attachmentBottomSheetView.findViewById(R.id.viewPagerAttachment);
        final Bundle bundle = new Bundle();
        viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                AttachmentTabsFragment attachmentTabsFragment = new AttachmentTabsFragment();
                switch (position) {
                    case 0:
                        bundle.putString(KEY_ATTACHMENT_FRAGMENT_NAME, "Image");
                        break;
                    case 1:
                        bundle.putString(KEY_ATTACHMENT_FRAGMENT_NAME, "Video");
                        break;
                    case 2:
                        bundle.putString(KEY_ATTACHMENT_FRAGMENT_NAME, "GIF");
                        break;
                }
                attachmentTabsFragment.setArguments(bundle);
                return attachmentTabsFragment;
            }

            @Override
            public int getCount() {
                return 3;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                switch (position) {
                    case 0:
                        return "Image";
                    case 1:
                        return "Video";
                    case 2:
                        return "GIF";
                }
                return "";
            }
        });

        TabLayout tabLayout = attachmentBottomSheetView.findViewById(R.id.varun);
        tabLayout.setupWithViewPager(viewPager);

        window.setWindowAnimations(R.style.attachment_bottom_sheet_dialog_animation);
        attachmentBottomSheetDialog.setContentView(attachmentBottomSheetView, attachmentSheetParams);
        attachmentBottomSheetDialog.show();

错误是

  

java.lang.IllegalArgumentException:找不到ID为0x7f0901b9(com.example.circle:id/viewPagerAttachment)的片段,其中包含片段AttachmentTabsFragment {98eb499#7 id = 0x7f0901b9 android:switcher:2131296697:0}

当我尝试用getChildFragmentManager()替换getFragmentManger()时,我得到同样的错误。

1 个答案:

答案 0 :(得分:0)

ViewPager BottomSheetDialog存在于BottomSheetDialog,而不是Fragment的一部分。 DialogFragment而不是BottomSheetDialog

Soloution: - 使用BottomSheetDialogFragment代替Fragment。然后,您可以访问BottomSheet中<router-link :to="{name: 'League', params: {id: props.item.id}}">League</router_link> 的所有功能。