底部 sheet activity 未充气
Bottom sheet not inflating in activity
我有这个底部 sheet,我想在 DialogsActivity.java 中点击按钮时充气,但每当我 运行 我的应用程序点击按钮时,什么都没有发生。
这是应该使底部膨胀的代码 sheet。
DialogsActivity.java
floatingButtonContainer = new FrameLayout(context);
floatingButtonContainer.setVisibility(onlySelect && initialDialogsType != 10 || folderId != 0 ? View.GONE : View.VISIBLE);
contentView.addView(floatingButtonContainer, LayoutHelper.createFrame((Build.VERSION.SDK_INT >= 21 ? 56 : 60) + 20, (Build.VERSION.SDK_INT >= 21 ? 56 : 60) + 20, (LocaleController.isRTL ? Gravity.LEFT : Gravity.RIGHT) | Gravity.BOTTOM, LocaleController.isRTL ? 4 : 0, 0, LocaleController.isRTL ? 0 : 4, 0));
floatingButtonContainer.setOnClickListener(v -> {
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
inflater.inflate(R.layout.fragment_bottom_sheet,null);
});
这是BottomSheetFrag.java
public class BottomSheetFrag extends BottomSheetDialogFragment {
public BottomSheetFrag(){
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_bottom_sheet, container, false);
}
}
您 BottomSheetFrag
代码看起来不错。
您不需要在 activity 中膨胀 BottomSheetFrag
,相反,您应该为此使用 Fragment Transaction
。
请更新您的 DialogActivity
代码如下。
BottomSheetFrag bottomSheet = new BottomSheetFrag();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.executePendingTransactions();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (bottomSheet.isAdded()) {
FragmentTransaction removeTransaction = fragmentManager.beginTransaction();
removeTransaction.remove(bottomSheet);
removeTransaction.commitNow();
}
fragmentTransaction.add(bottomSheet, "tag");
fragmentTransaction.commitNow();
我有这个底部 sheet,我想在 DialogsActivity.java 中点击按钮时充气,但每当我 运行 我的应用程序点击按钮时,什么都没有发生。
这是应该使底部膨胀的代码 sheet。
DialogsActivity.java
floatingButtonContainer = new FrameLayout(context);
floatingButtonContainer.setVisibility(onlySelect && initialDialogsType != 10 || folderId != 0 ? View.GONE : View.VISIBLE);
contentView.addView(floatingButtonContainer, LayoutHelper.createFrame((Build.VERSION.SDK_INT >= 21 ? 56 : 60) + 20, (Build.VERSION.SDK_INT >= 21 ? 56 : 60) + 20, (LocaleController.isRTL ? Gravity.LEFT : Gravity.RIGHT) | Gravity.BOTTOM, LocaleController.isRTL ? 4 : 0, 0, LocaleController.isRTL ? 0 : 4, 0));
floatingButtonContainer.setOnClickListener(v -> {
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
inflater.inflate(R.layout.fragment_bottom_sheet,null);
});
这是BottomSheetFrag.java
public class BottomSheetFrag extends BottomSheetDialogFragment {
public BottomSheetFrag(){
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_bottom_sheet, container, false);
}
}
您 BottomSheetFrag
代码看起来不错。
您不需要在 activity 中膨胀 BottomSheetFrag
,相反,您应该为此使用 Fragment Transaction
。
请更新您的 DialogActivity
代码如下。
BottomSheetFrag bottomSheet = new BottomSheetFrag();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.executePendingTransactions();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (bottomSheet.isAdded()) {
FragmentTransaction removeTransaction = fragmentManager.beginTransaction();
removeTransaction.remove(bottomSheet);
removeTransaction.commitNow();
}
fragmentTransaction.add(bottomSheet, "tag");
fragmentTransaction.commitNow();