Skip to content
This repository has been archived by the owner on Oct 23, 2020. It is now read-only.

Commit

Permalink
Add support for CoordinatorLayout
Browse files Browse the repository at this point in the history
+ sample

ie add default behavior for both floating action button and menu
  • Loading branch information
Bao-Long Nguyen-Trong committed Nov 20, 2015
1 parent b0a15b8 commit 76b8924
Show file tree
Hide file tree
Showing 17 changed files with 326 additions and 46 deletions.
1 change: 1 addition & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:design:23.0.1'
}

apply from: '../gradle-mvn-push.gradle'
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.os.Parcel;
import android.os.Parcelable;
import android.os.SystemClock;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
Expand All @@ -38,6 +39,7 @@
import android.widget.ImageButton;
import android.widget.TextView;

@CoordinatorLayout.DefaultBehavior(FloatingActionButtonBehavior.class)
public class FloatingActionButton extends ImageButton {

public static final int SIZE_NORMAL = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.github.clans.fab;

import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.util.AttributeSet;
import android.view.View;


public class FloatingActionButtonBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> {

private int mToolbarHeight = -1;

public FloatingActionButtonBehavior() {
super();
}

public FloatingActionButtonBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
return dependency instanceof Snackbar.SnackbarLayout
|| dependency instanceof AppBarLayout;
}

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
super.onDependentViewChanged(parent, fab, dependency);

if (mToolbarHeight == -1) {
mToolbarHeight = Util.getToolbarHeight(fab.getContext());
}

float translationY;
if (dependency instanceof Snackbar.SnackbarLayout) {
translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
} else {
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab
.getLayoutParams();
int famBottomMargin = lp.bottomMargin;
int height = fab.getHeight();
int distanceToScroll = height + famBottomMargin;
float ratio = (float) dependency.getY() / (float) mToolbarHeight;
translationY = - distanceToScroll * ratio;
}
fab.setTranslationY(translationY);

return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.design.widget.CoordinatorLayout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
Expand All @@ -27,6 +28,7 @@
import java.util.ArrayList;
import java.util.List;

@CoordinatorLayout.DefaultBehavior(FloatingActionMenuBehavior.class)
public class FloatingActionMenu extends ViewGroup {

private static final int ANIMATION_DURATION = 300;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.github.clans.fab;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.util.AttributeSet;
import android.view.View;


public class FloatingActionMenuBehavior extends CoordinatorLayout.Behavior<FloatingActionMenu> {

private int mToolbarHeight = -1;

public FloatingActionMenuBehavior() {
super();
}

public FloatingActionMenuBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionMenu child, View dependency) {
return dependency instanceof Snackbar.SnackbarLayout
|| dependency instanceof AppBarLayout;
}

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionMenu fam, View dependency) {
super.onDependentViewChanged(parent, fam, dependency);

if (mToolbarHeight == -1) {
mToolbarHeight = Util.getToolbarHeight(fam.getContext());
}

float translationY;
if (dependency instanceof Snackbar.SnackbarLayout) {
translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
} else {
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fam
.getLayoutParams();
int famBottomMargin = lp.bottomMargin;
int height;
if (!fam.isOpened()) {
height = fam.getChildAt(0).getHeight();
} else {
height = fam.getHeight();
}
int distanceToScroll = height + famBottomMargin;
float ratio = (float) dependency.getY() / (float) mToolbarHeight;
translationY = - distanceToScroll * ratio;
}
fam.setTranslationY(translationY);

return true;
}

}
10 changes: 10 additions & 0 deletions library/src/main/java/com/github/clans/fab/Util.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.clans.fab;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;

final class Util {
Expand All @@ -20,4 +21,13 @@ static boolean hasJellyBean() {
static boolean hasLollipop() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
}

public static int getToolbarHeight(Context context) {
final TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(
new int[]{R.attr.actionBarSize});
int toolbarHeight = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();

return toolbarHeight;
}
}
1 change: 1 addition & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
compile project(':library')
}
12 changes: 8 additions & 4 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
</intent-filter>
</activity>
<activity
android:label="@string/floating_action_menu"
android:name="com.github.clans.fab.sample.FloatingMenusActivity" />
android:name="com.github.clans.fab.sample.FloatingMenusActivity"
android:label="@string/floating_action_menu" />
<activity
android:label="@string/recyclerview_fab_example"
android:name="com.github.clans.fab.sample.RecyclerViewActivity" />
android:name="com.github.clans.fab.sample.RecyclerViewActivity"
android:label="@string/recyclerview_fab_example" />
<activity android:name="com.github.clans.fab.sample.CoordinatorLayoutActivity"
android:label="@string/coordinatorlayout_example"
android:theme="@style/AppTheme.NoActionBar" >
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.github.clans.fab.sample;

