详解Android studio 动态fragment的用法

2020-10-10 0 911

fragment的使用时Android的基础,它有两种用法,第一个就是静态的fragment。第二个则是动态的fragment。
静态fragment直接在layout创建你想要的fragment的XML的文件,然后在你的Java包里面创建对应fragment的class文件
布局代码如下所示

详解Android studio 动态fragment的用法

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
 android:layout_width=\"match_parent\"
 android:layout_height=\"match_parent\"
 android:orientation=\"vertical\">
 <TextView
  android:layout_width=\"match_parent\"
  android:layout_height=\"wrap_content\"
  android:text=\"欢迎来到广西!\"/>

</LinearLayout>

详解Android studio 动态fragment的用法

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
 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\">
 <Button
  android:layout_width=\"wrap_content\"
  android:layout_height=\"wrap_content\"
  android:text=\"去广西\"
  android:id=\"@+id/bt_anjian1\"/>
 <Button
  android:layout_width=\"wrap_content\"
  android:layout_height=\"wrap_content\"
  android:text=\"去广东\"
  android:id=\"@+id/bt_anjian2\"/>
 <LinearLayout
  android:layout_width=\"match_parent\"
  android:layout_height=\"wrap_content\"
  android:orientation=\"vertical\"
  android:id=\"@+id/ll_rongqi\"
  android:layout_weight=\"9\">

 </LinearLayout>
 <fragment
  android:layout_width=\"wrap_content\"
  android:layout_height=\"wrap_content\"
  android:id=\"@+id/fragment_1\"/>

</LinearLayout>

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

详解Android studio 动态fragment的用法

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中写下如下代码

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
 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\">
 <Button
  android:layout_width=\"wrap_content\"
  android:layout_height=\"wrap_content\"
  android:text=\"去广西\"
  android:id=\"@+id/bt_anjian1\"/>
 <Button
  android:layout_width=\"wrap_content\"
  android:layout_height=\"wrap_content\"
  android:text=\"去广东\"
  android:id=\"@+id/bt_anjian2\"/>
 <LinearLayout
  android:layout_width=\"match_parent\"
  android:layout_height=\"wrap_content\"
  android:orientation=\"vertical\"
  android:id=\"@+id/ll_rongqi\"
  android:layout_weight=\"9\">

 </LinearLayout>

</LinearLayout>

布局效果图是这样的

详解Android studio 动态fragment的用法

这里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用法内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

遇见资源网 Android 详解Android studio 动态fragment的用法 http://www.ox520.com/23773.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务