Last active
August 29, 2015 14:16
-
-
Save alorma/282fddf92ed8644bcbfc to your computer and use it in GitHub Desktop.
FabLayout with center ,left or right position
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<declare-styleable name="FABCenterLayout"> | |
<attr name="top_id" format="reference"/> | |
<attr name="location" format="enum"> | |
<enum name="left" value="0" /> | |
<enum name="right" value="1" /> | |
<enum name="center" value="2" /> | |
</attr> | |
</declare-styleable> | |
</resources> |
This file contains hidden or 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
package com.worldline.evasdk.view.widget; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.Color; | |
import android.graphics.drawable.Drawable; | |
import android.support.v4.view.ViewCompat; | |
import android.util.AttributeSet; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.RelativeLayout; | |
import android.widget.Toast; | |
import com.getbase.floatingactionbutton.AddFloatingActionButton; | |
import com.nineoldandroids.view.ViewHelper; | |
import com.worldline.evasdk.R; | |
/** | |
* Created by Bernat on 26/08/2014. | |
*/ | |
public class FABCenterLayout extends RelativeLayout { | |
private static final int LOCATION_LEFT = 0; | |
private static final int LOCATION_RIGHT = 1; | |
private static final int LOCATION_CENTER = 2; | |
private int topId; | |
private OnClickListener fabClickListener; | |
private boolean fabVisible; | |
private int fabLocation; | |
private String fabTag; | |
private AddFloatingActionButton fabView; | |
private View topView; | |
private View[] translatableViews; | |
public FABCenterLayout(Context context) { | |
super(context); | |
init(null, 0); | |
} | |
public FABCenterLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(attrs, 0); | |
} | |
public FABCenterLayout(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(attrs, defStyle); | |
} | |
private void init(AttributeSet attrs, int defStyle) { | |
isInEditMode(); | |
if (attrs != null) { | |
TypedArray attr = getContext().obtainStyledAttributes(attrs, R.styleable.FABCenterLayout, defStyle, 0); | |
if (attr.hasValue(R.styleable.FABCenterLayout_top_id)) { | |
topId = attr.getResourceId(R.styleable.FABCenterLayout_top_id, 0); | |
if (topId != 0) { | |
fabVisible = true; | |
createFabView(); | |
} | |
fabLocation = attr.getInt(R.styleable.FABCenterLayout_location, LOCATION_RIGHT); | |
attr.recycle(); | |
} | |
} | |
} | |
private void createFabView() { | |
fabView = (AddFloatingActionButton) LayoutInflater.from(getContext()).inflate(R.layout.fab, this, false); | |
ViewCompat.setElevation(fabView, 6f); | |
fabView.setOnClickListener(fabClickListener); | |
setFabTag(); | |
} | |
@Override | |
protected void onLayout(boolean changed, int l, int t, int r, int b) { | |
super.onLayout(changed, l, t, r, b); | |
if (topView != null && fabVisible && fabView != null) { | |
int bottom = topView.getBottom(); | |
if (bottom > 0) { | |
int int8 = getResources().getDimensionPixelOffset(R.dimen.gapMedium); | |
if (fabLocation == LOCATION_LEFT) { | |
fabView.layout(l + int8, bottom - fabView.getHeight() / 2, l + int8 + fabView.getWidth(), bottom + fabView.getHeight() / 2); | |
} else if (fabLocation == LOCATION_RIGHT) { | |
fabView.layout(r - fabView.getWidth() - int8, bottom - fabView.getHeight() / 2, r - int8, bottom + fabView.getHeight() / 2); | |
} else if (fabLocation == LOCATION_CENTER) { | |
int centerX = (topView.getRight() - topView.getLeft()) / 2; | |
fabView.layout(centerX - fabView.getWidth() / 2, bottom - fabView.getHeight() / 2, centerX + fabView.getWidth() / 2, bottom + fabView.getHeight() / 2); | |
} | |
removeView(fabView); | |
addView(fabView); | |
} | |
} | |
} | |
public void setFabIcon(Drawable drawable) { | |
if (fabView != null) { | |
fabView.setImageDrawable(drawable); | |
} | |
} | |
public void setFabClickListener(OnClickListener fabClickListener, final String tag) { | |
this.fabClickListener = fabClickListener; | |
this.fabTag = tag; | |
if (fabView != null) { | |
fabView.setOnClickListener(fabClickListener); | |
setFabTag(); | |
} | |
} | |
private void setFabTag() { | |
if (fabView != null && fabTag != null) { | |
fabView.setTag(fabTag); | |
fabView.setOnLongClickListener(new OnLongClickListener() { | |
@Override | |
public boolean onLongClick(View v) { | |
Toast.makeText(v.getContext(), String.valueOf(v.getTag()), Toast.LENGTH_SHORT).show(); | |
return true; | |
} | |
}); | |
} | |
} | |
public void setFabViewVisibility(int visibility) { | |
if (fabView != null) { | |
fabView.setVisibility(visibility); | |
} | |
} | |
public void translateHeader(float scroll) { | |
ViewHelper.setTranslationY(topView, scroll); | |
int size = topView.getHeight(); | |
if (translatableViews != null) { | |
for (View translatableView : translatableViews) { | |
size -= translatableView.getHeight(); | |
ViewHelper.setTranslationY(translatableView, scroll); | |
} | |
} | |
size += (fabView.getHeight() / 2); | |
if (scroll > -size) { | |
ViewHelper.setTranslationY(fabView, scroll); | |
} else { | |
ViewHelper.setTranslationY(fabView, scroll + (fabView.getHeight() / 2)); | |
} | |
} | |
public int getFabId() { | |
return fabView != null ? fabView.getId() : 0; | |
} | |
@Override | |
public void addView(View child, int index, ViewGroup.LayoutParams params) { | |
super.addView(child, index, params); | |
if (child.getId() == topId) { | |
topView = child; | |
} else { | |
View viewTop = child.findViewById(topId); | |
if (topView == null && viewTop != null) { | |
topView = viewTop; | |
} | |
} | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
int count = getChildCount(); | |
int width = getMeasuredWidth(); | |
int height = 0; | |
for (int i = 0; i < count; i++) { | |
View child = getChildAt(i); | |
if (child != fabView) { | |
height += child.getHeight(); | |
} | |
} | |
setMeasuredDimension(width, height); | |
} | |
public void addTranslatableViews(View... views) { | |
translatableViews = views; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment