Android点击底部按钮变状态切换页面

Android点击底部按钮变状态切换页面_第1张图片Android点击底部按钮变状态切换页面_第2张图片

 新建三个Fragment

MainActivity:

package com.example.administrator.switchpage;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout mOneLin, mTwoLin, mThreeLin;
    private ImageView mOneImg,mTwoImg,mThreeImg;

    private FrameLayout mFrameLayout;
    private OneFragment mOneFragment;
    private TwoFragment mTwoFragment;
    private ThreeFragment mThreeFragment;
    private FragmentManager manager;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFrameLayout = findViewById(R.id.frame);
        mOneLin = findViewById(R.id.one_lin);
        mTwoLin = findViewById(R.id.two_lin);
        mThreeLin = findViewById(R.id.three_lin);
        mOneImg=findViewById(R.id.one_img);
        mTwoImg=findViewById(R.id.two_img);
        mThreeImg=findViewById(R.id.three_img);
        mOneLin.setOnClickListener(this);
        mTwoLin.setOnClickListener(this);
        mThreeLin.setOnClickListener(this);
        //获取FragmentManager对象
        manager = getSupportFragmentManager();
        //获取FragmentTransaction对象
        transaction = manager.beginTransaction();
        setSwPage(0);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.one_lin:
                setSwPage(0);
                break;
            case R.id.two_lin:
                setSwPage(1);
                break;
            case R.id.three_lin:
                setSwPage(2);
                break;
        }
    }

    public void setSwPage(int i) {
        //获取FragmentManager对象
        manager = getSupportFragmentManager();
        //获取FragmentTransaction对象
        transaction = manager.beginTransaction();
        //先隐藏所有的Fragment
        hideFragments(transaction);
        switch (i) {
            case 0:
                reImgSelect();
                mOneImg.setSelected(true);
                if (mOneFragment == null) {
                    mOneFragment = new OneFragment();
                    transaction.add(R.id.frame, mOneFragment);
                } else {
                    //如果微信对应的Fragment已经实例化,则直接显示出来
                    transaction.show(mOneFragment);
                }
                break;
            case 1:
                reImgSelect();
                mTwoImg.setSelected(true);
                if (mTwoFragment == null) {
                    mTwoFragment = new TwoFragment();
                    transaction.add(R.id.frame, mTwoFragment);
                } else {
                    //如果微信对应的Fragment已经实例化,则直接显示出来
                    transaction.show(mTwoFragment);
                }
                break;
            case 2:
                reImgSelect();
                mThreeImg.setSelected(true);
                if (mThreeFragment == null) {
                    mThreeFragment = new ThreeFragment();
                    transaction.add(R.id.frame, mThreeFragment);
                } else {
                    //如果微信对应的Fragment已经实例化,则直接显示出来
                    transaction.show(mThreeFragment);
                }
                break;
        }
        transaction.commit();
    }

    //将四个的Fragment隐藏
    private void hideFragments(FragmentTransaction transaction) {
        if (mOneFragment != null) {
            transaction.hide(mOneFragment);
        }
        if (mTwoFragment != null) {
            transaction.hide(mTwoFragment);
        }
        if (mThreeFragment != null) {
            transaction.hide(mThreeFragment);
        }
    }

    //初始化底部菜单选择状态
    private void reImgSelect(){
        mOneImg.setSelected(false);
        mTwoImg.setSelected(false);
        mThreeImg.setSelected(false);
    }
}

主页面布局:




    

    

    

    

        
            
        
        
            
        
        
            
        

    

注:底部的图片选中和未选中状态切换变化


    
    

在代码中设置图片.setSelected(true);进行选中或.setSelected(false);未选中

 

下载地址:https://download.csdn.net/download/lanrenxiaowen/10736674

你可能感兴趣的:(自用)