代码之家  ›  专栏  ›  技术社区  ›  Feroz Siddiqui

Android BottomSheetFragment ViewPager Fragment没有视图

  •  0
  • Feroz Siddiqui  · 技术社区  · 6 年前

    我正试图在BottomSheetFragment中实现一个viewPager,但由于viewPager位于NestedScrollView中,所以它似乎不可见。

    下面是用于打开BottomSheetFragment的代码

            BottomSheetDialogFragment bottomSheetDialogFragment = new CustomBottomSheetDialogFragment();
    
            bottomSheetDialogFragment.show(((MyActivity)context).getSupportFragmentManager(), bottomSheetDialogFragment.getTag());
    

    这是从recylerview item click触发的函数

    下面是CustomBottomSheet片段类:

    import android.app.Dialog;
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.design.widget.BottomSheetBehavior;
    import android.support.design.widget.BottomSheetDialogFragment;
    import android.support.design.widget.CoordinatorLayout;
    import android.support.v4.view.ViewPager;
    import android.support.v7.widget.LinearLayoutManager;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Spinner;
    import android.widget.TextView;
    
    import sales.talentify.ai.aitalentifysales.R;
    import sales.talentify.ai.aitalentifysales.landing.adapter.AddLeadAdapter;
    import sales.talentify.ai.aitalentifysales.salespojo.Lead;
    
    public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {
        public static final String DATA="data";
        Lead lead;
        private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
    
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                    dismiss();
                }
    
            }
    
            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            }
        };
    
    
        private LinearLayoutManager mLinearLayoutManager;
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            lead =(Lead) getArguments().getSerializable(DATA);
    
            return super.onCreateDialog(savedInstanceState);
    
    
        }
    
        @Override
        public void onViewCreated(View contentView, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(contentView, savedInstanceState);
    
    
        }
    
        @Override
        public void setupDialog(Dialog dialog, int style) {
            super.setupDialog(dialog, style);
            View view = View.inflate(getContext(), R.layout.include_modal_content, null);
    
            EditText company_name = view.findViewById(R.id.company_name);
            Spinner lead_source = view.findViewById(R.id.lead_source);
            EditText address = view.findViewById(R.id.address);
            EditText state = view.findViewById(R.id.state);
            EditText country = view.findViewById(R.id.country);
            EditText city = view.findViewById(R.id.city);
            EditText pincode = view.findViewById(R.id.pincode);
            Button lead_submit = view.findViewById(R.id.lead_submit);
            TextView company_title = view.findViewById(R.id.company_title);
            final Button company_button = view.findViewById(R.id.company_button);
            company_title.setText(lead.getCompanyName());
    
    
    
    
    
    
            dialog.setContentView(view);
    
            ViewPager viewPager = view.findViewById(R.id.sales_viewpager);
    
            AddLeadAdapter addLeadAdapter = new AddLeadAdapter(getChildFragmentManager(),lead);
            viewPager.setAdapter(addLeadAdapter);
            CoordinatorLayout.LayoutParams layoutParams =
                    (CoordinatorLayout.LayoutParams) ((View) view.getParent()).getLayoutParams();
            CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();
            if (behavior != null && behavior instanceof BottomSheetBehavior) {
                ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
            }
    
        }
    
    
    
    }
    

    以下是包含“模式”内容布局文件

     <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:orientation="vertical"
            android:id="@+id/main_layout"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:id="@+id/toolbar"
                android:foreground="?attr/selectableItemBackground"
                android:orientation="horizontal"
                android:paddingBottom="8dp"
                android:paddingLeft="18dp"
                android:paddingRight="18dp"
                android:paddingTop="8dp"
                android:background="@drawable/input_shape"
                android:focusable="true">
    
                <Button
                    android:id="@+id/company_button"
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginEnd="8dp"
                    android:background="@drawable/circle_button_solid"
                    android:text="#"
    
                    android:textColor="@color/white"
                    />
    
                <TextView
                    android:id="@+id/company_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:maxLines="1"
                    android:ellipsize="end"
                    android:textSize="@dimen/h6"
                    app:fontFamily="@font/lato"
                    android:text="Email a Copy"
                    android:textColor="@color/black_theme_text"
                />
    
            </LinearLayout>
    
           <include layout="@layout/myinclude"/>
            <Button
    
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/lead_submit"
                android:background="@color/theme_color"
                android:text="Save Lead"
                android:textColor="@color/white"
                android:fontFamily="@font/lato"
                android:textSize="@dimen/p1"
    
                android:layout_alignParentBottom="true"/>
        </RelativeLayout>
    
    
    
     myinclude is included layout which contains nestedscrollview and viewpager as :
    
    
     <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:orientation="vertical"
            android:id="@+id/main_layout"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:id="@+id/toolbar"
                android:foreground="?attr/selectableItemBackground"
                android:orientation="horizontal"
                android:paddingBottom="8dp"
                android:paddingLeft="18dp"
                android:paddingRight="18dp"
                android:paddingTop="8dp"
                android:background="@drawable/input_shape"
                android:focusable="true">
    
                <Button
                    android:id="@+id/company_button"
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginEnd="8dp"
                    android:background="@drawable/circle_button_solid"
                    android:text="#"
    
                    android:textColor="@color/white"
                    />
    
                <TextView
                    android:id="@+id/company_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:maxLines="1"
                    android:ellipsize="end"
                    android:textSize="@dimen/h6"
                    app:fontFamily="@font/lato"
                    android:text="Email a Copy"
                    android:textColor="@color/black_theme_text"
                />
    
            </LinearLayout>
    
           <include layout="@layout/edit_lead"/>
            <Button
    
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/lead_submit"
                android:background="@color/theme_color"
                android:text="Save Lead"
                android:textColor="@color/white"
                android:fontFamily="@font/lato"
                android:textSize="@dimen/p1"
    
                android:layout_alignParentBottom="true"/>
        </RelativeLayout>
    

    请帮助我如何解决此NestedScrollView中的ViewPager。

    我得到以下例外:

      java.lang.IllegalStateException: Fragment does not have a view
            at android.support.v4.app.Fragment$2.onFindViewById(Fragment.java:2391)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1444)
            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.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:168)
            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:23169)
            at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
            at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
            at android.view.View.measure(View.java:23169)
            at android.support.v4.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1502)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Pankaj Kumar    6 年前

    使用 onCreateView(..) 而不是 setupDialog(...) 为您的 BottomSheetDialogFragment . 像下面

    @Override
    public View onCreateView(LayoutInflater inflater,
                                 @Nullable ViewGroup container,
                                 @Nullable Bundle savedInstanceState) {
    
            View view = inflater.inflate(R.layout.include_modal_content, container,
                    false);
    
            // get the views and attach the listener
    
            return view;
        }