You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
8.7 KiB
Java

package com.common.commonlib.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.common.commonlib.R;
import com.common.commonlib.utils.DisplayUtils;
/**
*
*
* @author miracle
* @since 2021/8/2
*/
public class CommonTitleView extends FrameLayout implements View.OnClickListener {
private Context mContext;
private ImageView ivLeft;
private TextView tv_title;
private ImageView ivRight;
private TextView tv_edit;
private int showStyle;
private static final int STYLE_LEFT = 1;
private static final int STYLE_LEFT_MIDDLE = 2;
private static final int STYLE_LEFT_MIDDLE_RIGHT = 3;
private static final int STYLE_MIDDLE = 4;
private static final int STYLE_MIDDLE_RIGHT = 5;
private static final int STYLE_RIGHT = 6;
private static final int STYLE_EDIT = 7;
private String title;
//是否要固定高度的模式不受match parent\warp content等影响
private boolean isFixedHeight;
//设置固定的高度值,默认是48dp
private int fixedHeight;
private static final int DEFAULT_HEIGHT = 48;
private View rlLeftHotZone;
private View rlRightHotZone;
private View rlEditZone;
private Drawable leftDrawable;
private Drawable rightDrawable;
private int bgColor;
public CommonTitleView(Context context) {
this(context, null);
}
public CommonTitleView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CommonTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
init(attrs);
}
private void init(AttributeSet attrs) {
initAttrs(attrs);
View inflate = LayoutInflater.from(mContext).inflate(R.layout.common_title_view, this, true);
View root_view = findViewById(R.id.root_view);
ivLeft = findViewById(R.id.iv_left);
ivRight = findViewById(R.id.iv_right);
tv_title = findViewById(R.id.tv_title);
rlLeftHotZone = findViewById(R.id.rl_left_hot_zone);
rlRightHotZone = findViewById(R.id.rl_add_hot_zone);
tv_edit = findViewById(R.id.tv_edit);
rlEditZone = findViewById(R.id.rl_edit_hot_zone);
root_view.setBackgroundColor(bgColor);
if (leftDrawable != null) {
ivLeft.setImageDrawable(leftDrawable);
}
if (rightDrawable != null) {
ivRight.setImageDrawable(rightDrawable);
}
initStyle();
setTitle(title);
}
private void initAttrs(AttributeSet attrs) {
TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.CommonTitleView);
showStyle = typedArray.getInt(R.styleable.CommonTitleView_show_style, STYLE_MIDDLE);
title = typedArray.getString(R.styleable.CommonTitleView_title);
isFixedHeight = typedArray.getBoolean(R.styleable.CommonTitleView_is_fixed_height, true);
fixedHeight = typedArray.getInteger(R.styleable.CommonTitleView_fixed_height, DisplayUtils.INSTANCE.dp2px(mContext, DEFAULT_HEIGHT));
leftDrawable = typedArray.getDrawable(R.styleable.CommonTitleView_drawableLeft);
rightDrawable = typedArray.getDrawable(R.styleable.CommonTitleView_drawableRight);
bgColor = typedArray.getColor(R.styleable.CommonTitleView_bgColor, getResources().getColor(R.color.title_bar_bg_color));
typedArray.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (isFixedHeight) {
int makeMeasureSpec = MeasureSpec.makeMeasureSpec(fixedHeight, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, makeMeasureSpec);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
private void initStyle() {
switch (showStyle) {
case STYLE_LEFT:
ivLeft.setVisibility(VISIBLE);
rlLeftHotZone.setOnClickListener(this);
tv_title.setVisibility(INVISIBLE);
tv_title.setText("");
ivRight.setVisibility(INVISIBLE);
rlRightHotZone.setOnClickListener(null);
break;
case STYLE_LEFT_MIDDLE:
ivLeft.setVisibility(VISIBLE);
rlLeftHotZone.setOnClickListener(this);
tv_title.setVisibility(VISIBLE);
tv_title.setText("");
ivRight.setVisibility(INVISIBLE);
rlRightHotZone.setOnClickListener(null);
break;
case STYLE_MIDDLE_RIGHT:
ivLeft.setVisibility(INVISIBLE);
rlLeftHotZone.setOnClickListener(null);
tv_title.setVisibility(VISIBLE);
tv_title.setText("");
ivRight.setVisibility(VISIBLE);
rlRightHotZone.setOnClickListener(this);
break;
case STYLE_RIGHT:
ivLeft.setVisibility(INVISIBLE);
rlLeftHotZone.setOnClickListener(null);
tv_title.setVisibility(INVISIBLE);
tv_title.setText("");
ivRight.setVisibility(VISIBLE);
rlRightHotZone.setOnClickListener(this);
break;
case STYLE_LEFT_MIDDLE_RIGHT:
ivLeft.setVisibility(VISIBLE);
rlLeftHotZone.setOnClickListener(this);
tv_title.setVisibility(VISIBLE);
tv_title.setText("");
ivRight.setVisibility(VISIBLE);
rlRightHotZone.setOnClickListener(this);
break;
case STYLE_EDIT:
ivLeft.setVisibility(VISIBLE);
rlLeftHotZone.setOnClickListener(this);
tv_title.setVisibility(VISIBLE);
tv_title.setText("");
ivRight.setVisibility(GONE);
rlRightHotZone.setVisibility(GONE);
rlRightHotZone.setOnClickListener(null);
tv_edit.setVisibility(VISIBLE);
rlEditZone.setVisibility(VISIBLE);
rlEditZone.setOnClickListener(this);
break;
case STYLE_MIDDLE:
default:
ivLeft.setVisibility(INVISIBLE);
rlLeftHotZone.setOnClickListener(null);
tv_title.setVisibility(VISIBLE);
tv_title.setText("");
ivRight.setVisibility(INVISIBLE);
rlRightHotZone.setOnClickListener(null);
break;
}
}
public void setTitle(String title) {
this.title = title;
tv_title.setText(title);
}
@Override
public void onClick(View v) {
int viewId = v.getId();
if (viewId == R.id.rl_left_hot_zone) {
if (leftIconListener != null) {
leftIconListener.onClick();
}
} else if (viewId == R.id.rl_add_hot_zone) {
if (rightIconListener != null) {
rightIconListener.onClick();
}
} else if (viewId == R.id.rl_edit_hot_zone) {
if (editListener != null) {
editListener.onClick();
}
}
}
public interface LeftIconListener {
void onClick();
}
private LeftIconListener leftIconListener;
public void setLeftIconListener(LeftIconListener leftIconListener) {
this.leftIconListener = leftIconListener;
}
public interface RightIconListener {
void onClick();
}
private RightIconListener rightIconListener;
public void setRightIconListener(RightIconListener rightIconListener) {
this.rightIconListener = rightIconListener;
}
public interface EditListener {
void onClick();
}
private EditListener editListener;
public void setEditListener(EditListener editListener) {
this.editListener = editListener;
}
public int getEditState() {
if (tv_edit.getText().equals(mContext.getResources().getString(R.string.edit))) {
return 0;
} else if (tv_edit.getText().equals(mContext.getResources().getString(R.string.save))) {
return 1;
} else {
return -1;
}
}
public void setEditText(int state) {
if (state == 0) {
tv_edit.setText(mContext.getResources().getString(R.string.edit));
} else if (state == 1) {
tv_edit.setText(mContext.getResources().getString(R.string.save));
} else {
tv_edit.setText("");
}
}
}