492_fragment搭主界面





fragment搭主界面


public class MainActivity extends AppCompatActivity {


    private Context ctx = this;
    private View ll_tab0;
    private View ll_tab1;
    private View ll_tab2;
    private View ll_tab3;
    private ImageView iv_tab0;
    private ImageView iv_tab1;
    private ImageView iv_tab2;
    private ImageView iv_tab3;
    private TextView tv_tab0;
    private TextView tv_tab1;
    private TextView tv_tab2;
    private TextView tv_tab3;
    private FrameLayout fl_content;
    private MainFragment mainFragment;
    private FujinFragment fujinFragment;
    private JiekeFragment jiekeFragment;
    private WodeFragment wodeFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //初始化ViewsById
        initViewsById();
    }


    //==============================================================================================
    //初始化ViewsById
    private void initViewsById() {


        //初始化FrameLayout
        initFrameLayout();


        //初始化底部tab
        initBottomTab();


    }


    //==============================================================================================
    //初始化FrameLayout
    private void initFrameLayout() {
        fl_content = (FrameLayout) findViewById(R.id.fl_content);
        mainFragment = new MainFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.fl_content, mainFragment, "main").commitAllowingStateLoss();
    }


    //==============================================================================================
    //初始化底部tab
    private void initBottomTab() {
        ll_tab0 = findViewById(R.id.ll_tab0);
        ll_tab1 = findViewById(R.id.ll_tab1);
        ll_tab2 = findViewById(R.id.ll_tab2);
        ll_tab3 = findViewById(R.id.ll_tab3);
        iv_tab0 = (ImageView) findViewById(R.id.iv_tab0);
        iv_tab1 = (ImageView) findViewById(R.id.iv_tab1);
        iv_tab2 = (ImageView) findViewById(R.id.iv_tab2);
        iv_tab3 = (ImageView) findViewById(R.id.iv_tab3);
        tv_tab0 = (TextView) findViewById(R.id.tv_tab0);
        tv_tab1 = (TextView) findViewById(R.id.tv_tab1);
        tv_tab2 = (TextView) findViewById(R.id.tv_tab2);
        tv_tab3 = (TextView) findViewById(R.id.tv_tab3);
        ll_tab0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickBottomTab(0);
            }
        });
        ll_tab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickBottomTab(1);
            }
        });
        ll_tab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickBottomTab(2);
            }
        });
        ll_tab3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickBottomTab(3);
            }
        });


        clickBottomTab(0);
    }


    //==============================================================================================
    //底部tab点击事件
    private void clickBottomTab(int tab) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        clearTabAndFragment(transaction);
        switch (tab) {
            case 0:
                changeTab(tv_tab0, iv_tab0);
                if (mainFragment == null) {
                    mainFragment = new MainFragment();
                    transaction.add(R.id.fl_content, mainFragment, "main");
                } else {
                    transaction.show(mainFragment);
                }
                break;
            case 1:
                changeTab(tv_tab1, iv_tab1);
                if (fujinFragment == null) {
                    fujinFragment = new FujinFragment();
                    transaction.add(R.id.fl_content, fujinFragment, "fujin");
                } else {
                    transaction.show(fujinFragment);
                }
                break;
            case 2:
                changeTab(tv_tab2, iv_tab2);
                if (jiekeFragment == null) {
                    jiekeFragment = new JiekeFragment();
                    transaction.add(R.id.fl_content, jiekeFragment, "jieke");
                } else {
                    transaction.show(jiekeFragment);
                }
                break;
            case 3:
                changeTab(tv_tab3, iv_tab3);
                if (wodeFragment == null) {
                    wodeFragment = new WodeFragment();
                    transaction.add(R.id.fl_content, wodeFragment, "main");
                } else {
                    transaction.show(wodeFragment);
                }
                break;
        }
        transaction.commitAllowingStateLoss();
    }


    //==============================================================================================
    //重置tab和Fragment
    private void clearTabAndFragment(FragmentTransaction transaction) {
        iv_tab0.setImageResource(R.drawable.icon_float_window);
        iv_tab1.setImageResource(R.drawable.icon_float_window);
        iv_tab2.setImageResource(R.drawable.icon_float_window);
        iv_tab3.setImageResource(R.drawable.icon_float_window);
        tv_tab0.setTextColor(ColorUtil.parse("#999999"));
        tv_tab1.setTextColor(ColorUtil.parse("#999999"));
        tv_tab2.setTextColor(ColorUtil.parse("#999999"));
        tv_tab3.setTextColor(ColorUtil.parse("#999999"));
        if (mainFragment != null) {
            transaction.hide(mainFragment);
        }
        if (fujinFragment != null) {
            transaction.hide(fujinFragment);
        }
        if (jiekeFragment != null) {
            transaction.hide(jiekeFragment);
        }
        if (wodeFragment != null) {
            transaction.hide(wodeFragment);
        }
    }


    //==============================================================================================
    //改变tab状态
    private void changeTab(TextView tv, ImageView iv) {
        tv.setTextColor(ColorUtil.parse("#333333"));
        iv.setImageResource(R.drawable.icon_float_window_light);
    }


}





1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博私信或留言,博看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博删除。 6、可私信博看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博私信或留言,博看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博删除。 6、可私信博看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博私信或留言,博看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博删除。 6、可私信博看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值