当前位置 : 主页 > 编程语言 > c++ >

一个Android项目中的主界面

来源:互联网 收集:自由互联 发布时间:2021-07-03
主界面Hub,项目采用MVP架构,这个Activity是View层中的一部分。 package com.yusys.mpos.main;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.view.LayoutIn
主界面Hub,项目采用MVP架构,这个Activity是View层中的一部分。
package com.yusys.mpos.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.yusys.mpos.R;
import com.yusys.mpos.common.YXApplication;
import com.yusys.mpos.common.bean.overall.LoginInfo;
import com.yusys.mpos.common.listener.YXResultListener;
import com.yusys.mpos.common.manager.InfoManager;
import com.yusys.mpos.common.widget.tabhost.YXTabHost;
import com.yusys.mpos.gather.GatherFragment;
import com.yusys.mpos.identification.IdentificationActivity;
import com.yusys.mpos.life.LifeFragment;
import com.yusys.mpos.login.LoginActivity;
import com.yusys.mpos.login.LoginContract;
import com.yusys.mpos.login.LoginPresenter;
import com.yusys.mpos.common.bean.request.LogoutRequest;
import com.yusys.mpos.manage.ManageFragment;
import com.yusys.mpos.query.QueryFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * 主界面
 *
 * @author yuanshuai (marshall.yuan@foxmail.com)
 * @since 2016-04-06 17:16
 */
public class MainActivity extends FragmentActivity {

    @Bind(android.R.id.tabhost)
    YXTabHost tabHost;
    private LayoutInflater inflater;

    LoginContract.Presenter mPresenter;

    private Class classes[] = {GatherFragment.class, QueryFragment.class, LifeFragment.class, ManageFragment.class};
    private int selectors[] = {R.drawable.selector_tab_gathering, R.drawable.selector_tab_query, R.drawable.selector_tab_life, R.drawable.selector_tab_manage};
    private String titles[] = {"收款", "交易查询", "生活", "个人管理"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        inflater = LayoutInflater.from(this);
        mPresenter = new LoginPresenter();
        initView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    private void initView() {
        tabHost.setup(this, getSupportFragmentManager(), R.id.framecontent);
        int count = classes.length;
        int i;
        for (i = 0; i < count; i++) {
            //为每一个Tab按钮设置图标、文字和内容
            TabSpec tabSpec = tabHost.newTabSpec(titles[i]).setIndicator(getTabItemView(i));
            //将Tab按钮添加进Tab选项卡中
            tabHost.addTab(tabSpec, i, classes[i], null);
        }

        LoginInfo loginInfo = InfoManager.getLoginInfo();
        // 判断商户审核状态,详见AuditStatus
        switch (loginInfo.merchantInfo.auditSts) {
            case "201":
            case "202":
                SweetAlertDialog dialog = new SweetAlertDialog(this, SweetAlertDialog.NORMAL_TYPE);
                dialog.setCancelable(false);
                dialog.setTitleText("审核未通过").setContentText("请等待审核通过后登录")
                        .setConfirmText("确定").setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(final SweetAlertDialog sweetAlertDialog) {
                        mPresenter.logout(new LogoutRequest(), new YXResultListener() {
                            @Override
                            public void onSuccess(Object result) {
                                sweetAlertDialog.cancel();
                                YXApplication.clearTempInfo();// 清除登录信息
                                startActivity(new Intent(MainActivity.this, LoginActivity.class));
                                finish();
                            }

                            @Override
                            public void onFailure(Object result) {
                                finish();
                            }
                        });
                    }
                }).show();
                break;
            case "200":
                startActivity(new Intent(MainActivity.this, IdentificationActivity.class));
                break;
            default:
                break;
        }
    }

    /**
     * 给Tab按钮设置图标和文字
     */
    private View getTabItemView(int index) {
        View root = inflater.inflate(R.layout.item_tab, null);
        ImageView imageView = (ImageView) root.findViewById(R.id.iv_tab_item);
        imageView.setImageResource(selectors[index]);
        TextView textView = (TextView) root.findViewById(R.id.tv_tab_item);
        textView.setText(titles[index]);
        return root;
    }

    @Override
    public void onBackPressed() {
    }

}
网友评论