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

Android Fragment实现底部通知栏

程序员文章站 2022-04-09 16:37:27
android fragment实现底部通知栏,供大家参考,具体内容如下截图如下:1. 第一步先要创建fragment(动态注册)然后将两个勾选取消掉(还有一种是自己手动创建)会自动生成相对应的lay...

android fragment实现底部通知栏,供大家参考,具体内容如下

截图如下:

Android Fragment实现底部通知栏

1. 第一步先要创建fragment(动态注册)

Android Fragment实现底部通知栏

然后将两个勾选取消掉(还有一种是自己手动创建)
会自动生成相对应的layout布局,剩下的要根据自己的需求了

2.在activity的布局里写好四个按钮

这里不是重点…

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  >
<linearlayout
  android:id="@+id/ll_content_part"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  >
</linearlayout>
<linearlayout
  android:id="@+id/ll_function"
  android:layout_alignparentbottom="true"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

<button
  android:id="@+id/btn_msg_list"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:textsize="13dp"
  android:onclick="click"
  android:text="msg"/>
  <button
    android:id="@+id/btn_contact"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textsize="13dp"
    android:onclick="click"
    android:text="contact"/>
  <button
    android:id="@+id/btn_disc"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textsize="13dp"
    android:onclick="click"
    android:text="disc"/>
  <button
    android:id="@+id/btn_me"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textsize="13dp"
    android:onclick="click"
    android:text="me"/>
</linearlayout>
</relativelayout>

3.activity的代码

其中定义了四个整型常量记录了四个按钮的状态,还有一个当前状态,进而判断当前点击按钮的状态,点击切换文字颜色和图标
每次判断四个fragment的引用是否为空,不为空就不需要每次在new一遍fragment
replace每次都会重新初始化fragment,它是先remove掉相同id的fragment,再add当前fragment。
add不会回每次都初始化fragment,一般配合hide()和show()方法

public class mainactivity extends appcompatactivity {

  private button btn_contact;
  private button btn_disc;
  private button btn_me;
  private button btn_msg_list;
  private fragmentmanager fragmentmanager;
  private msglistfragment msglistfragment;
  private contactfragment contactfragment;
  private discoveryfragment discoveryfragment;
  private mefragment mefragment;

  private final int state_msg =1;
  private final int state_con =2;
  private final int state_dis =3;
  private final int state_me =4;
  private int currentstate;

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    initview();
    initdata();
    chagebuttoncolor(currentstate,state_msg);
  }

  private void initdata() {
    fragmentmanager = getfragmentmanager();
    switchfragment(this.state_msg);
  }

  private void initview() {
    btn_contact = (button) findviewbyid(r.id.btn_contact);
    btn_disc = (button) findviewbyid(r.id.btn_disc);
    btn_me = (button) findviewbyid(r.id.btn_me);
    btn_msg_list = (button) findviewbyid(r.id.btn_msg_list);
  }

  public void click(view view) {

    switch (view.getid()){
      case r.id.btn_msg_list:

        switchfragment(this.state_msg);
        break;
      case r.id.btn_contact:
        switchfragment(this.state_con);
        break;

      case r.id.btn_disc:
        switchfragment(this.state_dis);
        break;
      case r.id.btn_me:
        switchfragment(this.state_me);
        break;
    }
  }

  private void switchfragment(int tostate) {

    if (tostate == currentstate)
      return;
    chagebuttoncolor(currentstate,tostate);
    this.currentstate=tostate;

    fragmenttransaction transaction = this.fragmentmanager.begintransaction();

    fragment fragment=msglistfragment;
    switch (tostate){
      case state_dis:
        if (discoveryfragment == null)
          discoveryfragment= new discoveryfragment();
        fragment= discoveryfragment;
        break;
      case state_me:
        if (mefragment == null)
          mefragment= new mefragment();
        fragment= mefragment;
        break;
      case state_con:
        if (contactfragment == null)
          contactfragment= new contactfragment();
        fragment= contactfragment;
        break;
      case state_msg:
        if (msglistfragment == null)
          msglistfragment= new msglistfragment();
        fragment= msglistfragment;
        break;
    }
    transaction.replace(r.id.ll_content_part,fragment);
    transaction.commit();
  }

  private void chagebuttoncolor(int currentstate,int tostate){

    switch (currentstate){
      case state_dis:
        this.btn_disc.settextcolor(color.black);
        break;
        case state_me:
          this.btn_me.settextcolor(color.black);
        break;
      case state_con:
        this.btn_contact.settextcolor(color.black);
        break;
      case state_msg:
        this.btn_msg_list.settextcolor(color.black);
        break;
    }
    switch (tostate){
      case state_dis:
        this.btn_disc.settextcolor(color.green);
        break;
      case state_me:
        this.btn_me.settextcolor(color.green);
        break;
      case state_con:
        this.btn_contact.settextcolor(color.green);
        break;
      case state_msg:
        this.btn_msg_list.settextcolor(color.green);
        break;
    }
  }
}

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