This repository has been archived by the owner on Oct 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ 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
Showing
17 changed files
with
326 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
library/src/main/java/com/github/clans/fab/FloatingActionButtonBehavior.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
library/src/main/java/com/github/clans/fab/FloatingActionMenuBehavior.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
sample/src/main/java/com/github/clans/fab/sample/CoordinatorLayoutActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
sample/src/main/java/com/github/clans/fab/sample/LanguageAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.