не может заменить фрагмент в ViewPager в PopupWindow - PullRequest
1 голос
/ 18 апреля 2019

Я пытаюсь добавить viewpager в popupwindow, но я не могу этого сделать, я всегда получаю сообщение об ошибке java.lang.IllegalArgumentException: не найдено представление для идентификатора 0x7f0902d9 (: id / viewpager) для фрагмента FirstFragment { 875be04 # 0 id = 0x7f0902d9 android: переключатель: 2131296985: 0}

Я испробовал все возможности, но безрезультатно.

Вот мой код,

public void showEditingPopupWindow() {

        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        if (inflater != null) {
            View view = inflater.inflate(R.layout.editor_layout, new LinearLayout(this));

            popupWindow = new PopupWindow();
            popupWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            popupWindow.setOutsideTouchable(false);
            popupWindow.setContentView(view);

            popupWindow.setFocusable(true);
            popupWindow.getContentView().requestFocus();

            LinearLayout llSelectedColor, llBackgroundSelectedTextColor;

            ViewPager viewPager = view.findViewById(R.id.viewpager);
            setupViewPager(viewPager);
            TabLayout tabLayout = view.findViewById(R.id.tabs);
            tabLayout.setupWithViewPager(viewPager);
            tabLayout.setTabMode(TabLayout.MODE_FIXED);
            tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

            View headerView = inflater.inflate(R.layout.custom_tab, new LinearLayout(this));
            LinearLayout llTextStyle = headerView.findViewById(R.id.llTextStyle);
            LinearLayout llBackground = headerView.findViewById(R.id.llBackground);
            LinearLayout llTextColor = headerView.findViewById(R.id.llTextColor);
            llSelectedColor = headerView.findViewById(R.id.llSelectedColor);
            llBackgroundSelectedTextColor = headerView.findViewById(R.id.llBackgroundSelectedTextColor);

            tabLayout.getTabAt(0).setCustomView(llTextStyle);
            tabLayout.getTabAt(1).setCustomView(llBackground);
            tabLayout.getTabAt(2).setCustomView(llTextColor);

            llSelectedColor.setBackgroundColor(Color.parseColor(selectedBackgroundColor));
            llBackgroundSelectedTextColor.setBackgroundColor(Color.parseColor(selectedTextColor));

            DocumentColorChangeListener documentColorChangeListener = (color, isText) -> {
                if (isText) {
                    if (!selectedTextColor.equals(color)) {
                        selectedTextColor = color;
                        llBackgroundSelectedTextColor.post(() -> llBackgroundSelectedTextColor.setBackgroundColor(Color.parseColor(color)));
                    }
                } else if (!selectedBackgroundColor.equals(color)) {
                    selectedBackgroundColor = color;
                    llSelectedColor.setBackgroundColor(Color.parseColor(color));
                }
            };
            setOnColorUpdateListener(documentColorChangeListener);

            llTextEditor.setSelected(true);

            popupWindow.setOnDismissListener(() -> {
                llTextEditor.setSelected(false);
            });

            view.measure(Utils.makeViewMeasureSpec(popupWindow.getWidth()), Utils.makeViewMeasureSpec(popupWindow.getHeight()));
            llTextEditor.measure(0, 0);
            popupWindow.showAsDropDown(llTextEditor, -(popupWindow.getContentView().getMeasuredWidth() / 2) + llLanguageSelect.getMeasuredWidth() / 2, 0);
        }
    }

настройка просмотра пейджера с фрагментом

    private void setupViewPager(ViewPager viewPager) {

        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

        Bundle bundle = new Bundle();
        bundle.putString(AppConstants.OtherConstants.SELECTED_TEXT_STYLE, selectedTextStyle);
        TextStyleFragment textStyleFragment = new TextStyleFragment();
        textStyleFragment.setArguments(bundle);

        bundle = new Bundle();
        BackgroundFragment backgroundFragment = new BackgroundFragment();
        bundle.putString(AppConstants.OtherConstants.SELECTED_BACKGROUND_COLOR, selectedBackgroundColor);
        backgroundFragment.setArguments(bundle);

        bundle = new Bundle();
        TextColorPickerFragment textColorPickerFragment = new TextColorPickerFragment();
        bundle.putString(AppConstants.OtherConstants.SELECTED_TEXT_COLOR, selectedTextColor);
        textColorPickerFragment.setArguments(bundle);


        viewPagerAdapter.addFragment(textStyleFragment, AppConstants.OtherConstants.TEXT_STYLE);
        viewPagerAdapter.addFragment(backgroundFragment, AppConstants.OtherConstants.BACKGROUND);
        viewPagerAdapter.addFragment(textColorPickerFragment, AppConstants.OtherConstants.COLOR_PICKER);
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.setOffscreenPageLimit(3);
    }

и это мой адаптер ViewPager

    public class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        ViewPagerAdapter(android.support.v4.app.FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

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

        void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

вот журнал ошибок

java.lang.IllegalArgumentException: No view found for id 0x7f0902d9 (com.appneme.myappp:id/viewpager) for fragment FirstFragment{875be04 #0 id=0x7f0902d9 android:switcher:2131296985:0}
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1454)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2243)
        at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:654)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:146)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1244)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1092)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1622)
        at android.view.View.measure(View.java:18830)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
        at android.view.View.measure(View.java:18830)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
        at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1112)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:632)
        at android.view.View.measure(View.java:18830)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:18830)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2141)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1228)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1144)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6102)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:860)
        at android.view.Choreographer.doCallbacks(Choreographer.java:672)
        at android.view.Choreographer.doFrame(Choreographer.java:608)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:846)
        at android.os.Handler.handleCallback(Handler.java:742)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5601)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)

