美文网首页
Android-BottomSheets

Android-BottomSheets

作者: 初见破晓 | 来源:发表于2016-02-25 14:44 被阅读802次

    先吐槽一下android studio 1.5 beta版编译的时候真的挺慢的,虽说热编译速度上来了,有时候还是会编译错误

    BottomSheets 在项目中用到了这个组件之前使用Dialog写的, 今天上班的时候说google在support library23.2中增加了这个组件,今天上午试了下,遇到了一些问题

    先记录一下BottomSheets的用法吧

    我们要在布局中添加一个属性
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    这个布局一定要是CoordinatorLayout的子布局

    app:behavior_peekHeight 这个属性的意思是提升的高度
    app:behavior_hideable 是否可以隐藏
    基本上就是这两个属性了,具体可以看这里http://android-developers.blogspot.com/2016/02/android-support-library-232.html

    <LinearLayout
            android:id="@+id/buttom_sheet"
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:background="@color/colorAccent"
            android:orientation="vertical"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
            app:behavior_peekHeight="50dp">
    <LinearLayout>
    
    
     // The View with the BottomSheetBehavior   
    View bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet);   
    BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
    behavior.setBottomSheetCallback(new BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
    // React to state change      
    }
    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
    // React to dragging events
    }   
    }); 
    
    

    代码添加的很简单
    运行之后的效果就是这样的了

    Paste_Image.png

    底下的button少了一部分,折腾了一上午才找到,我在父控件中添加了一句android:fitsSystemWindows="true",是为了改变statusBar的颜色,Material Design嘛!
    这个button就是少了statusBar的高度了,所以就改了下代码,思路是,当创建bottomsheets时向上移动statusbar的高度,当bottomsheets发生移动时,就让bottomsheets的高度还原,思路是这样了,贴代码,省的以后用到了忘记

    package org.cycling.tiny;
    
    import android.app.Activity;
    import android.graphics.Rect;
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.design.widget.BottomSheetBehavior;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.util.Log;
    import android.view.View;
    
    public class ScrollingActivity extends AppCompatActivity {
        private boolean first = true;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_scrolling);
    
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            View bottomSheet = findViewById(R.id.buttom_sheet);
            bottomSheet.setTranslationY(-getStatusHeight(this));
    
            BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
    
            behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                    @Override
                    public void onStateChanged(@NonNull
                    View bottomSheet, int newState) {
                    }
    
                    @Override
                    public void onSlide(@NonNull
                    View bottomSheet, float slideOffset) {
                        if (first) {
                            bottomSheet.setTranslationY(0);
                            first = false;
                        }
                    }
                });
        }
    
        /**
         * @param activity
         * @return > 0 success; <= 0 fail
         */
        public static int getStatusHeight(Activity activity) {
            int statusHeight = 0;
            Rect localRect = new Rect();
            activity.getWindow().getDecorView()
                    .getWindowVisibleDisplayFrame(localRect);
            statusHeight = localRect.top;
    
            if (0 == statusHeight) {
                Class<?> localClass;
    
                try {
                    localClass = Class.forName("com.android.internal.R$dimen");
    
                    Object localObject = localClass.newInstance();
                    int i5 = Integer.parseInt(localClass.getField(
                                "status_bar_height").get(localObject).toString());
                    statusHeight = activity.getResources().getDimensionPixelSize(i5);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                }
            }
    
            return statusHeight;
        }
    }
    
    

    以下是修改完之后的运行效果

    Paste_Image.png Paste_Image.png

    相关文章

      网友评论

          本文标题:Android-BottomSheets

          本文链接:https://www.haomeiwen.com/subject/nywikttx.html