1.静态加载fragment
package com.example.shinelon.diyfragment;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
//自定义一个类继承Fragment,继承这个android.app.Fragment,v4那个已经被淘汰,详情见菜鸟教程,及fragment的作用
public class Demo2 extends Fragment{
@Nullable
//重写onCreate方法
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
//用inflate加载布局页面,布局页面命名规则 fragment_xxxx
View view = inflater.inflate(R.layout.fragment2, null);
return view;
}
}
在activity_main布局中加载fragment
name属性值当前所要加载的fragment的全类名
标签全小写
<fragment
android:name="com.example.shinelon.diyfragment.Demo3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></fragment>
2.动态加载fragment(横竖屏切换加载不同的页面)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取手机屏幕
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
//获取fragment管理者
android.app.FragmentManager manager = getFragmentManager();
//管理者开启事务
// 开启一个事物,事物是执行一段逻辑,要么同时成功,要么同时失败
FragmentTransaction transaction = manager.beginTransaction();
if(height>width){
//加载之前的写好的页面
transaction.replace(R.id.LL,new Demo1());
}else {
transaction.replace(R.id.LL,new Demo2());
}
//提交一个事务,业务逻辑完成后一定要提交(commit)事务
transaction.commit();
}
}
activity_main中
<LinearLayout
android:id="@+id/LL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
3.Fragment与Activity的交互
左边的Button是一个fragment,右边的TextView在Activity上
fragment_layout
简单新建一个布局,上面只写了一个Button
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="button"/>
</LinearLayout>
Fragment
/和之前的操作一样,先创建一个类继承app.Fragment,用inflate加载刚才写的布局
public class Fragment extends android.app.Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, null);
//设置Button的监听,用匿名内部类的方法
// 这里的view应该相当于上下文对象,理解的不是很好
view.findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//用getActivity方法,得到Activity activity = getActivity();
//指定是MainActivity,然后进行强制转换
MainActivity m = (MainActivity) getActivity();
m.update("123456");
}
});
return view;
}
}
activity_main
主布局文件中,写了一个线性布局,用来放Fragment
<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="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/LL"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"></LinearLayout>
<TextView
android:id="@+id/tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hello Lenovo" />
</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv);
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.LL,new Fragment());
transaction.commit();
}
//将修改文字封装成一个方法,然后在Button的点击事件中,直接调用这个方法
public void update(String s){
tv.setText(s);
}
}
4.Fragment与Fragment之间的数据互传
效果与之前类似,不再做演示
Fragment2
//操作与上一个案例相同,布局文件也很简单,就是一个TextView
public class Fragment2 extends Fragment{
@Nullable
TextView tv;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2_layout, null);
tv = view.findViewById(R.id.tv);
return view;
}
public void setData(String s){
tv.setText(s);
}
}
Fragment1
public class Fragment1 extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1_layout, null);
view.findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity mactivity = (MainActivity) getActivity();
//这里用一个getFragmentManger方法,得到manger对象
FragmentManager manager = mactivity.getFragmentManager();
//用manger找到这个取名为"f2"的fragment
//和上一个案例不同之处
Fragment2 f2 = (Fragment2) manager.findFragmentByTag("f2");
f2.setData("123654");
}
});
return view;
}
}
activity_main
主布局文件中就放两个LineLayout
<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"
tools:context=".MainActivity"
android:orientation="horizontal">
<LinearLayout
//设置id,必须要设置orientation的方向
android:id="@+id/LL1"
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"></LinearLayout>
<LinearLayout
android:id="@+id/LL2"
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"></LinearLayout>
</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager f1 = getFragmentManager();
FragmentTransaction t1 = f1.beginTransaction();
//和上一个不同之处,在后面加了一个自取的fragment名字
//用于前面FragmentManager找到这个取名的fragment
t1.replace(R.id.LL1,new Fragment1(),"f1");
t1.commit();
FragmentManager f2 = getFragmentManager();
FragmentTransaction t2 = f2.beginTransaction();
t2.replace(R.id.LL2,new Fragment2(),"f2");
t2.commit();
}
}