欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Builder设计模式构建NavigationBar

程序员文章站 2022-05-28 23:30:41
...

Builder设计模式构建NavigationBar

首先定义布局:顶部的布局,一般分为左边返回键按钮,中间是title,右边是文本

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/title_bar_style">

    <TextView
        android:id="@+id/back"
        style="@style/back_view" />

    <TextView
        android:id="@+id/title"
        style="@style/title_text_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="75.0dip"
        android:layout_marginRight="75.0dip"
        android:ellipsize="end"
        android:singleLine="true"
        android:visibility="gone"
        />

    <TextView
        android:id="@+id/right_text"
        android:visibility="gone"
        style="@style/title_bar_button"
        android:layout_alignParentRight="true" />
</RelativeLayout>

title_bar_style:

<style name="title_bar_style">
        <item name="android:background">@color/title_bar_bg_day</item>
        <item name="android:paddingLeft">0.0dip</item>
        <item name="android:paddingRight">0.0dip</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">44.0dip</item>
    </style>
 <color name="title_bar_bg_day">#ffdcd9cf</color>

back_view:

<style name="back_view" parent="@style/title_bar_button">
        <item name="android:gravity">center</item>
        <item name="android:background">@drawable/btn_back</item>
        <item name="android:paddingLeft">0.0dip</item>
        <item name="android:paddingRight">0.0dip</item>
        <item name="android:layout_marginLeft">0.0dip</item>
        <item name="android:layout_marginRight">0.0dip</item>
        <item name="android:text">@null</item>
        <item name="android:drawableLeft">@null</item>
    </style>

title_text_style:

<style name="title_text_style">
        <item name="android:textSize">18.0sp</item>
        <item name="android:textColor">@color/s9</item>
    </style>
 <color name="s9">#ff6b5547</color>

title_bar_button

<style name="title_bar_button">
        <item name="android:textSize">16.0sp</item>
        <item name="android:textColor">@color/s9</item>
        <item name="android:gravity">center</item>
        <item name="android:background">@null</item>
        <item name="android:paddingLeft">5.0dip</item>
        <item name="android:paddingRight">5.0dip</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:layout_marginLeft">0.0dip</item>
        <item name="android:layout_marginRight">10.0dip</item>
    </style>
 <color name="s9">#ff6b5547</color>

首先定义一个接口:定义导航条的规范,有个布局的id和设置参数

public interface INavigationBar {
    /**
     * 头部的布局
     */

    public int bindLayoutld();

    /**
     * 绑定头部的参数
     */
    public void applyview();
}

定义一个基本的导航条类

public abstract class AbsNavigationBar<P extends AbsNavigationBar.Builder.AbsNavigationParams> implements INavigationBar {
    private P mParams;
    private View mNagivationView;

    public AbsNavigationBar(P params) {
        this.mParams = params;
        createAndBindView();
    }

    public P getParams() {
        return mParams;
    }


    /**
     * 创建和绑定view
     */
    private void createAndBindView() {
        if(mParams.mParent==null){
            ViewGroup viewGroup = (ViewGroup) ((Activity) mParams.mContext)
                    .findViewById(android.R.id.content);
            mParams.mParent = (ViewGroup) viewGroup.getChildAt(0);
        }
        if(mParams.mParent==null){
            return;
        }
        //1.创建View
        mNagivationView = LayoutInflater.from(mParams.mContext).inflate(bindLayoutld(), mParams.mParent, false);
        //2.添加
        mParams.mParent.addView(mNagivationView, 0);
        //3.放置参数
        applyview();
    }


    //AbsNavigationBar  Builder  参数params
    public abstract static class Builder {
        AbsNavigationParams P;

        public Builder(Context context, ViewGroup parent) {//目标parent
            P = new AbsNavigationParams(context, parent);
        }


        //放置参数
        public static class AbsNavigationParams {
            public Context mContext;
            public ViewGroup mParent;

