sdk 23.2 Bottom Sheets

surpport libraray 23.2新增了以下功能:

  • 1. Support Vector Drawables and Animated Vector Drawables

  • 2. AppCompat DayNight theme

  • 3. Design Support Library: Bottom Sheets

  • 4. Support v4: MediaBrowserServiceCompat

  • 5. RecyclerView

  • 6. Custom Tabs

  • 7. Leanback for Android TV

在这里学习一下bottom sheets的使用,其中一项功能可以取代以前的开源 ActionSheetDialog

扩展了 CoordinatorLayout.Behavior 的自定义


bootom sheets的使用范围:nestedScrollView,RecyclerView,以及android自带的bootomSheetDialog,bottomSheetDialogFragment

可以呈现上拉显示出一个sheet  类似于淘宝详情 ,下拉关闭,可以呈现显示在底部的dialog,以及监听这些位置显示状态


个人感觉android抛弃drawerlayout完全有理由


使用记得添加远程依赖版本 

compile 'com.android.support:design:23.2.0'
 
 
例子:
main.xml
<?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">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nsv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:behavior_hideable="true"
        android:background="#6cf"
        app:layout_behavior="@string/bottom_sheet_behavior">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/i015" />
    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>


activity:
package com.example.bottomsheets;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v4.widget.NestedScrollView;
import android.util.Log;
import android.view.View;

public class MainActivity extends Activity {

    private static String TAG = "----->";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        NestedScrollView scrollingView = (NestedScrollView) findViewById(R.id.nsv);
        BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(scrollingView);
        bottomSheetBehavior.setPeekHeight(40); // 设置当关闭时 底部 的高度 app:behavior_peekHeight="50dp"
        //这里为蓝色的部分
        bottomSheetBehavior.setHideable(false);//设置当拉升到底部是否可以隐藏  app:behavior_hideable="true"
        //bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);//设置状态

        //回掉监听
        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                /**
                 *public static final int STATE_DRAGGING = 1;  //拖动
                 public static final int STATE_SETTLING = 2;//沉降中
                 public static final int STATE_EXPANDED = 3;//打开了
                 public static final int STATE_COLLAPSED = 4;//关闭了
                 public static final int STATE_HIDDEN = 5;//隐藏了
                 */
                Log.d(TAG, "---->state:" + newState);
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                Log.d(TAG, "slideOffset:" + slideOffset);
            }
        });
    }
}

效果图:
 
 
 
 
 
 
sdk 23.2 Bottom Sheets_第1张图片 
 
 
 

BottomSheets和RecyclerView 结和非常有意思:RecyclerView 尽然能wrap_content
例子:
sheets_recyclerview.xml
<?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">

    <CheckBox
        android:id="@+id/cb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#6cf"
        android:padding="10dp"
        android:text="开启" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        app:layout_behavior="@string/bottom_sheet_behavior"/>

</android.support.design.widget.CoordinatorLayout>

package com.example.bottomsheets;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

/**
 * @author xuanyouwu
 * @email [email protected]
 * @time 2016-03-08 11:26
 */
public class BottomSheetsRecyclerView extends Activity {

    RecyclerView recyclerView;
    CheckBox checkBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sheets_recyclerview);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new MyAdapter());

        final BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(recyclerView);
        bottomSheetBehavior.setHideable(false);
        bottomSheetBehavior.setPeekHeight(50);
        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            }
        });
        checkBox = ((CheckBox) findViewById(R.id.cb));
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                bottomSheetBehavior.setState(isChecked ? BottomSheetBehavior.STATE_EXPANDED : BottomSheetBehavior.STATE_COLLAPSED);
            }
        });


    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {

        class MyHolder extends RecyclerView.ViewHolder {

            TextView textView;

            public MyHolder(View itemView) {
                super(itemView);
                textView = (TextView) itemView.findViewById(R.id.textView);
            }
        }


        @Override
        public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_string, parent, false);
            return new MyHolder(v);
        }

        @Override
        public void onBindViewHolder(MyHolder holder, int position) {
            holder.textView.setText("item:" + position);
        }

        @Override
        public int getItemCount() {
            return 3;
        }
    }
}
效果图:
sdk 23.2 Bottom Sheets_第2张图片


一个灰常有意思的对话框BottomSheetDialog,可以取代一些又下向上弹出的对话框, 这个对话框默认只显示对话框实际大小的1/2 想要查看可以拖动出来
例子:
dialog_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="选择你最喜欢的水果" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

package com.example.bottomsheets;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * @author xuanyouwu
 * @email [email protected]
 * @time 2016-03-08 12:13
 */
public class BottomSheetDialogDemo extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);

        findViewById(R.id.button5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showBottomSheetDialog();
            }
        });
    }

    private void showBottomSheetDialog() {
        final BottomSheetDialog dialog = new BottomSheetDialog(this);
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_view, null);
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new MyAdapter());
        dialog.setContentView(view);
        dialog.show();
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {

        String[] titles = {"苹果", "梨子", "香蕉", "橘子", "香瓜", "西红柿", "xxx", "4777"};

        class MyHolder extends RecyclerView.ViewHolder {

            TextView textView;

            public MyHolder(View itemView) {
                super(itemView);
                textView = (TextView) itemView.findViewById(R.id.textView);
            }
        }


        @Override
        public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_string, parent, false);
            return new MyHolder(v);
        }

        @Override
        public void onBindViewHolder(MyHolder holder, int position) {
            holder.textView.setText(titles[position]);
        }

        @Override
        public int getItemCount() {
            return titles.length;
        }
    }
}

效果:
sdk 23.2 Bottom Sheets_第3张图片
 
 
怎么途中悬停,google貌似没有给出方案,请知道的告诉我!

你可能感兴趣的:(sdk 23.2 Bottom Sheets)