尝试了解android支持库23.2.1中BottomSheet的行为 [英] Try to Understand the behavior of BottomSheet in android support library 23.2.1

查看:47
本文介绍了尝试了解android支持库23.2.1中BottomSheet的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的一项活动中实现底页",但我对它的行为方式感到困惑!

这是问题所在,我有一个活动正在尝试显示底表,并且看到了:

  1. 如果我们未设置app:behavior_peekHeight属性,则底部"表将永远无法工作

  2. 如果将PeekHeight设置为小于30dp(基本上只是将其隐藏在屏幕上)

  3. 如果在布局文件中将app:behavior_peekHeight设置为大于30dp,并尝试在onCreate方法中将bottomSheetBehavior的状态设置为STATE_HIDDEN,则您的应用程序将因该错误而崩溃

原因:

java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference             at    android.support.design.widget.BottomSheetBehavior.setState(BottomSheetBehavior.jav    a:440)
at myapp.activity.SomeActivity.onCreate(SomeActivity.java:75)

我真的很困惑为什么不允许我将其隐藏在onCreate中?或者为什么我们不能仅仅将peekHeight设置为0,以使它在屏幕上不可见,除非我们调用STATE_EXPANDED,或者甚至不设置该属性应将其默认为隐藏!或至少我应该能够将其设置为隐藏在我的onCreate中!

我错过了什么吗?还是BottomSheet的行为是僵化的?

我的BottomSheet布局文件是这样的:

<LinearLayout 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_width="match_parent"
android:background="@android:color/white"
android:layout_height="100dp"
android:orientation="vertical"
app:behavior_hideable="true"
app:behavior_peekHeight="40dp" <!-- I cant set this less than 30dp just to hide-->
app:layout_behavior="@string/bottom_sheet_behavior"
tools:context="someActivity"
android:id="@+id/addressbottomSheet"
tools:showIn="@layout/some_activity">

在我的活动中,我正在做这样的事情:

@InjectView(R.id.addressbottomSheet)
 View bottomSheetView;
@Override
protected void onCreate(Bundle savedInstanceState) {
....
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView);

// only if I have set peek_height to more than 30dp
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); 
}

在onclick中,我正在这样做:

@Override
public void onItemClick(View view, int position) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}

解决方案

在解决了这一问题几天后,我发现了另一种解决方案:

如果我们先创建一个Bottom_Sheet片段,然后在活动中实例化它,而不是直接在布局中使用Bottom_sheet,则不会发生此问题,并且将隐藏底部的表,并且我们无需指定peek_height

这是我所做的

public class BottomSheetDialog extends BottomSheetDialogFragment implements View.OnClickListener {
    @Override
    public View onCreateView(LayoutInflater inflater, 
                             ViewGroup container, 
                             Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_bottom_sheet, container, false);
}

然后参加我的活动

bottomSheetDialog = BottomSheetDialog.newInstance(addressList.get(position), position);
bottomSheetDialog.show(getSupportFragmentManager(), AddressActivity.class.getSimpleName());

这实际上解决了我的问题,即活动开始时底部表不被隐藏的问题,但是我仍然无法理解为什么如果直接包含bottom_sheet我们会面临该问题!

I am trying to implement Bottom sheet in one of my activities and I am kind of confused by the way it is behaving!

So here is the problem, I have an activity in which I am trying to show Bottom sheet and I see that:

  1. if we dont set the app:behavior_peekHeight property then the Bottom sheet never works

  2. If you set the PeekHeight to something less than 30dp (basically just to hide it from screen)

  3. If you set app:behavior_peekHeight to more than 30dp in layout file and try to set the state of bottomSheetBehavior to STATE_HIDDEN in you onCreate method your app crashes with this error

caused by:

java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference             at    android.support.design.widget.BottomSheetBehavior.setState(BottomSheetBehavior.jav    a:440)
at myapp.activity.SomeActivity.onCreate(SomeActivity.java:75)

I am really confused on why is it not allowing me to hide it in onCreate? or why cant we just set the peekHeight to 0 so that it is not visible on screen unless we call the STATE_EXPANDED or even not setting that property should default it to hide! or atleast I should be able to set it as hidden in my onCreate!

am I missing something? or is the behavior of the BottomSheet rigid?

my layout file for the BottomSheet is something like this:

<LinearLayout 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_width="match_parent"
android:background="@android:color/white"
android:layout_height="100dp"
android:orientation="vertical"
app:behavior_hideable="true"
app:behavior_peekHeight="40dp" <!-- I cant set this less than 30dp just to hide-->
app:layout_behavior="@string/bottom_sheet_behavior"
tools:context="someActivity"
android:id="@+id/addressbottomSheet"
tools:showIn="@layout/some_activity">

in my activity I am doing something like this:

@InjectView(R.id.addressbottomSheet)
 View bottomSheetView;
@Override
protected void onCreate(Bundle savedInstanceState) {
....
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView);

// only if I have set peek_height to more than 30dp
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); 
}

In my onclick I am doing this:

@Override
public void onItemClick(View view, int position) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}

解决方案

After working on this issue for few more days I found one alternate solution for this:

Instead of using the Bottom_sheet directly inside your layout, if we create a Bottom_Sheet fragment and then instantiate it in the activity this issue will not occur and the bottom sheet will be hidden and we dont need to specify the peek_height

here is what I did

public class BottomSheetDialog extends BottomSheetDialogFragment implements View.OnClickListener {
    @Override
    public View onCreateView(LayoutInflater inflater, 
                             ViewGroup container, 
                             Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_bottom_sheet, container, false);
}

Then in my activity

bottomSheetDialog = BottomSheetDialog.newInstance(addressList.get(position), position);
bottomSheetDialog.show(getSupportFragmentManager(), AddressActivity.class.getSimpleName());

This actually solved my problem of bottom sheet being not hidden when the activity starts but I am still not able to understand why if bottom_sheet is included directly we face that problem!

这篇关于尝试了解android支持库23.2.1中BottomSheet的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