资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android中Fragment的静态注册和动态注册创建步骤-创新互联

本篇内容主要讲解“Android中Fragment的静态注册和动态注册创建步骤”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android中Fragment的静态注册和动态注册创建步骤”吧!

公司主营业务:网站设计制作、成都做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出崇礼免费做网站回馈大家。

一、fragment静态注册创建方法及步骤

1.创建一个StaticFragment.java文件继承Fragment类和一个static_fragment.xml文件完成fragment的布局。在StaticFragment.java中重载onCreateView(……)方法,通过调用inflater.inflate(……)方法并传入布局资源ID生成fragment的视图资源,并绑定static_fragment.xml中的相关组件然后实现其功能。实现代码如下:

static_fragment.xml
                
StaticFragment.java
package com.example.myapplication;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.EditText;import androidx.annotation.NonNull;import androidx.annotation.Nullable;import androidx.fragment.app.Fragment;public class StaticFragment extends Fragment {  private Button mBtnFm;  private EditText mEtFm;  @Nullable  @Override  public View onCreateView(@NonNull LayoutInflater inflater,               @Nullable ViewGroup container,               @Nullable Bundle savedInstanceState) {    //fragment的视图资源是直接通过调用inflater.inflate(……)方法并传入布局资源ID生成的。    View v = inflater.inflate(R.layout.static_fragment,                 container,false);    mEtFm = v.findViewById(R.id.et_fm);    mBtnFm = v.findViewById(R.id.btn_fm);    mBtnFm.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View v) {        mBtnFm.setText(mEtFm.getText().toString());      }    });    return v;  }}

2.在主布局activity_main.xml文件中绑定fragment布局文件。

实现代码如下:

activity_main.xml
            

注意:布局文件中加fragment节点,name属性必须填写完整的路径

MainActivity.java
package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity {  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);  }}

演示:

二、fragment动态注册创建方法及步骤

1.新建一个项目,创建2个Fragment继承类分别为MyFragment1.java和MyFragment2.java,然后创建2个布局文件分别为fragment1.xml和fragment2.xml.详细代码如下:

fragment1.xml
    
MyFragment1.java
package com.example.myapplication;import android.content.Context;import android.net.Uri;import android.os.Bundle;import androidx.fragment.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class MyFragment1 extends Fragment {  @Override  public View onCreateView(LayoutInflater inflater, ViewGroup container,               Bundle savedInstanceState) {    // Inflate the layout for this fragment    return inflater.inflate(R.layout.fragment1, container, false);  }}
fragment2.xml
    
MyFragment2.java
package com.example.myapplication;import android.os.Bundle;import androidx.fragment.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class MyFragment2 extends Fragment {  @Override  public View onCreateView(LayoutInflater inflater,               ViewGroup container,               Bundle savedInstanceState) {    // Inflate the layout for this fragment    return inflater.inflate(R.layout.fragment2, container, false);  }}

上述代码与静态创建的区别不大。

2.以代码的形式将fragment添加到activity需要在activity里直接调用FragmentManager。

FragmentManager fm = getSupportFragmentManager();

然后通过代码块:

FragmentTransaction ts = fm.beginTransaction();Fragment mfg1 = new MyFragment1();ts.add(R.id.fragment_container,mfg1);ts.commit();

提交一个fragment事务。其核心是ts.add(……)方法。

详细代码如下:

activity_main.xml:
                        

注意:fragment模块一般用FrameLayout布局承载


MainActivity.java

package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import androidx.fragment.app.Fragment;import androidx.fragment.app.FragmentManager;import androidx.fragment.app.FragmentTransaction;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener {  private Button mBtnDy1;  private Button mBtnDy2;  FragmentManager fm;  Fragment mfg1;  Fragment mfg2;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    fm = getSupportFragmentManager();    mBtnDy1 = findViewById(R.id.btn_dy1);    mBtnDy2 = findViewById(R.id.btn_dy2);    mBtnDy1.setOnClickListener(this);    mBtnDy2.setOnClickListener(this);  }  @Override  public void onClick(View v) {    clearSelection();//清除按钮状态    FragmentTransaction ts = fm.beginTransaction();    hideFragments(ts);    switch (v.getId()){      case R.id.btn_dy1:        mBtnDy1.setBackgroundColor(0xff0000ff);        if(mfg1 == null){          mfg1 = new MyFragment1();          ts.add(R.id.fragment_container,mfg1);        }else {          ts.show(mfg1);        }        break;      case R.id.btn_dy2:        mBtnDy2.setBackgroundColor(0xff0000ff);        if(mfg2 == null){          mfg2 = new MyFragment2();          ts.add(R.id.fragment_container,mfg2);        }else {          ts.show(mfg2);        }        break;        default:          break;    }    ts.commit();  }//  将所有的Fragment都置为隐藏状态。  private void hideFragments(FragmentTransaction transaction) {    if (mfg1 != null) {      transaction.hide(mfg1);    }    if (mfg2 != null) {      transaction.hide(mfg2);    }  }//   清除掉所有的选中状态。  private void clearSelection() {    mBtnDy1.setBackgroundColor(0xffffffff);    mBtnDy2.setBackgroundColor(0xffffffff);  }}

到此,相信大家对“Android中Fragment的静态注册和动态注册创建步骤”有了更深的了解,不妨来实际操作一番吧!这里是创新互联建站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


本文名称:Android中Fragment的静态注册和动态注册创建步骤-创新互联
文章URL:http://cdkjz.cn/article/dighed.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220