import com.github.fab.sample.R;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import java.util.Locale;

public class CoordinatorLayoutActivity extends AppCompatActivity implements View.OnClickListener {

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

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

findViewById(R.id.fab).setOnClickListener(this);
findViewById(R.id.fab1).setOnClickListener(this);
findViewById(R.id.fab2).setOnClickListener(this);
findViewById(R.id.fab3).setOnClickListener(this);

Locale[] availableLocales = Locale.getAvailableLocales();

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new LanguageAdapter(availableLocales));
}

@Override
public void onClick(View v) {
Snackbar.make(v, R.string.lorem_ipsum, Snackbar.LENGTH_SHORT).show();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ private void createCustomAnimation() {
ObjectAnimator scaleOutX = ObjectAnimator.ofFloat(menu3.getMenuIconView(), "scaleX", 1.0f, 0.2f);
ObjectAnimator scaleOutY = ObjectAnimator.ofFloat(menu3.getMenuIconView(), "scaleY", 1.0f, 0.2f);

ObjectAnimator scaleInX = ObjectAnimator.ofFloat(menu3.getMenuIconView(), "scaleX", 0.2f, 1.0f);
ObjectAnimator scaleInY = ObjectAnimator.ofFloat(menu3.getMenuIconView(), "scaleY", 0.2f, 1.0f);
ObjectAnimator scaleInX = ObjectAnimator.ofFloat(menu3.getMenuIconView(), "scaleX", 0.1f, 5.0f);
ObjectAnimator scaleInY = ObjectAnimator.ofFloat(menu3.getMenuIconView(), "scaleY", 0.1f, 5.0f);

scaleOutX.setDuration(50);
scaleOutY.setDuration(50);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.github.clans.fab.sample;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Locale;

class LanguageAdapter extends RecyclerView.Adapter<LanguageAdapter.ViewHolder> {

private Locale[] mLocales;

LanguageAdapter(Locale[] mLocales) {
this.mLocales = mLocales;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
TextView tv = (TextView) LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_list_item_1, parent, false);

return new ViewHolder(tv);
}

@Override
public void onBindViewHolder(LanguageAdapter.ViewHolder holder, int position) {
holder.mTextView.setText(mLocales[position].getDisplayName());
}

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

static class ViewHolder extends RecyclerView.ViewHolder {

public TextView mTextView;

public ViewHolder(TextView v) {
super(v);
mTextView = v;
}
}
}
16 changes: 13 additions & 3 deletions sample/src/main/java/com/github/clans/fab/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void run() {
fab.show(true);
fab.setShowAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.show_from_bottom));
fab.setHideAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.hide_to_bottom));
fab.setShowAnimation(
AnimationUtils.loadAnimation(MainActivity.this, R.anim.show_from_bottom));
fab.setHideAnimation(
AnimationUtils.loadAnimation(MainActivity.this, R.anim.hide_to_bottom));
}
}, 300);

Expand All @@ -61,7 +63,8 @@ public void onScrollStateChanged(AbsListView view, int scrollState) {
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
if (firstVisibleItem > mPreviousVisibleItem) {
fab.hide(true);
} else if (firstVisibleItem < mPreviousVisibleItem) {
Expand All @@ -70,5 +73,12 @@ public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCoun
mPreviousVisibleItem = firstVisibleItem;
}
});

findViewById(R.id.fab_edit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, CoordinatorLayoutActivity.class));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,43 +115,6 @@ public void run() {
}
}

private class LanguageAdapter extends RecyclerView.Adapter<ViewHolder> {

private Locale[] mLocales;

private LanguageAdapter(Locale[] mLocales) {
this.mLocales = mLocales;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
TextView tv = (TextView) LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_list_item_1, parent, false);

return new ViewHolder(tv);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText(mLocales[position].getDisplayName());
}

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

private static class ViewHolder extends RecyclerView.ViewHolder {

public TextView mTextView;

public ViewHolder(TextView v) {
super(v);
mTextView = v;
}
}

private enum ProgressType {
INDETERMINATE, PROGRESS_POSITIVE, PROGRESS_NEGATIVE, HIDDEN, PROGRESS_NO_ANIMATION, PROGRESS_NO_BACKGROUND
}
Expand Down
Loading

0 comments on commit 76b8924

Please sign in to comment.