Fragment的用法

XML文件

<FrameLayout
        android:id="@+id/ft1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/fistrl"
        android:layout_weight="1">


    </FrameLayout>


java文件

public class MainActivity extends FragmentActivity {

    private RelativeLayout bt_rl1,bt_rl2,bt_rl3,bt_rl4,top_rlt1;
    private ImageView bt_iv1,bt_iv2,bt_iv3,bt_iv4;
    private TextView bt_tv1,bt_tv2,bt_tv3,bt_tv4,main_tv,main_tvl,main_tvr;


    private FragmentHome home;
    private FragmentList listl;
    private FragmentMydoctor mydoctor;
    private FragmentMore more;
    FragmentTransaction transaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initBt();
        enabledFalse();
        show();
    }


    //初始化id
    public void initBt() {
        bt_rl1 = (RelativeLayout) findViewById(R.id.bt_rl1);
        bt_rl2 = (RelativeLayout) findViewById(R.id.bt_rl2);
        bt_rl3 = (RelativeLayout) findViewById(R.id.bt_rl3);
        bt_rl4 = (RelativeLayout) findViewById(R.id.bt_rl4);
        top_rlt1 = (RelativeLayout) findViewById(R.id.toprlt1);


        bt_iv1 = (ImageView) findViewById(R.id.bt_iv1);
        bt_iv2 = (ImageView) findViewById(R.id.bt_iv2);
        bt_iv3 = (ImageView) findViewById(R.id.bt_iv3);
        bt_iv4 = (ImageView) findViewById(R.id.bt_iv4);


        bt_tv1 = (TextView) findViewById(R.id.bt_tv1);
        bt_tv2 = (TextView) findViewById(R.id.bt_tv2);
        bt_tv3 = (TextView) findViewById(R.id.bt_tv3);
        bt_tv4 = (TextView) findViewById(R.id.bt_tv4);
        main_tv = (TextView) findViewById(R.id.main_tv);
        main_tvl = (TextView) findViewById(R.id.main_tvl);
        main_tvr = (TextView) findViewById(R.id.main_tvr);
    }
    public void show(){
        hideFragment();
        //管理器要用get方式获取
        FragmentManager manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        enabledFalse();
        bt_iv1.setEnabled(true);
        bt_tv1.setTextColor(Color.rgb(39,174,96));
        main_tv.setText("丁香医生");
        if (home == null) {
            home = new FragmentHome();
            transaction.replace(R.id.ft1, home);


        }else{
            transaction.replace(R.id.ft1, home);
        }
        transaction.commit();
    }
   public void ck1(View view){
       top_rlt1.setVisibility(View.VISIBLE);


       show();


   }
    public void ck2(View view){
        hideFragment();
        //管理器要用get方式获取
        FragmentManager manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        enabledFalse();
        top_rlt1.setVisibility(View.GONE);
        bt_iv2.setEnabled(true);
        bt_tv2.setTextColor(Color.rgb(39,174,96));
        main_tv.setText("健康科普");
        if (listl == null) {
            listl = new FragmentList();
            transaction.replace(R.id.ft1, listl);


        }else{
            transaction.replace(R.id.ft1, listl);
        }
        transaction.commit();


    }
    public void ck3(View view){
        hideFragment();
        //管理器要用get方式获取
        FragmentManager manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        enabledFalse();
        top_rlt1.setVisibility(View.GONE);
        bt_iv3.setEnabled(true);
        bt_tv3.setTextColor(Color.rgb(39,174,96));
        main_tv.setText("家人健康");
        main_tvl.setText("同步");
        main_tvr.setText("编辑");
        if (mydoctor == null) {
            mydoctor = new FragmentMydoctor();
            transaction.replace(R.id.ft1, mydoctor);


        }else{
            transaction.replace(R.id.ft1, mydoctor);
        }
        transaction.commit();
    }
    public void ck4(View view){
        hideFragment();
        //管理器要用get方式获取
        FragmentManager manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        enabledFalse();
        top_rlt1.setVisibility(View.GONE);
        bt_iv4.setEnabled(true);
        bt_tv4.setTextColor(Color.rgb(39,174,96));
        main_tv.setText("更多");
        if (more == null) {
            more = new FragmentMore();
            transaction.replace(R.id.ft1, more);


        }else{
            transaction.replace(R.id.ft1, more);
        }
        transaction.commit();
    }




    //设置一个隐藏所有Fragmnet的方法
    public void hideFragment() {
        if (home != null) {
            transaction.hide(home);
        }
        if (listl != null) {
            transaction.hide(listl);
        }
        if (mydoctor != null) {
            transaction.hide(mydoctor);
        }
        if (more != null) {
            transaction.hide(more);
        }
    }
    //初始化按键
    public void enabledFalse(){
        bt_iv1.setEnabled(false);
        bt_iv2.setEnabled(false);
        bt_iv3.setEnabled(false);
        bt_iv4.setEnabled(false);
        bt_tv1.setTextColor(Color.rgb(153,153,153));
        bt_tv2.setTextColor(Color.rgb(153,153,153));
        bt_tv3.setTextColor(Color.rgb(153,153,153));
        bt_tv4.setTextColor(Color.rgb(153,153,153));
        main_tv.setText("");
        main_tvl.setText("");
        main_tvr.setText("");
    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值