|
@@ -1,18 +1,24 @@
|
|
|
package com.sheishuo.app.common.views;
|
|
|
|
|
|
import android.content.Context;
|
|
|
+import android.graphics.Color;
|
|
|
import android.support.annotation.Nullable;
|
|
|
import android.support.v7.widget.Toolbar;
|
|
|
import android.util.AttributeSet;
|
|
|
import android.util.Log;
|
|
|
+import android.util.TypedValue;
|
|
|
import android.view.LayoutInflater;
|
|
|
import android.view.View;
|
|
|
+import android.view.ViewGroup;
|
|
|
+import android.widget.Button;
|
|
|
+import android.widget.EditText;
|
|
|
import android.widget.FrameLayout;
|
|
|
import android.widget.ImageView;
|
|
|
import android.widget.LinearLayout;
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
import com.bumptech.glide.Glide;
|
|
|
+import com.netease.nim.uikit.common.util.sys.ScreenUtil;
|
|
|
import com.sheishuo.app.R;
|
|
|
import com.sheishuo.app.common.util.img.ImgUtil;
|
|
|
|
|
@@ -25,14 +31,14 @@ import java.util.List;
|
|
|
|
|
|
public class BaseToolbar extends Toolbar {
|
|
|
|
|
|
- private final String TITLE = "title",LEFT_TEXT = "left_text",RIGHT_TEXT = "right_text";
|
|
|
private static Context context;
|
|
|
private FrameLayout toolbarLayout;
|
|
|
- private String titleStr = "",leftStr = "",rightStr = "";
|
|
|
- private TextView titleTV,leftTV,rightTV;
|
|
|
- private LinearLayout icoLayout;
|
|
|
- private ImageView indexIco;
|
|
|
|
|
|
+ private Toolbar sub_toolbar;
|
|
|
+ private TextView titleTxt;
|
|
|
+ private Button toolbarBtn;
|
|
|
+ private LinearLayout leftLayout;
|
|
|
+ private LinearLayout rightLayout;
|
|
|
|
|
|
public interface IcoBtnClickListener{
|
|
|
void onClick(View view);
|
|
@@ -44,183 +50,105 @@ public class BaseToolbar extends Toolbar {
|
|
|
this.context = context;
|
|
|
View view = LayoutInflater.from(context).inflate(R.layout.base_toolbar,this,true);
|
|
|
findViews(view);
|
|
|
- assert attrs != null;
|
|
|
- int count = attrs.getAttributeCount();
|
|
|
- for (int index = 0; index < count; index++) {
|
|
|
- String attributeName = attrs.getAttributeName(index);
|
|
|
- switch (attributeName){
|
|
|
- case TITLE:
|
|
|
- if (attrs.getAttributeValue(index) != null)
|
|
|
- titleTV.setText(titleStr = attrs.getAttributeValue(index));
|
|
|
- break;
|
|
|
- case LEFT_TEXT:
|
|
|
- if (attrs.getAttributeValue(index) != null)
|
|
|
- leftTV.setText(leftStr = attrs.getAttributeValue(index));
|
|
|
- break;
|
|
|
- case RIGHT_TEXT:
|
|
|
- if (attrs.getAttributeValue(index) != null)
|
|
|
- rightTV.setText(rightStr = attrs.getAttributeValue(index));
|
|
|
- break;
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
+ clearItems();
|
|
|
}
|
|
|
|
|
|
|
|
|
private void findViews(View view){
|
|
|
- titleTV = (TextView) view.findViewById(R.id.toolbar_title);
|
|
|
- leftTV = (TextView) view.findViewById(R.id.toolbar_left);
|
|
|
- rightTV = (TextView) view.findViewById(R.id.toolbar_right);
|
|
|
- icoLayout = (LinearLayout) view.findViewById(R.id.toolbar_ico_layout);
|
|
|
toolbarLayout = (FrameLayout) view.findViewById(R.id.toolbar_layout);
|
|
|
- indexIco = (ImageView) view.findViewById(R.id.toolbar_location_ico);
|
|
|
- }
|
|
|
|
|
|
-
|
|
|
- public FrameLayout getToolbarLayout() {
|
|
|
- return toolbarLayout;
|
|
|
- }
|
|
|
-
|
|
|
- public ImageView getIndexIco(){
|
|
|
- return indexIco;
|
|
|
- }
|
|
|
- public TextView getTitleTV() {
|
|
|
- return titleTV;
|
|
|
+ sub_toolbar = (Toolbar) findViewById(R.id.sub_toolbar);
|
|
|
+ leftLayout = (LinearLayout) findViewById(R.id.toolbar_left_items);
|
|
|
+ rightLayout = (LinearLayout) findViewById(R.id.toolbar_right_items);
|
|
|
+ titleTxt = (TextView) findViewById(R.id.toolbar_title_text);
|
|
|
}
|
|
|
|
|
|
- public TextView getLeftTV() {
|
|
|
- return leftTV;
|
|
|
- }
|
|
|
-
|
|
|
- public TextView getRightTV() {
|
|
|
- return rightTV;
|
|
|
+ public void clearItems()
|
|
|
+ {
|
|
|
+ leftLayout.removeAllViewsInLayout();
|
|
|
+ rightLayout.removeAllViewsInLayout();
|
|
|
}
|
|
|
|
|
|
+ public void setBackOnClickListener(OnClickListener listener)
|
|
|
+ {
|
|
|
+ if(listener != null)
|
|
|
+ {
|
|
|
+ ImageView backIcon = new ImageView(getContext());
|
|
|
+ backIcon.setImageResource(R.drawable.nim_actionbar_white_back_icon);
|
|
|
+ backIcon.setOnClickListener(listener);
|
|
|
+ setLeftItems(new View[]{backIcon});
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ setLeftItems(new View[0]);
|
|
|
+ }
|
|
|
|
|
|
- public void init(){
|
|
|
- this.setVisibility(VISIBLE);
|
|
|
- indexIco.setVisibility(GONE);
|
|
|
- titleTV.setVisibility(INVISIBLE);
|
|
|
- leftTV.setVisibility(INVISIBLE);
|
|
|
- rightTV.setVisibility(INVISIBLE);
|
|
|
- icoLayout.removeAllViews();
|
|
|
- icoLayout.setVisibility(GONE);
|
|
|
- titleTV.setOnClickListener(null);
|
|
|
- leftTV.setOnClickListener(null);
|
|
|
- rightTV.setOnClickListener(null);
|
|
|
- this.setLogo(null);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
public void setTitle(String title){
|
|
|
- getTitleTV().setText(title);
|
|
|
- titleTV.setVisibility(VISIBLE);
|
|
|
- }
|
|
|
-
|
|
|
- public void setTitle(int resId){
|
|
|
- String title = getResources().getString(resId);
|
|
|
- getTitleTV().setText(title);
|
|
|
- titleTV.setVisibility(VISIBLE);
|
|
|
- }
|
|
|
-
|
|
|
- public void setLeftText(String leftStr){
|
|
|
- getLeftTV().setText(leftStr);
|
|
|
- leftTV.setVisibility(VISIBLE);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public void setRightText(String rightStr){
|
|
|
- getRightTV().setText(rightStr);
|
|
|
- rightTV.setVisibility(VISIBLE);
|
|
|
-
|
|
|
+ titleTxt.setText(title);
|
|
|
}
|
|
|
|
|
|
- public LinearLayout getIcoLayout() {
|
|
|
- return icoLayout;
|
|
|
+ public TextView getTitleTxt()
|
|
|
+ {
|
|
|
+ return titleTxt;
|
|
|
}
|
|
|
|
|
|
- public void setIcoLayout(LinearLayout icoLayout) {
|
|
|
- this.icoLayout = icoLayout;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
+ public void setLeftItems(View[] views)
|
|
|
+ {
|
|
|
+ leftLayout.removeAllViewsInLayout();
|
|
|
|
|
|
-
|
|
|
- /**************用于为BaseToolbar增加额外功能***************/
|
|
|
-
|
|
|
-
|
|
|
- public static class Builder implements OnClickListener{
|
|
|
-
|
|
|
- BaseToolbar toolbar;
|
|
|
- private List<Integer> icoIds = new ArrayList<>();
|
|
|
- IcoBtnClickListener listener;
|
|
|
- Context context;
|
|
|
-
|
|
|
- public Builder create(Context context,@Nullable BaseToolbar toolbar){
|
|
|
- if (toolbar == null){
|
|
|
- this.toolbar = new BaseToolbar(context,null);
|
|
|
- }else {
|
|
|
- this.toolbar = toolbar;
|
|
|
+ for(int i=0; i<views.length; i++)
|
|
|
+ {
|
|
|
+ View view = views[i];
|
|
|
+ if(view instanceof ImageView)
|
|
|
+ {
|
|
|
+ ImageView imageView = (ImageView) view;
|
|
|
+ imageView.setLayoutParams(new ViewGroup.LayoutParams(ScreenUtil.dip2px(24), ScreenUtil.dip2px(24)));
|
|
|
+ imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
|
|
|
}
|
|
|
-
|
|
|
- this.context = context;
|
|
|
-
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 为Toolbar添加一个图标按钮
|
|
|
- *
|
|
|
- * 为了保证布局显示正常,若添加ico按钮则自动隐藏Right_Text
|
|
|
- * @param resId
|
|
|
- */
|
|
|
- public BaseToolbar.Builder addIcoButton(int resId){
|
|
|
- toolbar.rightTV.setVisibility(GONE);
|
|
|
- toolbar.icoLayout.setVisibility(VISIBLE);
|
|
|
- ImageView icoView = new ImageView(context);
|
|
|
- icoView.setAdjustViewBounds(true);
|
|
|
- int dpToPixel = (int) ImgUtil.convertDpToPixel(24,context);
|
|
|
- Glide.with(context)
|
|
|
- .load(resId)
|
|
|
- .override(dpToPixel,dpToPixel)
|
|
|
- .into(icoView);
|
|
|
- toolbar.icoLayout.addView(icoView);
|
|
|
- icoIds.add(resId);
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public BaseToolbar.Builder addIcoButtonClickListener(int index,IcoBtnClickListener listener){
|
|
|
- this.listener = listener;
|
|
|
- toolbar.getIcoLayout().getChildAt(index).setOnClickListener(this);
|
|
|
- Log.e("BaseToolbar",toolbar.getIcoLayout().getChildCount() + " index " + index);
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public BaseToolbar build(){
|
|
|
- return toolbar;
|
|
|
+ else if(view instanceof TextView)
|
|
|
+ {
|
|
|
+ TextView txtView = (TextView) view;
|
|
|
+ txtView.setTextColor(Color.WHITE);
|
|
|
+ txtView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
|
|
|
+ txtView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ view.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
|
|
|
+ }
|
|
|
+ leftLayout.addView(view);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
+ public void setRightItems(View[] views)
|
|
|
+ {
|
|
|
+ rightLayout.removeAllViewsInLayout();
|
|
|
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onClick(View v) {
|
|
|
- if (listener != null) listener.onClick(v);
|
|
|
+ for(int i=0; i<views.length; i++)
|
|
|
+ {
|
|
|
+ View view = views[i];
|
|
|
+ if(view instanceof ImageView)
|
|
|
+ {
|
|
|
+ ImageView imageView = (ImageView) view;
|
|
|
+ imageView.setLayoutParams(new ViewGroup.LayoutParams(ScreenUtil.dip2px(24), ScreenUtil.dip2px(24)));
|
|
|
+ imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
|
|
|
+ }
|
|
|
+ else if(view instanceof TextView)
|
|
|
+ {
|
|
|
+ TextView txtView = (TextView) view;
|
|
|
+ txtView.setTextColor(Color.WHITE);
|
|
|
+ txtView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
|
|
|
+ txtView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ view.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
|
|
|
+ }
|
|
|
+ rightLayout.addView(view);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
}
|