我正在尝试在bottomsheetFragment内部实现一个viewpager,但由于viewpager位于nestedScrollview内部,因此它似乎不可见。
下面是打开bottomsheetFragment的代码
BottomSheetDialogFragment bottomSheetDialogFragment = new CustomBottomSheetDialogFragment();
bottomSheetDialogFragment.show(((MyActivity)context).getSupportFragmentManager(), bottomSheetDialogFragment.getTag());
这是从recylerview项目单击触发的功能
下面是CustomBottomSheet Fragment类:
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);
}
}
}
下面是include_modal_content布局文件
<?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)
答案 0 :(得分:1)
使用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;
}
答案 1 :(得分:0)
我不知道上面的答案是如何得到预期的,因为它遗漏了非常重要的细节。在onCreateView方法中不能直接附加bottomSheetBehavior和bottomSheetCallback。
改为覆盖 onStart:
override fun onStart() {
super.onStart()
bottomSheet =
dialog!!.findViewById(com.google.android.material.R.id.design_bottom_sheet) as ViewGroup // notice the R root package
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
// SETUP YOUR BEHAVIOR HERE AND ADD THE BOTTOMSHEETCALLBACK HERE
然后如上所述覆盖 onCreateView
@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;
}