Android的Activity逻辑代码太多怎么办?

背景:

做过安卓开发的童鞋都有这个体会,随着业务的增多,activity代码量越来越多,少则几百行,多则上千行,翻看个方法鼠标滚轮滑动半天,相比这样的项目谁也不愿意接手,那么目前采取了很多方法来减少activity逻辑代码,比如众所周知的MVP,具体我就不过多介绍,接下来分享一个我平时的做法。

步入正题:
当activity逻辑比较多的时候,一般布局也是比较复杂的,那么我们可以对布局进行类别区分。举个简单例子,用户登录页面,可以分为用户输入信息模块登录按钮的登录操作,我们可以对这两个模块进行抽取,把他们的父布局自定义,然后在各自的父布局里进行逻辑处理,可能有童鞋看到这不知道我在说啥,没事我上代码吧。

<RelativeLayout
            android:id="@+id/loading_btn"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_marginTop="150dp"
            android:background="@drawable/bg_login">

            <TextView
                android:id="@+id/loading_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:textColor="@color/white"
                android:textSize="15dp" />

            <ImageView
                android:id="@+id/loading_img"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginRight="3dp"
                android:layout_toLeftOf="@+id/loading_text"
                android:src="@drawable/loading_w"
                android:visibility="invisible" />

        </RelativeLayout>

这是一个登录按钮,TextView现实按钮中间的字,ImageView显示转圈的图片,当网络请求时隐藏TextView然后把ImageView的转圈效果显示出来,请求完毕再隐藏ImageView显示TextView。这只是一个简单的例子,试想一下假如这个RelativeLayout包含更多的控件呢,可能需要很多逻辑对控件进行操作,把这些逻辑都放在activity里是不是太糟糕了?因此我们可以把这个RelativeLayout抽成一个view,接下来看代码

public class LoadingView extends RelativeLayout {
    private Context   mContext;
    private ImageView mLoadingImg;
    private Animation mLoadingAnim;
    private TextView  mText;

    public LoadingView(Context context) {
        super(context);
        mContext = context;
    }

    public LoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public LoadingView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if(isInEditMode()) {
            return;
        }
        initView();
    }

    private void initView() {
        mLoadingAnim = AnimationUtils.loadAnimation(mContext,R.anim.loading_anim);
        LinearInterpolator interpolator = new LinearInterpolator();
        mLoadingAnim.setInterpolator(interpolator);

        mLoadingImg  = (ImageView) findViewById(R.id.loading_img);
        mText        = (TextView) findViewById(R.id.loading_text);
    }

布局修改之后:

<com.hht.openlive.view.LoadingView
            android:id="@+id/loading_btn"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:layout_marginTop="150dp"
            android:background="@drawable/bg_login">

            <TextView
                android:id="@+id/loading_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:textColor="@color/white"
                android:textSize="15dp" />

            <ImageView
                android:id="@+id/loading_img"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginRight="3dp"
                android:layout_toLeftOf="@+id/loading_text"
                android:src="@drawable/loading_w"
                android:visibility="invisible" />

        </com.hht.openlive.view.LoadingView>

这样做我们就可以在LoadingView进行findViewById取到LoadingView所包含的控件,然后在LoadingView提供public方法让activity调用,由LoadingView内部进行逻辑处理,这样我们就可以通过抽取view的方法把一部分代码从activity分离出来,而且这样还有一个好处,你可以根据功能区分模块,把activity细分好多个模块,各个模块处理各自的逻辑。
在这里插入图片描述
这是我们项目的目录,后期需要代码改动的时候,我们可以根据view的名字能很清晰的知道去那个view里修改代码,而不用在activity里漫游了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值