suj*_*jay 8 android android-support-library bottom-sheet
我正在尝试在我的一个活动中实现Bottom表,我对它的行为方式感到困惑!
所以这就是问题,我有一个活动,我试图显示底部表格,我看到:
如果我们不设置app:behavior_peekHeight属性,那么Bottom表永远不会工作
如果你将PeekHeight设置为小于30dp的东西(基本上只是为了将其隐藏在屏幕上)
app:behavior_peekHeight在布局文件超过30DP并尝试设置的状态bottomSheetBehavior来STATE_HIDDEN与这个错误在你onCreate方法您的应用程序崩溃引起的:
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)
Run Code Online (Sandbox Code Playgroud)
我真的很困惑,为什么它不允许我隐藏在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">
Run Code Online (Sandbox Code Playgroud)
在我的活动中,我正在做这样的事情:
@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);
}
Run Code Online (Sandbox Code Playgroud)
在我的onclick中,我这样做:
@Override
public void onItemClick(View view, int position) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
Run Code Online (Sandbox Code Playgroud)
在解决这个问题几天后,我找到了一个替代解决方案:
如果我们创建一个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);
}
Run Code Online (Sandbox Code Playgroud)
然后在我的活动中
bottomSheetDialog = BottomSheetDialog.newInstance(addressList.get(position), position);
bottomSheetDialog.show(getSupportFragmentManager(), AddressActivity.class.getSimpleName());
Run Code Online (Sandbox Code Playgroud)
这实际上解决了我在活动开始时未隐藏底部工作表的问题,但我仍然无法理解为什么如果直接包含底部工作表我们会面临这个问题!
| 归档时间: |
|
| 查看次数: |
11316 次 |
| 最近记录: |