android studio 导入包分不分动态静态,详解Android studio 动态fragment的用法

fragment的使用时Android的基础,它有两种用法,第一个就是静态的fragment。第二个则是动态的fragment。

静态fragment直接在layout创建你想要的fragment的XML的文件,然后在你的Java包里面创建对应fragment的class文件

布局代码如下所示

e8682d52075d14117811bb3989f85a8a.png

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="欢迎来到广西!"/>

dcc65f2c71fa99283cf42e451f00e8f2.png

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="去广西"

android:id="@+id/bt_anjian1"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="去广东"

android:id="@+id/bt_anjian2"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

android:id="@+id/ll_rongqi"

android:layout_weight="9">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/fragment_1"/>

*这里需要注意一下,如果你不给fragment加个id,那你运行app的时候将会发生闪退现象。

17098436e009542a1a854445a893c7dc.png

package com.example.anyone_fragment_2;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import androidx.annotation.NonNull;

import androidx.annotation.Nullable;

import androidx.fragment.app.Fragment;

public class Fragment_1 extends Fragment {

@Nullable

@Override

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View view=inflater.inflate(R.layout.fragment_1,container,false);

return view;

}

}

这样静态fragment算是弄好了,但是这次我们主要讨论动态fragment的用法

首先,我们先在activity_main中写下如下代码

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="去广西"

android:id="@+id/bt_anjian1"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="去广东"

android:id="@+id/bt_anjian2"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

android:id="@+id/ll_rongqi"

android:layout_weight="9">

布局效果图是这样的

7530361010cc284d89ff2bc21f79f793.png

这里fragment的XML文件和开头所说的静态fragment的那个XML文件的写法是一样的

同理,fragment对应的class文件也是相同的。

package com.example.anyone_fragment_2;

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 {//有abstract就闪退

private Button bt_anjian1,bt_anjian2;

private Fragment Fragment_1,Fragmentnow,Fragment_2;

private FragmentManager fragmentManager;

private FragmentTransaction fragmentTransaction;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

chushihua();

shilihua();

}

private void chushihua()

{

bt_anjian1=findViewById(R.id.bt_anjian1);

bt_anjian2=findViewById(R.id.bt_anjian2);

bt_anjian1.setOnClickListener(this);

bt_anjian2.setOnClickListener(this);

}

private void shilihua(){

//用于实例化fragment

Fragment_1=new Fragment_1();

Fragment_2=new Fragment_2();

Fragmentnow=Fragment_1;

fragmentManager=getSupportFragmentManager();

fragmentTransaction=fragmentManager.beginTransaction();

//38:只要你要对fragment进行操作就少不了这句 原来的写法是FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();

fragmentTransaction.add(R.id.ll_rongqi,Fragment_1).commit();

}

public void onClick(View vv)

{

fragmentTransaction=fragmentManager.beginTransaction();

switch (vv.getId())

{

case R.id.bt_anjian1:if (Fragment_1.isAdded())

{

fragmentTransaction.hide(Fragmentnow).show(Fragment_1).commit();

}

else

{

fragmentTransaction.hide(Fragmentnow).add(R.id.ll_rongqi,Fragment_1).commit();

}

Fragmentnow=Fragment_1;

break;

case R.id.bt_anjian2:if(Fragment_2.isAdded())

{

fragmentTransaction.hide(Fragmentnow).show(Fragment_2).commit();

}

else

{

fragmentTransaction.hide(Fragmentnow).add(R.id.ll_rongqi,Fragment_2).commit();

}

Fragmentnow=Fragment_2;

break;

}

}

}

下面来分析一些地方

初始化功能函数

private void chushihua()

{

bt_anjian1=findViewById(R.id.bt_anjian1);

bt_anjian2=findViewById(R.id.bt_anjian2);

bt_anjian1.setOnClickListener(this);

bt_anjian2.setOnClickListener(this);

}

这样写的目的是让代码可读性更好,不至于很混乱。

其次就是实例化我们所写的fragment功能函数

private void shilihua(){

//用于实例化fragment

Fragment_1=new Fragment_1();

Fragment_2=new Fragment_2();

Fragmentnow=Fragment_1;

fragmentManager=getSupportFragmentManager();

fragmentTransaction=fragmentManager.beginTransaction();

//38:只要你要对fragment进行操作就少不了这句 原来的写法是FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();

fragmentTransaction.add(R.id.ll_rongqi,Fragment_1).commit();

}

其中的

FragmentManager fragmentManager;

这个是fragment和activity交互所要用到的。

fragmentManager=getSupportFragmentManager();

固定写法。

private FragmentTransaction fragmentTransaction;

fragmentTransaction=fragmentManager.beginTransaction();

是调动fragment操作的API,也必不可少。

fragmentTransaction.add(R.id.ll_rongqi,Fragment_1).commit();

是添加fragment所用的语句,在这里就相当于是初始化吧。add(容器id,碎片对象),commit则是提交。

public void onClick(View vv)

{

fragmentTransaction=fragmentManager.beginTransaction();

switch (vv.getId())

{

case R.id.bt_anjian1:if (Fragment_1.isAdded())

{

fragmentTransaction.hide(Fragmentnow).show(Fragment_1).commit();

}

else

{

fragmentTransaction.hide(Fragmentnow).add(R.id.ll_rongqi,Fragment_1).commit();

}

Fragmentnow=Fragment_1;

break;

case R.id.bt_anjian2:if(Fragment_2.isAdded())

{

fragmentTransaction.hide(Fragmentnow).show(Fragment_2).commit();

}

else

{

fragmentTransaction.hide(Fragmentnow).add(R.id.ll_rongqi,Fragment_2).commit();

}

Fragmentnow=Fragment_2;

break;

}

}

这里的onClick的名字是不能改变的,否则你button没办法触发。

用传来的形参View vv来获取到我们所点击的按钮来判断操作。

思想就是,如果Fragment存在,则只需要把它展示出来即可。isAdded嘛,ed过去式,那就是代表存在过咯。

若是没有,则添加就好。

好了,就到这吧,有错误的话希望能指出来,大家一起共同进步!

到此这篇关于详解Android studio 动态fragment的用法的文章就介绍到这了,更多相关Android studio fragment用法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值