一:Fragment的概念
1:Fragment本身的中文意思是“碎片”,可以将Activity拆分成几个完全独立封装的可重用的组件,每个组件有 自己的生命周期和ui布局。
二:Fragment的静态加载
布局文件
<?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.example.myapplication.MainActivity">
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.example.myapplication.HelloFragment"
android:id="@+id/hello_fragment"/>
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/list_fragment"
android:name="com.example.myapplication.ListFragment"/>
</LinearLayout>
结果
三:Fragment的动态加载
不需要在主布局文件中去声明fragment的,而是直接在java代码中去添加
FragmentTransaction方法:
Fragment事物对象,对Fragment进行添加(add)、移除(remove)、替换(replace)、提交(commit)等操作的对象。
viewpager页码的滑动
主要代码:
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
ViewPager+Fragment实现页卡滑动
主要步骤:
在建完需要的Fragment之后,新建一个适配器Adapter,继承FragmentPagerAdapter,主要代码如下:
List<Fragment> fragmentList;
public ViewpagerAdapter(FragmentManager fm,List<Fragment> fragmentList) {
super(fm);
this.fragmentList=fragmentList;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
Activity类:
friend = findViewById(R.id.friend);
my = findViewById(R.id.my);
//绑定id到点击事件
cont.setOnClickListener(this);
friend.setOnClickListener(this);
my.setOnClickListener(this);
vp = findViewById(R.id.vp);
cont_view = findViewById(R.id.cont_view);
friend_view = findViewById(R.id.friend_view);
my_view = findViewById(R.id.my_view);
contFragment = new ContFragment();
friendFragment = new FriendFragment();
myFragment = new MyFragment();
fragmentList.add(contFragment);
fragmentList.add(friendFragment);
fragmentList.add(myFragment);
ViewpagerAdapter adapter = new ViewpagerAdapter(getSupportFragmentManager(), fragmentList);
vp.setAdapter(adapter);
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
cont_view.setBackgroundColor(Color.GRAY);
friend_view.setBackgroundColor(Color.GRAY);
my_view.setBackgroundColor(Color.GRAY);
switch (position) {
case 0:
cont_view.setBackgroundColor(Color.BLUE);
break;
case 1:
friend_view.setBackgroundColor(Color.BLUE);
break;
case 2:
my_view.setBackgroundColor(Color.BLUE);
break;
default:
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.cont:
//点中哪个页卡就到哪个页面
vp.setCurrentItem(0);
break;
case R.id.friend:
vp.setCurrentItem(1);
break;
case R.id.my:
vp.setCurrentItem(2);
break;
default:
break;
}
}
}
这样就实现了页卡的滑动和点击。
Fragment的生命周期
Fragment必须是依存于Activity而存在的。
因此,Activity的生命周期会直接影响到Fragment的生命周期。
onAttach()
onCreate()
onCreateView()
onActivityCreated()
onStart()
onResume()
onPause()
onStop()
onDestroyView()
onDestroy()
onDetach()
代码演示:
public class FriendFragment extends Fragment {
private String TAG="FriendFragment";
public FriendFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.d(TAG, "onAttach:********* ");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate: *********");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Log.d(TAG, "onCreateView: *********");
return inflater.inflate(R.layout.fragment_friend, container, false);
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart: *********");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume: *********");
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "onPause: *********");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG, "onDestroyView: *********");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy:*********");
}
@Override
public void onDetach() {
super.onDetach();
Log.d(TAG, "onDetach: *********");
}
}
Fragment与Activity的通信
可以在Fragment上控制Activity
假如在Fragment的button可以在Activity的Textview上出现标题
主要代码
Activity的XML:
<TextView
android:id="@+id/title_tv"
android:layout_width="match_parent"
android:layout_height="50dp" />
Activity代码:
将想要的内容(title)传至Fragment
public void modifyTitie(String title){
title1.setText(title);
Fragment的XML:
<Button
android:id="@+id/one_bt"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="我是张国庆" />
Fragment代码:
View view=inflater.inflate(R.layout.fragment_one,container,false);
button=view.findViewById(R.id.one_bt);
button.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
Main5Activity main5Activity= (Main5Activity) getActivity();
main5Activity.modifyTitie("我是张国庆");
}
实现Activity传值Fragment
类似Activity之间的通信intent,这里用Bundle
Activity代码:
Bundle bundle=new Bundle();
bundle.putString("name","我是满少爷");
oneFragment.setArguments(bundle);
Fragment代码:
Bundle bundle = getArguments();
String name = bundle.getString("name");
button.setText(name);