, пожалуйста, помогите с этой проблемой

1 Ответ

0 голосов
/ 19 апреля 2019

Я решил, мой вопрос, используя DialogFragment, в котором я заменил Fragment, который имеет Viewpager & tabs. см. ниже код

public class DocumentEditingDialog extends BaseDialogFragment {

    public DocumentEditingDialog() {
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        setRetainInstance(false);
        ButterKnife.bind(Objects.requireNonNull(getActivity()));
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

        Window window = getDialog().getWindow();
        if (window != null) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); // This flag is required to set otherwise the setDimAmount method will not show any effect
            window.setDimAmount(0); //0 for no dim to 1 for full dim

            WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
            wmlp.gravity = Gravity.TOP;

            Bundle arguments = getArguments();
            if (arguments != null) {
                if (Utils.isTabletDevice(getActivity())) {
                    if (getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                        wmlp.x = arguments.getInt("x") - dpToPx(getResources().getDimension(R.dimen._105sdp));
                        wmlp.y = arguments.getInt("y") - dpToPx(getResources().getDimension(R.dimen._115sdp));
                    } else {
                        wmlp.x = arguments.getInt("x") - dpToPx(getResources().getDimension(R.dimen._175sdp));
                        wmlp.y = arguments.getInt("y") - dpToPx(getResources().getDimension(R.dimen._50sdp));
                    }
                } else {
                    wmlp.x = arguments.getInt("x") - dpToPx(getResources().getDimension(R.dimen._75sdp));
                    wmlp.y = arguments.getInt("y") - dpToPx(getResources().getDimension(R.dimen._72sdp));
                }
            }
            window.setAttributes(wmlp);
        }

        View view = inflater.inflate(R.layout.popup_windows_doc_editing, container, false);

        android.support.v4.app.FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        Bundle bundle = new Bundle();
        TextEditorFragment myf = new TextEditorFragment();
        myf.setArguments(bundle);
        transaction.replace(R.id.frm_language_list, myf);
        transaction.commit();
        return view;
    }

    public int dpToPx(float valueInDp) {
        DisplayMetrics metrics = Objects.requireNonNull(getActivity()).getResources().getDisplayMetrics();
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
    }
}
...