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); } }
在activity上添加Fragment及底部按钮切换
最新推荐文章于 2023-06-26 14:52:41 发布