Skip to content

Instantly share code, notes, and snippets.

@solkin
Created Oct 10, 2014
Embed
What would you like to do?
package ru.mail.im.ui;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;
import ru.mail.R;
import java.util.ArrayList;
import java.util.List;
@EViewGroup(R.layout.action_bar_view)
public class ActionBar extends ViewGroup {
private Measure measure = new Measure();
@ViewById(R.id.action_bar_back_button)
View backButton;
@ViewById(R.id.action_bar_default_content)
TextView defaultHeader;
View view;
private List<View> menuItems = new ArrayList<>();
private int viewGravity;
public ActionBar(Context context) {
super(context);
}
public ActionBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ActionBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@AfterViews
void initView() {
view = defaultHeader;
}
public void setMenu(MenuBuilder menu) {
for (View view : menuItems) {
removeView(view);
}
menuItems.clear();
for (int i = menu.size() - 1; i >= 0; i--) {
final MenuItemImpl item = menu.getItem(i);
if (item.isVisible()) {
View view = createViewForItem(item);
addView(view);
menuItems.add(view);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Activity activity = getActivity();
activity.onOptionsItemSelected(item);
}
});
}
}
}
public void setText(CharSequence text) {
removeView(view);
addView(defaultHeader);
view = defaultHeader;
defaultHeader.setText(text);
}
public void setTextColor(int color) {
defaultHeader.setTextColor(color);
}
private Activity getActivity() {
return (Activity) getContext();
}
private View createViewForItem(MenuItemImpl item) {
return ActionBarItem_.build(getContext()).setItem(item);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
measure.clear();
measure.width = width;
measure.height = MeasureSpec.getSize(heightMeasureSpec);
measure.maxHeight = 0;
measure(backButton, measure);
int backButtonWidth = getBackButtonWidth();
int menuWidth = 0;
for (int menuItemIndex = menuItems.size() - 1; menuItemIndex >= 0 ; menuItemIndex--) {
View menuItem = menuItems.get(menuItemIndex);
measure(menuItem, measure);
menuWidth += menuItem.getMeasuredWidth();
}
if (isCenterHorizontal()) {
measure.width = width - Math.max(backButtonWidth, menuWidth) * 2;
}
measure(view, measure);
boolean exact = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY;
setMeasuredDimension(width, exact ? measure.height : measure.maxHeight);
}
private boolean isCenterHorizontal() {
return (viewGravity & Gravity.CENTER_HORIZONTAL) == Gravity.CENTER_HORIZONTAL;
}
private boolean isRight() {
return (viewGravity & Gravity.RIGHT) == Gravity.RIGHT;
}
private boolean isFill() {
return (viewGravity & Gravity.FILL) == Gravity.FILL;
}
private void measure(View view, Measure measure) {
measureChild(view, atMost(measure.width), exact(measure.height));
measure.width -= view.getMeasuredWidth();
measure.maxHeight = Math.max(measure.maxHeight, view.getMeasuredHeight());
}
private static int atMost(int size) {
return MeasureSpec.makeMeasureSpec(size, MeasureSpec.AT_MOST);
}
private static int exact(int size) {
return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int originalWidth = r - l;
int width = originalWidth;
int height = b - t;
backButton.layout(0, 0, getBackButtonHeight(), getBackButtonWidth());
for (int menuItemIndex = menuItems.size() - 1; menuItemIndex >= 0 ; menuItemIndex--) {
View menuItem = menuItems.get(menuItemIndex);
int left = width - menuItem.getMeasuredWidth();
menuItem.layout(left, 0, width, menuItem.getMeasuredHeight());
width = left;
}
int left;
int right;
if (isFill()) {
right = width;
left = 0;
} else if (isCenterHorizontal()) {
left = (originalWidth - view.getMeasuredWidth()) / 2;
right = left + view.getMeasuredWidth();
} else if (isRight()) {
right = width;
left = right - view.getMeasuredWidth();
} else {
left = getBackButtonWidth();
right = left + view.getMeasuredWidth();
}
view.layout(left, 0, right, height);
}
@Click
void actionBarBackButtonClicked() {
getActivity().onBackPressed();
}
public void setView(View view) {
setView(view, 0);
}
public void setView(View view, int gravity) {
removeView(this.view);
addView(view);
this.view = view;
this.viewGravity = gravity;
}
private static class Measure {
int width,height,maxHeight;
void clear() {
width = height = maxHeight = 0;
}
}
public void setDisplayHomeAsUpEnabled(boolean enabled) {
backButton.setVisibility(enabled ? VISIBLE : GONE);
}
private int getBackButtonWidth() {
return backButton.getVisibility() == VISIBLE ? backButton.getMeasuredWidth() : 0;
}
private int getBackButtonHeight() {
return backButton.getVisibility() == VISIBLE ? backButton.getMeasuredHeight() : 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment