Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

一个界面管理多种状态方案

$
0
0

前言

很多项目中进行会出现,一个状态对应一种现实样式。是统一在一个界面处理,还是分开n多页面处理呢,
这篇给出了一点个人建议,及是实现方案。

效果图

这里写图片描述

方案一: 一个activity 全部进行控制。所有的状态。完全靠堆代码。(不推荐)
方案二 一个activity 中包含BaseFragment 然后 subfragment中实现不同状态。(推荐)
方案3

方案一:

优点:

 代码量少,处理简单ui问题。完全可以写到一个activity中。

缺点:

 后期维护,绝对是噩梦,7,8种状态,来回变换,各种逻辑穿插。导致类膨胀。

方案二:

优点:

 结构简单,单个类代码量少,清晰。便于维护,扩展性高,符合单一职责

缺点:

 前期工作量相对大于方案一

2.方案二 uml类图:

这里写图片描述

抽取基类就是为了复用。 能复用的尽量放入基类,统一入口。

基类职责:

  1.N多subFragment 公用一套 xml。(如果使用N个布局对应N个subfragment,重复工作量太大。)初始化root布局

2.特殊布局,分发给subFragment去处理,公共区域更新由基类完成。

3.统一处理一些逻辑。这里是处理状态对应文案,以及文案颜色等

4.作为工厂类,生成对应样式的subFragment

subFragmnet 职责:

1.更新ui
2.隐藏不需要的xml 组件。默认展示所有xml组件。

Activity :

1.作为BaseFragment容器,当然这是常识,fragment无法单独用来展示ui,必须依附于Activity。
2. 作为Mvp模式中的view,网络请求成功会先走到Activity中,然后通过baseFramgnet,分发数据到具体subFragment中。(fragmen同样可以作为View,这里为了统一处理,较小难度。用Activity做为View)

Eg:mvp模式不在这里介绍。有需要了解的自行前往google 官方sample地址查看。

说道这里觉得少了点:

贴代码:

subFragment

public class subFragment1 extends BaseBorrowFragment {

    private static BorrowCancelFragment instance;

    public static BorrowCancelFragment getInstance() {
        if (instance == null) {
            instance = new BorrowCancelFragment();
        }
        return instance;
    }

    @Override
    public View initLayout(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.initLayout(inflater, container, savedInstanceState);
    }

    @Override
    void updateUi(LoanDetailResponse loanDetailResponse) {
        LoanApplyItemEntity loanApplyItem = loanDetailResponse.loanApplyItem;
        nyBorrowMoney.setRightString(getUnitString(loanApplyItem.applyAmount));
        nyBorrowDeadline.setRightString(getDataStyle(loanApplyItem));
        nyBorrowDate.setRightString(loanApplyItem.lendTime);

        nyPayWay.setRightString(loanApplyItem.repaymentTypeStr);
        nyPayCancelReason.setRightString(loanApplyItem.cancelReason);
    }

    @Override
    public void init() {

    }

    @Override
    public void initView() {
        super.initView();
    }

    @Override
    public void initValue() {
        nyBorrowProtocol.setVisibility(View.GONE);
        nyNeedPayDate.setVisibility(View.GONE);
        nyAlreadyMoney.setVisibility(View.GONE);
        nyWaitMoney.setVisibility(View.GONE);
        nyRealPayDate.setVisibility(View.GONE);
        nyRealPayMoney.setVisibility(View.GONE);
    }

    @Override
    public void initListener() {

    }
}

baseFragment

public abstract class BasexFragment extends BaseFragment {

    protected Activity baseActivity;
    protected OnContactSelectedListener mListener; //回调监听
    protected LinearLayout lineLayer1, lineLayer2, lineLayer3;
    protected NyCompatView nyBorrowMoney, nyBorrowDeadline, nyBorrowDate,
            nyBorrowProtocol, nyPayWay, nyNeedPayDate, nyAlreadyMoney,
            nyWaitMoney, nyRealPayDate, nyRealPayMoney, nyPayCancelReason;

