欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android实现点击按钮切换不同的fragment布局

程序员文章站 2022-11-08 14:31:21
本文实例为大家分享了android点击按钮切换不同布局的具体代码,供大家参考,具体内容如下 先上效果图: 如图所示,实现点击下面的按钮切换不同的fragment布局...

本文实例为大家分享了android点击按钮切换不同布局的具体代码,供大家参考,具体内容如下

先上效果图:

android实现点击按钮切换不同的fragment布局

如图所示,实现点击下面的按钮切换不同的fragment布局;

不说了,先上主mainactivity代码:

mainactivity.java:

package com.example.xh.twostylefragment;
import android.content.intent;
import android.os.bundle;
import android.support.v4.app.fragmenttransaction;
import android.support.v7.app.appcompatactivity;
import android.view.view;
import android.widget.button;
 
public class mainactivity extends appcompatactivity implements view.onclicklistener{
  //定义5个fragment
  private myfragment f1;
  private myfragment2 f2;
  private myfragment3 f3;
  private myfragment4 f4;
  private myfragment5 f5;
  //定义底部5个按钮
  private button foot1;
  private button foot2;
  private button foot3;
  private button foot4;
  private button foot5;
  int i = 0;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
 
    foot1 = (button) findviewbyid(r.id.btn1);//注册按钮
    foot2 = (button) findviewbyid(r.id.btn2);
    foot3 = (button) findviewbyid(r.id.btn3);
    foot4 = (button) findviewbyid(r.id.btn4);
    foot5 = (button) findviewbyid(r.id.btn5);
    foot1.setonclicklistener(this);//对按钮设置监听
    foot2.setonclicklistener(this);
    foot3.setonclicklistener(this);
    foot4.setonclicklistener(this);
    foot5.setonclicklistener(this);
    //第一次初始化首页默认显示第一个fragment
    initfragment1();
  }
 
  //显示第一个fragment
  private void initfragment1(){
    //开启事务,fragment的控制是由事务来实现的
    fragmenttransaction transaction = getsupportfragmentmanager().begintransaction();
 
    //第一种方式(add),初始化fragment并添加到事务中,如果为null就new一个
    if(f1 == null){
      f1 = new myfragment();
      transaction.add(r.id.main_frame_layout, f1);
    }
    //隐藏所有fragment
    hidefragment(transaction);
    //显示需要显示的fragment
    transaction.show(f1);
 
    //第二种方式(replace),初始化fragment
//    if(f1 == null){
//      f1 = new myfragment("首页");
//    }
//    transaction.replace(r.id.main_frame_layout, f1);
 
    //提交事务
    transaction.commit();
  }
 
  //显示第二个fragment
  private void initfragment2(){
    fragmenttransaction transaction = getsupportfragmentmanager().begintransaction();
 
    if(f2 == null){
      f2 = new myfragment2();
      transaction.add(r.id.main_frame_layout,f2);
    }
    hidefragment(transaction);
    transaction.show(f2);
 
//    if(f2 == null) {
//      f2 = new myfragment("分类");
//    }
//    transaction.replace(r.id.main_frame_layout, f2);
 
    transaction.commit();
  }
 
  //显示第三个fragment
  private void initfragment3(){
    fragmenttransaction transaction = getsupportfragmentmanager().begintransaction();
 
    if(f3 == null){
      f3 = new myfragment3();
      transaction.add(r.id.main_frame_layout,f3);
    }
    hidefragment(transaction);
    transaction.show(f3);
 
//    if(f3 == null) {
//      f3 = new myfragment("发现");
//    }
//    transaction.replace(r.id.main_frame_layout, f3);
 
    transaction.commit();
  }
  private void initfragment4(){
    fragmenttransaction transaction = getsupportfragmentmanager().begintransaction();
 
    if(f4 == null){
      f4 = new myfragment4();
      transaction.add(r.id.main_frame_layout,f4);
    }
    hidefragment(transaction);
    transaction.show(f4);
 
//    if(f4 == null) {
//      f4 = new myfragment("购物车");
//    }
//    transaction.replace(r.id.main_frame_layout, f4);
 
    transaction.commit();
  }
  private void initfragment5(){
    fragmenttransaction transaction = getsupportfragmentmanager().begintransaction();
 
    if(f5 == null){
      f5 = new myfragment5();
      transaction.add(r.id.main_frame_layout,f5);
    }
    hidefragment(transaction);
    transaction.show(f5);
 
//    if(f5 == null) {
//      f5 = new myfragment("我的);
//    }
//    transaction.replace(r.id.main_frame_layout, f5);
 
    transaction.commit();
  }
 
  //隐藏所有的fragment
  private void hidefragment(fragmenttransaction transaction){
    if(f1 != null){
      transaction.hide(f1);
    }
    if(f2 != null){
      transaction.hide(f2);
    }
    if(f3 != null){
      transaction.hide(f3);
    }
    if(f4 != null){
      transaction.hide(f4);
    }
    if(f5 != null){
      transaction.hide(f5);
    }
  }
 
  @override
  public void onclick(view v) {//点击哪个按钮就显示哪个fragment;
    if(v == foot1){
      initfragment1();
    }else if(v == foot2){
      initfragment2();
    }else if(v == foot3){
      initfragment3();
    }else if(v == foot4){
      initfragment4();
    }else if(v == foot5){
      initfragment5();
    }
  }
}

大家需要创建5个fragment,还有对应的xml文件,这里我给大家展示我创建的myfragment4.java:

package com.example.xh.twostylefragment;
 
    import android.os.bundle;
    import android.support.v4.app.fragment;
    import android.view.layoutinflater;
    import android.view.view;
    import android.view.viewgroup;
 
/**
 * created by administrator on 2016/7/8.
 */
public class myfragment4 extends fragment {
  @override
  public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {
    view view = layoutinflater.from(getactivity()).inflate(r.layout.shoppingcar,container,false);//用view保存shoppingcar.xml布局,大家可以
    return view;                                         //自己创建一个xml.
  }
 
}

差不多就是这样的了,有问题大家可以提出来。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。