使用BottomSheetBehavior时遇到的坑(Support Library 25)

来源:互联网 发布:mac怎么qq远程控制 编辑:程序博客网 时间:2024/06/07 03:35

最近空闲的时候在研究Android Material Design,在看了严振杰大神的博客
Material之Behavior实现支付宝密码弹窗 仿淘宝/天猫商品属性选择
纸上得来终觉浅,于是决定自己试着用一用文中提到的BottomSheetBehavior。
具体用法这里不多说,感兴趣的朋友自己去看博客,大神写的很详细。
主要实现功能如下面两张图(点击按钮控制底部栏的出现和消失):
这里写图片描述这里写图片描述
我按着严振杰的博客来写发现点击按钮底部栏并无法显示和隐藏,而是一直显示,严振杰博客上写到24以上要在xml中加app:behavior_hideable=”true”的属性,sheet的隐藏和显示也要用STATE_EXPANDED 和 STATE_HIDDEN这两个状态,我用的是Support Library 25,发现居然没有app:behavior_hideable这个属性,看来25是不能用严振杰的解决方法了。debug时发现BottomSheetBehavior的初始状态是STATE_COLLAPSED,所以显示和隐藏应该还是用的STATE_EXPANDED和STATE_COLLAPSED。运行代码设置BottomSheetBehavior的状态为STATE_COLLAPSED之后,BottomSheetBehavior的状态就一直为STATE_SETTLING。我顺手上了Android Developer看看,发现有这么三个属性
这里写图片描述
红色箭头指向的那句英文被我的法眼抓住了,翻译了一下,大概意思是折叠之后的高度。我估摸着这大概就是隐藏时底部栏显示的高度吧。然后我在代码中加了那么一句

bottomSheetBehavior.setPeekHeight(0);

然后?然后就成功了啊哈哈哈哈哈
下面贴下我的代码:
activity

import android.support.design.widget.BottomSheetBehavior;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class Main2Activity extends AppCompatActivity {    private BottomSheetBehavior bottomSheetBehavior;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main2);        Button button = (Button) findViewById(R.id.control_button);        bottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.tab_bottom));        bottomSheetBehavior.setPeekHeight(0);        bottomSheetBehavior.setSkipCollapsed(false);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED){                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);                }else if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED){                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);                }            }        });    }}

布局文件

<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    xmlns:android.support.design="http://schemas.android.com/tools">    <android.support.design.widget.AppBarLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:theme="@style/AppTheme.AppBarOverlay">        <android.support.v7.widget.Toolbar            android:id="@+id/toolbar"            android:layout_width="match_parent"            android:layout_height="?attr/actionBarSize"            app:popupTheme="@style/AppTheme.PopupOverlay" />    </android.support.design.widget.AppBarLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_above="@+id/tab_bottom"        android:gravity="center"        android:orientation="vertical"        app:layout_behavior="@string/appbar_scrolling_view_behavior"        android.support.design:behavior_hideable="true">        <Button            android:id="@+id/control_button"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="sheet 显示/隐藏" />    </LinearLayout>    <LinearLayout        android:id="@+id/tab_bottom"        android:layout_width="match_parent"        android:layout_height="200dp"        android:layout_alignParentBottom="true"        android:background="@android:color/holo_purple"        app:layout_behavior="@string/bottom_sheet_behavior"        >        <Button            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:text="第一" />        <Button            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:text="第二" />        <Button            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:text="第三" />        <Button            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:text="第四" />    </LinearLayout></android.support.design.widget.CoordinatorLayout>

app的build.grade

apply plugin: 'com.android.application'android {    compileSdkVersion 25    buildToolsVersion "25.0.2"    defaultConfig {        applicationId "com.example.mopf.custombehavior"        minSdkVersion 15        targetSdkVersion 25        versionCode 1        versionName "1.0"        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }}dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {        exclude group: 'com.android.support', module: 'support-annotations'    })    compile 'com.android.support:appcompat-v7:25.3.1'    testCompile 'junit:junit:4.12'    compile 'com.android.support:design:25.3.1'    compile 'com.android.support:recyclerview-v7:25.3.1'    compile 'com.android.support:cardview-v7:25.3.1'}

接下来是BottomSheetDialog
先上代码

        Button dialog = (Button) findViewById(R.id.dialog_control);        sheetDialog = new BottomSheetDialog(this);        List<String> stringList = new ArrayList<>();        for (int i = 0; i < 30; i++) {            stringList.add(i + "");        }        View view = LayoutInflater.from(this).inflate(R.layout.layout_recycler, null);        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler);        ListRecyclerAdapter adapter = new ListRecyclerAdapter(stringList);        recyclerView.setAdapter(adapter);        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);        recyclerView.setLayoutManager(manager);        sheetDialog.setContentView(view);        sheetDialog.setCancelable(true);        sheetDialog.setCanceledOnTouchOutside(true);        dialog.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (sheetDialog.isShowing()) {                    sheetDialog.dismiss();                } else {                    sheetDialog.show();                }            }        });

与严振杰博客中不同的是没有对BottomSheetDialog中的Behavior的callback重新设置,但没有出现严振杰博客中的打开一次关闭后不能再打开,我看了一下源码没有发现跟23的库有什么不同,可能是鄙人水平问题。不过肯定是有所改动的。
鄙人水平略低,不足之处各位大神多多指教。

8 0