java fragment_初步认识Fragment 之一 & 编写简单的fragment代码
今天初步学习到了Fragment ,如果说activity是一堵墙, 那么它类似于一个便利贴,能将内容写在上面粘贴到任意一面墙上, 而不需要每一堵墙上都写满相同的内容 ,下面我们来了解 怎样编写 Fragment 以及 动静态加载 传参 的代码Fragment 之 静态传参第一步:创建一个 FragmentA.java 文件, 编写如下代码packagecom.lym.week12.frag...
今天初步学习到了Fragment ,如果说activity是一堵墙, 那么它类似于一个便利贴,能将内容写在上面粘贴到任意一面墙上, 而不需要每一堵墙上都写满相同的内容 ,
下面我们来了解 怎样编写 Fragment 以及 动静态加载 传参 的代码
Fragment 之 静态传参
第一步:创建一个 FragmentA.java 文件, 编写如下代码
packagecom.lym.week12.fragment;importandroid.os.Bundle;importandroid.support.annotation.Nullable;importandroid.support.v4.app.Fragment;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importcom.lym.week12.R;public class FragmentA extendsFragment{//初始化布局和控件
@Nullable
@OverridepublicView onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {/*** LayoutInflater 布局加载器*/View view= inflater.inflate(R.layout.fragment_a,null);
View textView=view.findViewById(R.id.text_view);returnview;
}
}
第二步:在layout下编写上诉代码中需要用到的 fragment.fragment_a.xml 文件
第三步:编写 activity_main.xml 文件
第四步:编写 MainActivity.java 文件
packagecom.lym.week12;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;public class ActivityWechat extendsAppCompatActivity {
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Fragment 之 动态传参
第一步:编写一个 fragmentB.java 文件
packagecom.lym.week12.fragment;importandroid.os.Bundle;importandroid.support.annotation.Nullable;importandroid.support.v4.app.Fragment;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importcom.lym.week12.R;public class FragmentB extendsFragment{
@Nullable
@OverridepublicView onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_b, null);returnview;
}
}
第二步:在layout中编写上述代码中用到的 fragment_b.xml 文件
第三步:编写 activity_main.xml 文件
第四步:编写 MainActivity.java 文件
packagecom.lym.week12;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.widget.Button;importcom.lym.week12.fragment.FragmentB;importcom.lym.week12.fragment.FragmentC;public class MainActivity extendsAppCompatActivity {privateFragmentC fragmentC;
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadFragmentB();
}/*** 1.动态加载FragmentB*/
private voidloadFragmentB() {//1.获取fragment管理器
android.support.v4.app.FragmentManager manager =getSupportFragmentManager();//2.开启一个事务
android.support.v4.app.FragmentTransaction transaction =manager.beginTransaction();
FragmentB fragmentB= newFragmentB();//3.向容器中添加fragment
transaction.add(R.id.fragment_b,fragmentB);//4.提交事务
transaction.commit();
}
}
此时 Fragment 的 动态传参 和 静态传参 已经编写完成,也可以发现两者 代码上只有些许的不同
但是 动态传参中 还有一些小小的缺陷,如上面动态传参代码中的数据也都是 初始化加载得到的,
并不是我們自己传入其中的, 那么 我们 怎样 来实现 真正得 动态传参 呢?
话不多少,我们上代码
第一步:我们再次创建 一个 fragmentC.java 文件
packagecom.lym.week12.fragment;importandroid.os.Bundle;importandroid.support.annotation.Nullable;importandroid.support.v4.app.Fragment;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.TextView;importcom.lym.week12.R;public class FragmentC extendsFragment{privateTextView textView;
@Nullable
@OverridepublicView onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_c, null);
textView=view.findViewById(R.id.text_view);
Bundle bundle= getArguments(); //获取 MainActivity.java 发过来的参数
if (bundle!=null){ //如果bundle不为空
String data1 = bundle.getString("data"); //把得到data里的值赋给字符串data1
textView.setText(data1); //把data1传给fragment_c中TextView 的 text
}returnview;
}public voidsendData(String data){
textView.setText(data);
}
}
第二步,我们分别编写上述代码中 高亮部分 的两个文件 fragment_c.xml 与 MainActivity.java
fragment_c.xml
MainActivity.java
packagecom.lym.week12;importandroid.support.v4.app.FragmentManager;importandroid.support.v4.app.FragmentTransaction;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importcom.lym.week12.fragment.FragmentB;importcom.lym.week12.fragment.FragmentC;public class MainActivity extendsAppCompatActivity {privateFragmentC fragmentC;
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadFragmentC();
Button button=findViewById(R.id.btn);
button.setOnClickListener(newView.OnClickListener() {
@Overridepublic voidonClick(View v) {
fragmentC.sendData("打游戏");
}
});
}private voidloadFragmentC() {//1.获取fragment管理器
FragmentManager manager =getSupportFragmentManager();//2.开启一个事务
FragmentTransaction transaction =manager.beginTransaction();
fragmentC= newFragmentC();
Bundle bundle= newBundle();//传入参数
bundle.putString("data", "家(杨明飞)");
bundle.putInt("int", 1000);
fragmentC.setArguments(bundle);//3.向容器中添加fragment
transaction.add(R.id.fragment_c, fragmentC);//4.提交事务
transaction.commit();
}
}
第三步:编写activity_main.xml文件
到这里 Fragment 简单的 静态传参 和 动态传参 就 写好了!
如有不对,多多指正!
更多推荐

所有评论(0)