    private TextView txtCommAccount;
    private TextView txtCommDeadline;
    private TextView txtCommBorrowState;
    private TextView txtChangeValue;


    @Override
    public View initLayout(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_common_borrow_detail, container, false);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.baseActivity = activity;
        try {
            mListener = (OnContactSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + "must implement OnContactSelectedListener");
        }
    }

    @Override
    public void initView() {

        txtChangeValue = findView(R.id.txt_change_value);
        txtCommAccount = findView(R.id.txt_comm_account);
        txtCommDeadline = findView(R.id.txt_comm_deadline);
        txtCommBorrowState = findView(R.id.txt_comm_borrow_state);


        lineLayer1 = findView(R.id.line_layer1);
        lineLayer2 = findView(R.id.line_layer2);
        lineLayer3 = findView(R.id.line_layer3);
        nyBorrowMoney = findView(R.id.ny_borrow_money);
        nyBorrowDeadline = findView(R.id.ny_borrow_deadline);
        nyBorrowDate = findView(R.id.ny_borrow_date);
        nyBorrowProtocol = findView(R.id.ny_borrow_protocol);
        nyPayWay = findView(R.id.ny_pay_way);
        nyNeedPayDate = findView(R.id.ny_need_pay_date);
        nyAlreadyMoney = findView(R.id.ny_already_money);
        nyWaitMoney = findView(R.id.ny_wait_money);
        nyRealPayDate = findView(R.id.ny_real_pay_date);
        nyRealPayMoney = findView(R.id.ny_real_pay_money);
        nyPayCancelReason = findView(R.id.ny_pay_cancel_reason);
    }

    /**
     * 更新ui
     *
     * @param loanDetailResponse
     */
    protected void updateUi(LoanDetailResponse loanDetailResponse) {
        refreshHeaderUi(loanDetailResponse);
    }

    public void refreshHeaderUi(LoanDetailResponse response) {
        String[] strings = getShowTextValue(response.loanApplyItem);
        txtCommAccount.setText(strings[1]);
        txtChangeValue.setText(strings[0]);

        txtCommDeadline.setText(response.loanApplyItem.repaymentTime);
        txtCommBorrowState.setText(response.loanApplyItem.sItemStatus);
        txtCommBorrowState.setTextColor(getStateColor(response.loanApplyItem.itemStatus));
    }


    protected String getFormatData(LoanApplyItemEntity loanApplyItemEntity) {
        int period = loanApplyItemEntity.period;
        int period_unit = loanApplyItemEntity.periodUnit;
        return FormatUtil.getProTimeStr(period, period_unit);
    }



    protected String getUnitString(String applyAmount) {
        return "¥" + applyAmount;
    }


    public static BaseBorrowFragment borrowFactory(int state) {
        BaseBorrowFragment baseBorrowFragment = null;
        switch (state) {
            case 1:
                baseBorrowFragment = subFragment1.getInstance();
                break;
            case 2:
                baseBorrowFragment = subFragment2.getInstance();
                break;
            case 3:
                baseBorrowFragment = subFragment3.getInstance();
                break;
            case 4:
                baseBorrowFragment = subFragment4.getInstance();
                break;
            case 5:
                baseBorrowFragment = subFragment5.getInstance();
                break;
            case 6:
                baseBorrowFragment = subFragment6.getInstance();
                break;
            case 7:
                baseBorrowFragment = subFragment7.getInstance();
                break;
        }
        return baseBorrowFragment;
    }


    private int getStateColor(int currentStateValue) {
        int color = 0;
        switch (currentStateValue) {
            case 1:
            case 2:
                color = getResources().getColor(R.color.color_00B0FF);
                break;
            case 3:
            case 4:
                color = getResources().getColor(R.color.color_ff6f00);
                break;
            case 5:
            case 6:
            case 7:
                color = getResources().getColor(R.color.txt_color_333333);
                break;

            default:
        }
        return color;

    }


}
作者:o279642707 发表于2017/8/18 17:05:11 原文链接
阅读:0 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>