美文网首页Android开发程序员iOS && Android
组合组件のCusRelativeLayout(Android-自

组合组件のCusRelativeLayout(Android-自

作者: kingpengLin | 来源:发表于2016-05-18 18:10 被阅读366次

最近在对公司的项目进行重构,在此坐下记录。

公司项目中的布局文件,可以抽出大部分公用组件,进行自定义组合组件,将这些公共的组件组合起来,在里面提供相应方法及变量,这样以后改界面就容易多了,只需改一处,处处都能生效;同时调用什么的,更方便快捷,代码也能得到规范及统一,特别是组件变量的命名。

每个界面中,用到的公共部分,总结起来有这么几个:

  • mTitleView 标题栏
  • mEmptyView 数据为空时的列表空界面
  • mListView 数据列表
  • mFailView 网络请求失败时,数据重新刷新页
  • mLoadView 网络请求时,数据加载的进度动画

由于我们项目中大部分应用的布局为RelativeLayout(相对布局),因此将自定义一个组件,并继承RelativeLayout,作为跟布局,引用到布局可以当做RelativeLayout那么处理,非常棒!!!

    public class CusRelativeLayout extends RelativeLayout

然后编写相应组件的布局文件xml

  • mTitleView --> layout_cus_titleview
  • mEmptyView --> layout_cus_emptyview
  • mListView --> layout_cus_listview
  • mFailView --> layout_cus_failview
  • mLoadView --> layout_cus_loadview

在构造函数中初始化,通过inflate方式生成布局对象,同时也获取自定义属性,并执行相应组件初始化操作

public CusRelativeLayout(Context context) {
        super(context);
        initView(context, null);
}

public CusRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
}

public CusRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
}
private void initView(Context context, AttributeSet attrs) {
    mContext = context;
    mEmptyText = context.getString(R.string.userhome_allempty);
    int marginTop = -1;
    boolean hasTitleView = true, hasListView = true;
    TypedArray a = null;
    if (attrs != null) {
        a = context.obtainStyledAttributes(attrs,R.styleable.CusRelativeLayout);
        mEmptyText = (String)a.getText(R.styleable.CusRelativeLayout_cus_empty_text);
        marginTop = a.getDimensionPixelOffset(R.styleable.CusRelativeLayout_cus_margin_top, -1);
        hasTitleView = a.getBoolean(R.styleable.CusRelativeLayout_cus_has_titleview, hasTitleView);
        hasListView = a.getBoolean(R.styleable.CusRelativeLayout_cus_has_listview, hasListView);
        }

        mLoadView = (ImageView) inflate(mContext, R.layout.layout_cus_loadview, null);
        mEmptyView = (TextView) inflate(mContext, R.layout.layout_cus_emptyview, null);
        mFailView = inflate(mContext, R.layout.layout_cus_failview, null);
        LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        
        ...and so on

自定义属性

    <declare-styleable name="CusRelativeLayout">
        <attr name="cus_empty_text" format="string"/><!--空界面,文案-->
        <attr name="cus_margin_top" format="dimension"/><!--空界面、失败请求页marginTop-->
        <attr name="cus_has_titleview_btn_back_visible" format="boolean"/><!--加载标题栏组件-返回键 默认false-->
        <attr name="cus_is_btn_cancel" format="boolean"/><!--返回键变为取消 默认false-->
        <attr name="cus_has_listview" format="boolean"/><!--加载列表组件 默认true-->
        <attr name="cus_has_titleview" format="boolean"/><!--加载标题栏组件 默认true-->
        <attr name="cus_click_titleview_listview_go_to_up" format="boolean"/><!--点击标题栏,列表回到顶部 默认false-->
</declare-styleable>

生成的组件对象,此时并未add进布局里,所以布局里面仍然为空,什么都木有,需要调用addView(View view)方法

注意!由于是相对布局,因此需要后面添加的View会覆盖在前面添加的View,因此我的组件,需要在布局Add完所有子View之后,在进行Add,这样显示的时候就可以覆盖其前面的子类,因此Add View的顺序也很重要,我们可以在onFinishInflate方法中Add 我们的公共View,该方法是布局Inflate完View之后执行的回调。

/**
     * Finalize inflating a view from XML.  This is called as the last phase
     * of inflation, after all child views have been added.
     *
     * <p>Even if the subclass overrides onFinishInflate, they should always be
     * sure to call the super method, so that we get called.
     */
    protected void onFinishInflate() {
    }

我的add view代码

@Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (mListView != null) {
            addView(mListView);
        }
        addView(mEmptyView);
        addView(mFailView);
        addView(mLoadView);
        if (mTitleView != null) {
            addView(mTitleView);
        }
    }

余下则编写相应的方法,抽出常用的部分写成方法并暴露给调用者,这部分代码就省略,每个人的业务不一样,方法提供也不一样,具体可以提供显示、隐藏相应组件等方法。

这样我们的自定义RelativeLayout已经完成,可以看下如何调用,就跟我们使用自定义布局一样,在xml文件里,作为跟布局即可

<?xml version="1.0" encoding="utf-8"?>
<com.babychat.view.Custom.CusRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rel_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:cus_click_titleview_listview_go_to_up="true"
    app:cus_has_titleview_btn_back_visible="true">

</com.babychat.view.Custom.CusRelativeLayout>

类中,我们通过findViewById获取根部局对象即可,然后就可以开始调用布局里面保留的方法、对象

 @Override
    protected void findViewById() {
        rel_parent = mFindViewById(R.id.rel_parent);
        rel_parent.mTvTitle.setText(R.string.special_history_list_title);
        rel_parent.mFailView.setBackgroundResource(R.color.transparent);
        rel_parent.mListView.setPullRefreshEnable(false);
        rel_parent.mListView.setmEnableAutoLoad(true);
    }

最后,总结一下,这些组件组合在一起,是为了减少轮子的重复制造,减少代码的冗余,并且统一代码规范,保持kiss原则

"Keep It Simple And Stupid"

“让它简单些,连笨蛋都看得懂”

相关文章

网友评论

    本文标题:组合组件のCusRelativeLayout(Android-自

    本文链接:https://www.haomeiwen.com/subject/topwrttx.html