在activity上添加Fragment及底部按钮切换

package com.example.a1508bnsg.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.example.a1508bnsg.R;
import com.example.a1508bnsg.fragment.Personal_fragment04;
import com.example.a1508bnsg.fragment.Shopping_fragment03;
import com.example.a1508bnsg.fragment.classification_fragment02;
import com.example.a1508bnsg.fragment.homepage_fragment01;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private FrameLayout frameLayout;
    private ImageView iv_nav_home_press;
    private ImageView iv_nav_class;
    private ImageView iv_nav_cart;
    private ImageView iv_nav_user;
    private homepage_fragment01 fragment01;
    private classification_fragment02 fragment02;
    private Shopping_fragment03 fragment03;
    private Personal_fragment04 fragment04;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvents();
        FragmentTransaction  fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment, new homepage_fragment01());
        fragmentTransaction.commit();
        iv_nav_home_press.setImageResource(R.mipmap.ic_nav_home_press);
    }


    private void initView() {
        Button mBt = (Button) findViewById(R.id.bt);
        iv_nav_home_press = (ImageView) findViewById(R.id.iv_nav_home_press);
        iv_nav_class = (ImageView) findViewById(R.id.iv_nav_class);
        iv_nav_cart = (ImageView) findViewById(R.id.iv_nav_cart);
        iv_nav_user = (ImageView) findViewById(R.id.iv_nav_user);
        frameLayout = (FrameLayout) findViewById(R.id.fragment);
    }

    private void initEvents() {
        iv_nav_home_press.setOnClickListener(this);
        iv_nav_class.setOnClickListener(this);
        iv_nav_cart.setOnClickListener(this);
        iv_nav_user.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        reset();
        FragmentTransaction  fragmentTransaction = getSupportFragmentManager().beginTransaction();
        switch (v.getId()) {
            case R.id.iv_nav_home_press:

                fragmentTransaction.replace(R.id.fragment, new homepage_fragment01());
                fragmentTransaction.commit();
                iv_nav_home_press.setImageResource(R.mipmap.ic_nav_home_press);
                break;
            case R.id.iv_nav_class:
                fragmentTransaction.replace(R.id.fragment, new classification_fragment02());
                fragmentTransaction.commit();
                iv_nav_class.setImageResource(R.mipmap.ic_nav_class_press);
                break;
            case R.id.iv_nav_cart:
                fragmentTransaction.replace(R.id.fragment, new Shopping_fragment03());
                fragmentTransaction.commit();
                iv_nav_cart.setImageResource(R.mipmap.ic_nav_cart_press);
                break;
            case R.id.iv_nav_user:
                fragmentTransaction.replace(R.id.fragment, new Personal_fragment04());
                fragmentTransaction.commit();
                iv_nav_user.setImageResource(R.mipmap.ic_nav_user_press);
                break;
        }
    }

    public void reset(){
        iv_nav_home_press.setImageResource(R.mipmap.ic_nav_home);
        iv_nav_class.setImageResource(R.mipmap.ic_nav_class);
        iv_nav_cart.setImageResource(R.mipmap.ic_nav_cart);
        iv_nav_user.setImageResource(R.mipmap.ic_nav_user);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值