Skip to content

Commit

Permalink
1,BottomSheetBehavior
Browse files Browse the repository at this point in the history
2,FAB and Snackbar 示例
  • Loading branch information
zhouwei committed Dec 22, 2016
1 parent 35687dd commit 43cb516
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 4 deletions.
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -43,6 +43,14 @@
android:windowSoftInputMode="stateAlwaysHidden"
android:theme="@style/MDTheme"
/>
<activity android:name=".behavoir.FABSimpleActivity"
android:windowSoftInputMode="stateAlwaysHidden"
android:theme="@style/MDTheme"
/>
<activity android:name=".behavoir.BottomSheetBehaviorActivity"
android:windowSoftInputMode="stateAlwaysHidden"
android:theme="@style/MDTheme"
/>
</application>

</manifest>
Expand Up @@ -6,8 +6,10 @@
import android.view.View;

import com.zhouwei.md.materialdesignsamples.behavoir.BehaviorSimpleActivity;
import com.zhouwei.md.materialdesignsamples.behavoir.BottomSheetBehaviorActivity;
import com.zhouwei.md.materialdesignsamples.behavoir.CustomBehaviorActivity;
import com.zhouwei.md.materialdesignsamples.behavoir.CustomBehaviorActivity2;
import com.zhouwei.md.materialdesignsamples.behavoir.FABSimpleActivity;
import com.zhouwei.md.materialdesignsamples.bottomsheetdialog.BottomSheetDialogActivity;
import com.zhouwei.md.materialdesignsamples.toolbar.AppbarLayoutActivity;
import com.zhouwei.md.materialdesignsamples.toolbar.JanshuActivity;
Expand All @@ -27,6 +29,8 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.swipe_btn).setOnClickListener(this);
findViewById(R.id.custom_behavior).setOnClickListener(this);
findViewById(R.id.custom_behavior2).setOnClickListener(this);
findViewById(R.id.fab_snack_btn).setOnClickListener(this);
findViewById(R.id.bottom_sheet_demo).setOnClickListener(this);
}

@Override
Expand Down Expand Up @@ -61,6 +65,14 @@ public void onClick(View v) {
intent = new Intent(this, CustomBehaviorActivity2.class);
startActivity(intent);
break;
case R.id.fab_snack_btn:
intent = new Intent(this, FABSimpleActivity.class);
startActivity(intent);
break;
case R.id.bottom_sheet_demo:
intent = new Intent(this, BottomSheetBehaviorActivity.class);
startActivity(intent);
break;
}
}
}
Expand Up @@ -38,9 +38,9 @@ private void initView() {
*/
swipe.setSwipeDirection(SwipeDismissBehavior.SWIPE_DIRECTION_START_TO_END);

swipe.setStartAlphaSwipeDistance(0.7f);
swipe.setStartAlphaSwipeDistance(0f);

swipe.setSensitivity(0.5f);
swipe.setSensitivity(0.2f);

swipe.setListener(new SwipeDismissBehavior.OnDismissListener() {
@Override
Expand Down
@@ -0,0 +1,57 @@
package com.zhouwei.md.materialdesignsamples.behavoir;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.zhouwei.md.materialdesignsamples.R;

/**
* Created by zhouwei on 16/12/20.
*/

public class BottomSheetBehaviorActivity extends AppCompatActivity {

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

View shareView = findViewById(R.id.share_view);
//获取BottomSheetBehavior
final BottomSheetBehavior sheetBehavior = BottomSheetBehavior.from(shareView);

//设置折叠时的高度
//sheetBehavior.setPeekHeight(BottomSheetBehavior.PEEK_HEIGHT_AUTO);

//监听BottomSheetBehavior 状态的变化
sheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {

}

@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {

}
});

//下滑的时候是否可以隐藏
sheetBehavior.setHideable(true);
findViewById(R.id.btn_show_bottom_sheet).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(sheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED){
sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}else {
sheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}

}
});
}
}
Expand Up @@ -74,7 +74,7 @@ public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View
}

