Fragment
- Fragment表现为Activity中UI的一部分
- Fragment有自己的生命周期
- Fragment总是必须被嵌入到一个Activity中
- Fragment的生命周期直接受到宿主Activity的生命周期的影响
Fragment的作用以及使用意义
- 作用:可以实现对屏幕分区,对UI控件实现分组,模块化管理
- 意义:实现了Activity的组件分离
Fragment的生命周期
Fragment对于在平板以及手机上APP的构造
Fragment使用用例
效果图
- 代码文件
主窗口
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
左边标题Fragment
public class FragmenTitle extends Fragment {
private static final String TAG ="FragmentTitel---->>>" ;
private ListView titel;
private List<String> title_list;
private int[] pid={
R.drawable.c1,
R.drawable.c2,
R.drawable.c3,
R.drawable.c4,
R.drawable.c5,
R.drawable.c6,
R.drawable.c7,
R.drawable.c8,
R.drawable.c9,
R.drawable.c10,
R.drawable.c11,
R.drawable.c12
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
//获取左边界面
View view = inflater.inflate(R.layout.fragmen_title, container);
//获取title界面
titel = (ListView) view.findViewById(R.id.titel);
initData();//初始化数据
//构造适配器
titel.setAdapter(new MyAdapter());
//设置点击
titel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Activity activity = getActivity();
//获取fragmen管理器
FragmentManager fragmentManager = activity.getFragmentManager();
//获取pic的fragmen
final FragmenPic pic_main = (FragmenPic) fragmentManager.findFragmentById(R.id.pic_main);
String t=title_list.get(position);
//调用FragmenPic中开放的方法用于修改pic
pic_main.set_title(t);
pic_main.set_pic( pid[position%12]);
}
});
Log.i(TAG,"OK! Title is Create!");
return view;
}
private void initData() {
title_list = new ArrayList<>();
for (int i=0;i<30;i++){
title_list.add("图片一:"+i);
}
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return title_list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView =null;
if (convertView==null){
textView=new TextView(parent.getContext());
}else {
textView= (TextView) convertView;
}
textView.setText(title_list.get(position));
textView.setTextSize(20);//text空间大小
textView.setBackgroundColor(Color.MAGENTA);
return textView;
}
}
}
右边详细Fragment
public class FragmenPic extends Fragment {
private TextView pic_title;
private ImageView pic;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
//获取两个子控件
View view = inflater.inflate(R.layout.fragmen_pic,container);
pic_title = (TextView) view.findViewById(R.id.pic_title);
pic = (ImageView) view.findViewById(R.id.pic);
return view;
}
//提供两个开放方法用于左边fragmen控件点击调用
//修改文字标题
public void set_title(String t){
pic_title.setText(t);
}
//修改图片信息
public void set_pic(int pid ){
pic.setImageResource(pid);
}
}
- 布局文件
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"
tools:context="com.works.day12_fragment.MainActivity"
>
<fragment
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:name="com.works.day12_fragment.FragmenTitle"
android:id="@+id/title_main"
android:layout_weight="3"
></fragment>
<fragment
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:name="com.works.day12_fragment.FragmenPic"
android:id="@+id/pic_main"
android:layout_weight="1.5"
></fragment>
</LinearLayout>
fragmen_pic.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pic_title"
android:text="图片一"
android:textSize="30sp"
android:gravity="center"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/c1"
android:id="@+id/pic"
/>
</LinearLayout>
fragmen_title.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/titel"
></ListView>
</LinearLayout>