首先,什么是Fragment:
Fragment也可以叫为“片段”,它可以表示Activity中的行为或用户界面部分。我们可以在一个Activity中用多个Fragment组合来构建多窗格的UI,以及在多个Activity中重复使用某个Fragment。它有自己的生命周期,能接受自己的输入,并且可以在 Activity 运行时添加或删除Fragment(有点像在不同 Activity 中重复使用的“子 Activity”)。
例如:
这里对回调方法做一个简要的描述
onAttach()在Fragment对象添加到Activity中时调用
onCreate() 创建Fragment对象时调用
onCreateView() 在Fragment绘制用户界面时调用
onActivityCreated()所在Activity和Fragment的UI界面创建时调用。
onStart()在任何UI变化时,Fragment开始变为可视状态时调用
onResume()在Fragment开始变为运行状态时调用
onPause()在Fragment运行状态结束线程挂起所在Activity不再是前台界面时调用
onSaveInstanceState() 在Fragment运行状态结束,保存UI状态时调用时
onStop()在可见状态结束时调用
onDestroyView() 在Fragment视图被删除时调用
onDestroy() 在Fragment生命周期结束时调用
onDetach() 当Fragment被所在Activity删除时调用
好了有了这些知识我们正式开始写
这是效果图
静态添加的Fragment
首先写两个fragment.xml
fragment_tiny_show:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView 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:scrollbars="none"
tools:context=".TinyShowFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/image1"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:scaleType="centerCrop"
android:src="@drawable/p1"/>
<ImageView
android:id="@+id/image2"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:scaleType="centerCrop"
android:src="@drawable/p2"/>
<ImageView
android:id="@+id/image3"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:scaleType="centerCrop"
android:src="@drawable/p3"/>
<ImageView
android:id="@+id/image4"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:scaleType="centerCrop"
android:src="@drawable/p4"/>
<ImageView
android:id="@+id/image5"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:scaleType="centerCrop"
android:src="@drawable/p5"/>
<ImageView
android:id="@+id/image6"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:scaleType="centerCrop"
android:src="@drawable/p6"/>
<ImageView
android:id="@+id/image7"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:scaleType="centerCrop"
android:src="@drawable/p7"/>
<ImageView
android:id="@+id/image8"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:scaleType="centerCrop"
android:src="@drawable/p8"/>
</LinearLayout>
</HorizontalScrollView>
fragment_big_show
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context=".BigShowFragment">
<!-- TODO: Update blank fragment layout -->
<ImageView
android:id="@+id/ImageShow"
android:layout_width="match_parent"
android:layout_height="500dp"
android:scaleType="centerCrop"
android:src="@drawable/p1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="25dp"
android:text="this is fragment1"/>
</LinearLayout>
将两个fragment静态添加到activity_main.xml中
<?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">
<fragment
android:id="@+id/fragment1"
android:name="com.example.staticfragment.BigShowFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
tools:layout="@layout/fragment_big_show"/>
<fragment
android:id="@+id/fragment2"
android:name="com.example.staticfragment.TinyShowFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
tools:layout="@layout/fragment_tiny_show"/>
</LinearLayout>
在TinyShowFragment中onActivityCreated添加点击事件处理
public class TinyShowFragment extends Fragment {
int imgId[]={R.id.image1,R.id.image2,R.id.image3,R.id.image4,R.id.image5,R.id.image6,R.id.image7,R.id.image8};
ImageView tinyimage;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final ImageView imageshow=(ImageView)getActivity().findViewById(R.id.ImageShow);
for(int i=0;i<imgId.length;i++)
{
tinyimage=(ImageView)getView().findViewById(imgId[i]);
if(tinyimage!=null)
{
tinyimage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView view=(ImageView)v;
imageshow.setImageDrawable(view.getDrawable());
}
});
}
}
}
}
这样静态的Fragment就成功了!
动态添加的Fragment
基于接口的Fragment通信
(有点事,过段时间再更新)