/**
*
* 获取Header 高度
* @return
*/
public int getHeaderHeight(){
Expand Down
@@ -0,0 +1,67 @@
package com.zhouwei.md.materialdesignsamples.behavoir;

import android.content.res.ColorStateList;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.zhouwei.md.materialdesignsamples.R;

/**
* Created by zhouwei on 16/12/20.
*/

public class FABSimpleActivity extends AppCompatActivity {
private static final String TAG = "FABSimpleActivity";
private FloatingActionButton fab1,fab2;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fab_simple_layout);

fab1 = (FloatingActionButton) findViewById(R.id.fab1);
fab2 = (FloatingActionButton) findViewById(R.id.fab2);

fab1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(fab1,"点击fab1",Snackbar.LENGTH_LONG).show();
}
});

fab1.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.colorPrimary)));


fab2.setImageResource(R.drawable.ic_book_list);

fab2.setCompatElevation(6);

fab2.setSize(FloatingActionButton.SIZE_NORMAL);
fab2.hide(new FloatingActionButton.OnVisibilityChangedListener() {
@Override
public void onHidden(FloatingActionButton fab) {
Log.i(TAG,"fab hidden...");
}
});
fab2.show(new FloatingActionButton.OnVisibilityChangedListener() {
@Override
public void onShown(FloatingActionButton fab) {
Log.i(TAG,"fab show...");
}
});

fab2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(fab2,"哈哈,我是Snackbar2",Snackbar.LENGTH_SHORT).show();
}
});



}
}
Expand Up @@ -78,6 +78,7 @@ private void showShareDialog(){
mBottomSheetDialog.setContentView(view);
mBottomSheetDialog.setCancelable(true);
mBottomSheetDialog.setCanceledOnTouchOutside(true);
// 解决下滑隐藏dialog 后,再次调用show 方法显示时,不能弹出Dialog
View view1 = mBottomSheetDialog.getDelegate().findViewById(android.support.design.R.id.design_bottom_sheet);
final BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(view1);
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Expand Up @@ -46,6 +46,20 @@
android:text="SwipeDismissBehavior示例"
android:gravity="center"
/>
<TextView
android:id="@+id/bottom_sheet_demo"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="BottomSheetBehavior示例"
android:gravity="center"
/>
<TextView
android:id="@+id/fab_snack_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="FAB、Snackbar 示例"
android:gravity="center"
/>
<TextView
android:id="@+id/custom_behavior"
android:layout_width="match_parent"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/behavoir_simple_activity.xml
Expand Up @@ -9,7 +9,7 @@
android:id="@+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/colorPrimary"
android:background="@android:color/holo_red_light"
>
<TextView
android:layout_width="wrap_content"
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/res/layout/bottom_sheet_behavior_ac.xml
@@ -0,0 +1,28 @@
<?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">
<TextView
android:id="@+id/btn_show_bottom_sheet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示/隐藏 BottomSheet"
android:background="@android:color/darker_gray"
android:textColor="@color/black"
android:padding="10dp"
/>
<FrameLayout
android:id="@+id/share_view"
app:layout_behavior="@string/bottom_sheet_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical"
app:behavior_peekHeight="0dp"
>
<include layout="@layout/bottom_sheet_share_dialog"/>
</FrameLayout>

</android.support.design.widget.CoordinatorLayout>
65 changes: 65 additions & 0 deletions app/src/main/res/layout/fab_simple_layout.xml
@@ -0,0 +1,65 @@
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="200dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@drawable/meizhi"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
/>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="18dp"
android:text="@string/large_text"/>

</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_dialog_email"
android:layout_marginBottom="20dp"
android:layout_marginRight="15dp"
android:layout_gravity="bottom|right"
app:rippleColor="@android:color/darker_gray"
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_dialog_email"
android:layout_marginBottom="20dp"
android:layout_marginRight="15dp"
app:layout_anchor="@+id/appbar_layout"
app:layout_anchorGravity="bottom|right"
/>
</android.support.design.widget.CoordinatorLayout>

0 comments on commit 43cb516

Please sign in to comment.