gpt4 book ai didi

android - Bottomsheetbehavior 为 Bottom Sheet 的顶部增加了额外的空间

转载 作者:行者123 更新时间:2023-12-01 04:24:16 37 4
gpt4 key购买 nike

我生成 Android Studio's BottomSheetDialogFragment只需稍加修改即可正常工作,但我添加 app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"到 Bottom Sheet 进行回调它会在 Bottom Sheet 的顶部添加空白区域。我也试过
app:behavior_hideable="true"app:behavior_peekHeight="128dp" After Adding BottomSheetBehavior Before adding BottomSheetBehavior

和来自 here 的解决方案和 here但在我的情况下它不起作用这是我的布局和 fragment

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent">

<RelativeLayout
android:id="@+id/bottomSheet"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
>



<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/topBarBottomSheet"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Spinner
android:id="@+id/attachmentOption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.019"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<SearchView
android:id="@+id/searchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/closeButton"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />

<ImageButton
android:id="@+id/closeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@android:drawable/ic_menu_close_clear_cancel" />
</androidx.constraintlayout.widget.ConstraintLayout>

<EditText
android:id="@+id/searchViewEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/topBarBottomSheet"
android:text="TextView" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleviewGallery"
android:layout_width="match_parent"
android:layout_height="600dp"
android:layout_below="@id/searchViewEditText"
android:clipToPadding="false"
android:paddingTop="@dimen/list_item_spacing_half"
android:paddingBottom="@dimen/list_item_spacing_half"
tools:listitem="@layout/recycler_view_item_3"
tools:spanCount="3"
tools:layoutManager="GridLayoutManager" />
</RelativeLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

BottomSheetDialogFragment
 public class BottomSheet extends BottomSheetDialogFragment {
public static String TAG = "Bottom Sheet";


// TODO: Customize parameter argument names
private static final String ARG_ITEM_COUNT = "item_count";
private Listener mListener;

// TODO: Customize parameters
public static BottomSheet newInstance() {
/*final Bundle args = new Bundle();
args.putInt(ARG_ITEM_COUNT, itemCount);
fragment.setArguments(args);*/
return new BottomSheet();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
getRecentImages();
return inflater.inflate(R.layout.fragment_bottemsheet_list_dialog, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
final RecyclerView recyclerView = view.findViewById(R.id.recycleviewGallery);
View parentView = view.findViewById(R.id.bottomSheet);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(parentView);
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View view, int i) {
switch (i){
case BottomSheetBehavior.STATE_COLLAPSED:
Log.d(TAG,"Collapsed");
break;
case BottomSheetBehavior.STATE_DRAGGING:
Log.d(TAG,"Dragging");
break;
case BottomSheetBehavior.STATE_EXPANDED:
Log.d(TAG,"Expanded");
break;
case BottomSheetBehavior.STATE_HALF_EXPANDED:
Log.d(TAG,"Half Expanded");
break;
case BottomSheetBehavior.STATE_HIDDEN:
Log.d(TAG,"Hidden");
break;
case BottomSheetBehavior.STATE_SETTLING:
Log.d(TAG,"Settling");
break;

}
}

@Override
public void onSlide(@NonNull View view, float v) {

}
});

}

@Override
public void onAttach(Context context) {
super.onAttach(context);
final Fragment parent = getParentFragment();
Log.d(TAG,"Parent = "+parent+" Context "+context);
if (parent != null) {
mListener = (Listener) parent;
} else {
mListener = (Listener) context;
}
}

@Override
public void onDetach() {
mListener = null;
super.onDetach();
}

public interface Listener {
void onBottomSheetClicked(int position);
}

}

最佳答案

我在不添加 app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" 的情况下使我的 Bottom Sheet 完全工作我从 xml 文件中删除了行为,然后在 BottomSheetDialogFragment 中删除了行为。我覆盖 onCreateDialog像这样

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
//Get the BottomSheetBehavior
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
if (bottomSheet != null) {
bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheet.setMinimumHeight(350);
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View view, int i) {
switch (i){
case BottomSheetBehavior.STATE_COLLAPSED:
Log.d(TAG,"Collapsed");

break;
case BottomSheetBehavior.STATE_DRAGGING:
Log.d(TAG,"Dragging");

break;
case BottomSheetBehavior.STATE_EXPANDED:
Log.d(TAG,"Expanded");

break;
case BottomSheetBehavior.STATE_HALF_EXPANDED:
Log.d(TAG,"Half Expanded");
break;
case BottomSheetBehavior.STATE_HIDDEN:
Log.d(TAG,"Hidden");
dismiss();
break;
case BottomSheetBehavior.STATE_SETTLING:
Log.d(TAG,"Settling");
break;

}
}

@Override
public void onSlide(@NonNull View view, float v) {

}
});
}
}
});

return dialog;
}

如果您注意到 onShow 中的上述代码我正在创建 BottomSheetDialog 的实例的方法并借助实例给出 FrameLayout对象 android(x) 默认 Bottom Sheet
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = d.findViewById(com.google.android.material.R.id.design_bottom_sheet);

然后将框架布局对象提供给 BottomSheetBehavior
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);

然后最后将回调添加到 Bottom Sheet
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() 

关于android - Bottomsheetbehavior 为 Bottom Sheet 的顶部增加了额外的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59511026/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com