            public AbsNavigationParams(Context context, ViewGroup parent) {
                this.mContext = context;
                this.mParent = parent;
            }
        }
       //由子类去实现builder
        public abstract AbsNavigationBar builder();
    }
    /**
     * 设置文本
     *
     * @param viewId id
     * @param text   设置的内容
     */
    public void setText(int viewId, String text) {
        TextView tv = findviewById(viewId);
        tv.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(text)) {
            tv.setText(text);
        }
    }

    /**
     * 设置点击事件
     * @param viewId  控件的id
     * @param listener  事件
     */
    public void setOnClickListener(int viewId, View.OnClickListener listener) {
        findviewById(viewId).setOnClickListener(listener);
    }

    public void setIcon(int viewId, int icon) {
        findviewById(viewId).setBackgroundResource(icon);
    }
    /**
     * 减少findview的次数
     */
    private <T extends View> T findviewById(int viewId) {
        return (T) mNagivationView.findViewById(viewId);
    }

}

默认的导航条

public class DefaultNavigationBar extends AbsNavigationBar<DefaultNavigationBar.Builder.DefaultNavigationParams> {
    public DefaultNavigationBar(DefaultNavigationBar.Builder.DefaultNavigationParams params) {
        super(params);
    }

    @Override
    public int bindLayoutld() {
        return R.layout.title_bar;
    }

    @Override
    public void applyview() {
        //绑定效果
        setText(R.id.title, getParams().mTitle);
        setText(R.id.right_text, getParams().mRightText);
        setOnClickListener(R.id.right_text, getParams().mRightOnClickListener);
        setOnClickListener(R.id.back, getParams().mLeftOnClickListener);
        setIcon(R.id.right_text, getParams().mRightIcon);
    }


    public static class Builder extends AbsNavigationBar.Builder {
        DefaultNavigationParams P;

        public Builder(Context context, ViewGroup parent) {
            super(context, parent);
            P = new DefaultNavigationParams(context, parent);
        }



        //1.设置所有效果
        //设置标题
        public DefaultNavigationBar.Builder setTitle(String title) {
            P.mTitle = title;
            return this;
        }

        //设置右边文字
        public DefaultNavigationBar.Builder setRightText(String rightText) {
            P.mRightText = rightText;
            return this;
        }

        //设置右边图标
        public DefaultNavigationBar.Builder setRightIcon(int rightIcon) {
            P.mRightIcon = rightIcon;
            return this;
        }

        //设置右边点击事件
        public DefaultNavigationBar.Builder setRightOnClickListener(View.OnClickListener rightOnClickListener) {
            P.mRightOnClickListener = rightOnClickListener;
            return this;
        }

        //设置左边文字
        public DefaultNavigationBar.Builder setLeftText(String leftText) {
            P.mLeftText = leftText;
            return this;
        }

        //设置左边图标
        public DefaultNavigationBar.Builder setLeftIcon(int leftIcon) {
            P.mLeftIcon = leftIcon;
            return this;
        }

        //设置左边点击事件
        public DefaultNavigationBar.Builder setLeftOnClickListener(View.OnClickListener leftOnClickListener) {
            P.mLeftOnClickListener = leftOnClickListener;
            return this;
        }

        @Override
        public DefaultNavigationBar builder() {
            DefaultNavigationBar navigationBar = new DefaultNavigationBar(P);
            return navigationBar;
        }

        public static class DefaultNavigationParams extends AbsNavigationParams {
            public String mTitle;//设置标题
            public String mRightText;//右边文字标题
            public int mRightIcon;//右边图片
            public View.OnClickListener mRightOnClickListener;//设置右边点击事件
            public String mLeftText;//左边文字标题
            public int mLeftIcon;//左边图标
            public View.OnClickListener mLeftOnClickListener = new View.OnClickListener() {//默认退出程序
                @Override
                public void onClick(View v) {
                    ((Activity) mContext).finish();
                }
            };//左边点击事件

            //2.放置所有效果
            public DefaultNavigationParams(Context context, ViewGroup parent) {
                super(context, parent);
            }
        }
    }
}

简单使用

  DefaultNavigationBar navigationBar=new DefaultNavigationBar.Builder
//                (this, (ViewGroup) findViewById(R.id.view_group))

                (this,null)
//                .setRightIcon(R.drawable.account_icon_weibo)
                .setRightText("发布")
                .setTitle("投稿")
                .setRightOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "发布", Toast.LENGTH_SHORT).show();
                    }
                })
                .builder();
相关标签: Builder Navigation