fragment事物跳转

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.bwei.czx.fragment.FragmentLawyer;
import com.bwei.czx.fragment.FragmentWeather;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button fondLawyer;
    private Button weather;
    private FrameLayout frameLayout;
    private FragmentManager fm;
    private List<Button> buttonList;
    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找到按钮控件
        fondLawyer = (Button) findViewById(R.id.fondLawyer);
        weather = (Button) findViewById(R.id.weather);
        weather.setOnClickListener(this);
        fondLawyer.setOnClickListener(this);
        //点击按钮添加到集合
        buttonList = new ArrayList<>();
        buttonList.add(weather);
        buttonList.add(fondLawyer);


        //找到fragment控件
        frameLayout = (FrameLayout) findViewById(R.id.frameLayout);

        //添加fragment布局
        fragments = new ArrayList<>();
        FragmentLawyer fragmentLawyer = new FragmentLawyer();
        FragmentWeather fragmentWeather = new FragmentWeather();
        fragments.add(fragmentWeather);
        fragments.add(fragmentLawyer);

        //得到事务管理器
        fm = getSupportFragmentManager();
        //开启事务
        FragmentTransaction ft = fm.beginTransaction();
        //把fragment添加到FragmentLayout里面
        ft.add(R.id.frameLayout,fragmentWeather);
        ft.add(R.id.frameLayout,fragmentLawyer);
        ft.hide(fragmentLawyer);
        //提交事务
        ft.commit();
    }

    @Override
    public void onClick(View v) {
        //实现点击按钮,页面跳转联动效果
        //开启事务
        FragmentTransaction ft = fm.beginTransaction();
        for (int i = 0;i < buttonList.size();i++){
            //得到按钮
            Button button = buttonList.get(i);
            if(button.getId() == v.getId()){
                button.setBackgroundColor(Color.RED);
                ft.show(fragments.get(i));
            }else{
                button.setBackgroundColor(Color.GRAY);
                ft.hide(fragments.get(i));
            }
        }
        //提交事务
        ft.commit();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值