如何使BottomSheetBehavior片段在内部单击时不会折叠,还需要执行其他操作,例如在单击时折叠键盘才能启用。我一直在为此问题上苦苦挣扎,所以这就是我在StackOverflow上创建此帐户的原因:)
编辑:这是FragmentTwo的FragmentOne容器内的FrameLayout
<FrameLayout
android:id="@+id/fragment_two_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:behavior_peekHeight="0dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior" />
现在,在FragmentOne.java内部:
private BottomSheetBehavior bottomSheetBehavior;
private FrameLayout fragmentTwoContainer;
private FragmentTransaction fragmentTransaction;
fragmentTwoContainer = fragmentOneView.findViewById(R.id.fragment_two_container);
bottomSheetBehavior = BottomSheetBehavior.from(fragmentTwoContainer);
private void startFragmentTwo(){
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
FragmentTwo fragmentTwo = new FragmentTwo();
fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_two_container, fragmentTwo, "");
fragmentTransaction.commit();
}
fragment_two_layout
<LinearLayout 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"
android:orientation="vertical"
android:id="@+id/fragment_two_linear_layout"
android:clickable="true"
android:focusable="true">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
FragmentTwo.java
private LinearLayout fragmentTwoLinearLayout;
fragmentTwoLinearLayout = fragmentTwoView.findViewById(R.id.fragment_two_linear_layout);
private void initListeners(){
fragmentTwoLinearLayout.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.fragment_two_linear_layout:
Log.i("CLICKED","CLICKED");
if (getActivity() != null) {
InputMethodManager inputManager =
(InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager != null) {
View v = getActivity().getCurrentFocus();
if (v != null) {
inputManager.hideSoftInputFromWindow(
v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
break;
}
}
因此,当我在fragmentTwoLinearLayout中单击时,不会打印“ CLICKED”,并且键盘也不会折叠,但是FragmentTwo折叠了。我在FragmentOne.java中尝试了此方法,但实际上并没有用:
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if(newState == BottomSheetBehavior.STATE_EXPANDED){
bottomSheet.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}
});
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
bottomSheet.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}
});
}
});