Desc: 扩展CommonTitleView

Author: jiaojing
master
miracle 3 years ago
parent c0de65fc01
commit 5106621d35

@ -5,6 +5,18 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.common.commonlib.view.CommonTitleView
app:title="111"
app:drawableLeft="@drawable/ic_right"
app:drawableRight="@drawable/ic_right"
app:show_style="left_middle_right"
app:bgColor="@color/white"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/iv"
android:layout_width="240dp"

@ -2,6 +2,7 @@ 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;
@ -16,9 +17,9 @@ import com.common.commonlib.utils.DisplayUtils;
public class CommonTitleView extends FrameLayout implements View.OnClickListener {
private Context mContext;
private ImageView ivBack;
private ImageView ivLeft;
private TextView tv_title;
private ImageView ivAdd;
private ImageView ivRight;
private TextView tv_edit;
private int showStyle;
@ -36,9 +37,12 @@ public class CommonTitleView extends FrameLayout implements View.OnClickListener
private int fixedHeight;
private static final int DEFAULT_HEIGHT = 48;
private View rlBackHotZone;
private View rlAddHotZone;
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);
@ -56,18 +60,42 @@ public class CommonTitleView extends FrameLayout implements View.OnClickListener
private void init(AttributeSet attrs) {
initAttrs(attrs);
LayoutInflater.from(mContext).inflate(R.layout.common_title_view, this, true);
ivBack = findViewById(R.id.iv_back);
ivAdd = findViewById(R.id.iv_add);
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);
rlBackHotZone = findViewById(R.id.rl_back_hot_zone);
rlAddHotZone = findViewById(R.id.rl_add_hot_zone);
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) {
@ -81,80 +109,69 @@ public class CommonTitleView extends FrameLayout implements View.OnClickListener
private void initStyle() {
switch (showStyle) {
case STYLE_LEFT:
ivBack.setVisibility(VISIBLE);
rlBackHotZone.setOnClickListener(this);
ivLeft.setVisibility(VISIBLE);
rlLeftHotZone.setOnClickListener(this);
tv_title.setVisibility(INVISIBLE);
tv_title.setText("");
ivAdd.setVisibility(INVISIBLE);
rlAddHotZone.setOnClickListener(null);
ivRight.setVisibility(INVISIBLE);
rlRightHotZone.setOnClickListener(null);
break;
case STYLE_LEFT_MIDDLE:
ivBack.setVisibility(VISIBLE);
rlBackHotZone.setOnClickListener(this);
ivLeft.setVisibility(VISIBLE);
rlLeftHotZone.setOnClickListener(this);
tv_title.setVisibility(VISIBLE);
tv_title.setText("");
ivAdd.setVisibility(INVISIBLE);
rlAddHotZone.setOnClickListener(null);
ivRight.setVisibility(INVISIBLE);
rlRightHotZone.setOnClickListener(null);
break;
case STYLE_MIDDLE_RIGHT:
ivBack.setVisibility(INVISIBLE);
rlBackHotZone.setOnClickListener(null);
ivLeft.setVisibility(INVISIBLE);
rlLeftHotZone.setOnClickListener(null);
tv_title.setVisibility(VISIBLE);
tv_title.setText("");
ivAdd.setVisibility(VISIBLE);
rlAddHotZone.setOnClickListener(this);
ivRight.setVisibility(VISIBLE);
rlRightHotZone.setOnClickListener(this);
break;
case STYLE_RIGHT:
ivBack.setVisibility(INVISIBLE);
rlBackHotZone.setOnClickListener(null);
ivLeft.setVisibility(INVISIBLE);
rlLeftHotZone.setOnClickListener(null);
tv_title.setVisibility(INVISIBLE);
tv_title.setText("");
ivAdd.setVisibility(VISIBLE);
rlAddHotZone.setOnClickListener(this);
ivRight.setVisibility(VISIBLE);
rlRightHotZone.setOnClickListener(this);
break;
case STYLE_LEFT_MIDDLE_RIGHT:
ivBack.setVisibility(VISIBLE);
rlBackHotZone.setOnClickListener(this);
ivLeft.setVisibility(VISIBLE);
rlLeftHotZone.setOnClickListener(this);
tv_title.setVisibility(VISIBLE);
tv_title.setText("");
ivAdd.setVisibility(VISIBLE);
rlAddHotZone.setOnClickListener(this);
ivRight.setVisibility(VISIBLE);
rlRightHotZone.setOnClickListener(this);
break;
case STYLE_EDIT:
ivBack.setVisibility(VISIBLE);
rlBackHotZone.setOnClickListener(this);
ivLeft.setVisibility(VISIBLE);
rlLeftHotZone.setOnClickListener(this);
tv_title.setVisibility(VISIBLE);
tv_title.setText("");
ivAdd.setVisibility(GONE);
rlAddHotZone.setVisibility(GONE);
rlAddHotZone.setOnClickListener(null);
ivRight.setVisibility(GONE);
rlRightHotZone.setVisibility(GONE);
rlRightHotZone.setOnClickListener(null);
tv_edit.setVisibility(VISIBLE);
rlEditZone.setVisibility(VISIBLE);
rlEditZone.setOnClickListener(this);
break;
case STYLE_MIDDLE:
default:
ivBack.setVisibility(INVISIBLE);
rlBackHotZone.setOnClickListener(null);
ivLeft.setVisibility(INVISIBLE);
rlLeftHotZone.setOnClickListener(null);
tv_title.setVisibility(VISIBLE);
tv_title.setText("");
ivAdd.setVisibility(INVISIBLE);
rlAddHotZone.setOnClickListener(null);
ivRight.setVisibility(INVISIBLE);
rlRightHotZone.setOnClickListener(null);
break;
}
}
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));
typedArray.recycle();
}
public void setTitle(String title) {
this.title = title;
tv_title.setText(title);
@ -164,15 +181,13 @@ public class CommonTitleView extends FrameLayout implements View.OnClickListener
@Override
public void onClick(View v) {
int viewId = v.getId();
if (viewId == R.id.rl_back_hot_zone) {
if (backButtonListener != null) {
backButtonListener.onClick();
} else {
Navigation.findNavController(this).navigateUp();
if (viewId == R.id.rl_left_hot_zone) {
if (leftIconListener != null) {
leftIconListener.onClick();
}
} else if (viewId == R.id.rl_add_hot_zone) {
if (addListener != null) {
addListener.onClick();
if (rightIconListener != null) {
rightIconListener.onClick();
}
} else if (viewId == R.id.rl_edit_hot_zone) {
if (editListener != null) {
@ -181,24 +196,24 @@ public class CommonTitleView extends FrameLayout implements View.OnClickListener
}
}
public interface BackButtonListener {
public interface LeftIconListener {
void onClick();
}
private BackButtonListener backButtonListener;
private LeftIconListener leftIconListener;
public void setBackButtonListener(BackButtonListener backButtonListener) {
this.backButtonListener = backButtonListener;
public void setLeftIconListener(LeftIconListener leftIconListener) {
this.leftIconListener = leftIconListener;
}
public interface AddListener {
public interface RightIconListener {
void onClick();
}
private AddListener addListener;
private RightIconListener rightIconListener;
public void setAddListener(AddListener addListener) {
this.addListener = addListener;
public void setRightIconListener(RightIconListener rightIconListener) {
this.rightIconListener = rightIconListener;
}
public interface EditListener {

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/title_bar_bg_color"
@ -7,14 +8,14 @@
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/rl_back_hot_zone"
android:id="@+id/rl_left_hot_zone"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingStart="@dimen/font_20dp"
android:paddingEnd="@dimen/font_20dp">
<ImageView
android:id="@+id/iv_back"
android:id="@+id/iv_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
@ -57,7 +58,7 @@
android:paddingEnd="@dimen/font_20dp">
<ImageView
android:id="@+id/iv_add"
android:id="@+id/iv_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"

@ -11,8 +11,11 @@
<enum name="right" value="6" />
<enum name="edit" value="7" />
</attr>
<attr name="is_fixed_height" format="boolean"></attr>
<attr name="fixed_height" format="integer"></attr>
<attr name="is_fixed_height" format="boolean"/>
<attr name="fixed_height" format="integer"/>
<attr name="drawableLeft" format="reference"/>
<attr name="drawableRight" format="reference"/>
<attr name="bgColor" format="reference|color"/>
</declare-styleable>
</resources>
Loading…
Cancel
Save