大家好,今天和大家分享的是类似Activity的组件片段fragment,很大原因是fragment的功能和Activity很相似,能显示界面,和其他组件进行交互,但也有不同的地方,fragment不是四大组件一种,不需要在AndroidManifest注册,当需要用到多个Activity实现交互的时候,我们就可以用片段fragment充当Activity的功能,一个Activity就能显示多个界面,它的价值就体现在这里。恩。不多说,先上图。
简单的解释显示界面,我们点击添加按钮,有个onefragement片段出现并显示在MainActivity界面上,我们点击替换按钮,twofragment界面的画面会在onefragment上显示,替换掉,我们点击移除按钮,就会把twofragment界面移除掉,功能就这样实现的,下面用代码分析fragment的强大作用。
1.res/layout
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffcc"
android:orientation="vertical" >
//控件Fragment,设置id为content
<FrameLayout
android:id="@+id/content"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="add"
android:text="添加" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="replace"
android:text="替换" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="remove"
android:text="移除" />
</LinearLayout>
</LinearLayout>
one_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_pada" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageView1"
android:layout_alignLeft="@+id/imageView1"
android:text="oneFragment"
android:textSize="25sp" />
</RelativeLayout>
其他两个two_fragment.xml和three_fragment.xml的控件都和one_fragment都差不多,换了前景图片而已,这里就不上代码了。
2.src文件
OneFragment.java
/**
*好比一个Activity,用来显示界面和交互
*
*/
//继承父类Fragment
public class OneFragment extends Fragment {
private static final String TAG = "OneFragment";
private Context context;
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate");
//得到Activity
context=getActivity();
super.onCreate(savedInstanceState);
}
//创建视图对象
//返回的是视图片段
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, "onCreateView");
//参数一:资源layout,参数二:ViewGroup,参数三:是否为父类
return inflater.inflate(R.layout.one_fragment, container,false);
}
//创建Activity,当依赖的Activity创建成功后,回调该方法,对片段的界面控件做监听,交互
public void onActivityCreated(Bundle savedInstanceState) {
Log.i(TAG, "onActivityCreated");
//添加点击事件,对象为View,不是ImageView
View image = getActivity().findViewById(R.id.imageView1);
image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "我是OneFragment", Toast.LENGTH_SHORT).show();
}
});
super.onActivityCreated(savedInstanceState);
}
2.TwoFragment.java
//与第一个OneFragment实现代码差不多,oncreateView的资源layout不同
public class TwoFragment extends Fragment {
private static final String TAG = "TwoFragment";
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate");
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, "onCreateView");
return inflater.inflate(R.layout.two_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.i(TAG, "onActivityCreated");
// 添加点击事件
View image = getActivity().findViewById(R.id.imageView1);
image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "我是TwoFragment",
Toast.LENGTH_SHORT).show();
}
});
super.onActivityCreated(savedInstanceState);
}
3.ThreeFragment.java
public class ThreeFragment extends Fragment {
private static final String TAG = "ThreeFragment";
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate");
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, "onCreate");
return inflater.inflate(R.layout.three_fragment, container,false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.i(TAG, "onActivityCreated");
//添加点击事件
View image = getActivity().findViewById(R.id.imageView1);
image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "我是ThreeFragment", Toast.LENGTH_SHORT).show();
}
});
super.onActivityCreated(savedInstanceState);
}
4.MainActivity.java
public class MainActivity extends Activity {
//片段管理器
private FragmentManager fragmentManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化片段,得到片段管理器
fragmentManager = getFragmentManager();
}
//增加片段按钮事件
public void add(View v) {
//开启事务
FragmentTransaction beginTransaction = fragmentManager
.beginTransaction();
//创建OneFragment对象
OneFragment fragment = new OneFragment();
//把事务添加到OneFragment中
beginTransaction.add(R.id.content, fragment);
//把操作事务添加到返回栈中
//beginTransaction.addToBackStack(null);
//提交失误
beginTransaction.commit();
}
//替换按钮事件
public void replace(View v) {
//开始事务
FragmentTransaction ft = fragmentManager.beginTransaction();
TwoFragment fragment = new TwoFragment();
//替换,跳转
ft.replace(R.id.content, fragment);
//提交事务
ft.commit();
}
//删除片段按钮事件
public void remove(View v) {
FragmentTransaction ft = fragmentManager.beginTransaction();
//找到栈顶元素
Fragment findFragmentById = fragmentManager.findFragmentById(R.id.content);
//删除容器里的元素
ft.remove(findFragmentById);
//提交事务
ft.commit();
}
}
恩,大话片段就分享到这了,代码清晰简单,好理解,没什么难的地方,主要理解的是怎样创建Fragment片段,怎样在Activity界面中实现效果。好了,大家晚安。