Fragment的使用心得
什么是Fragment
Fragment再英文单词中的含义是碎片,安卓3.0开始使用,当时为了平板和手机适配同一个app问题,就是用Fragment解决的,Fragment的优点有俩点:
1.局部加载
使用Fragment可以更快的切换页面。
2.轻量化
Fragment可以被重复利用。
Fragment的静态加载
再Activity中直接调用Fragment,代码如下:
<fragment
android:id="@+id/call_fm"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.demo5.CallFragment"></fragment>
效果和View基本相同
Fragment的动态加载
提到动态加载,就必须提到FragmentTransaction(事物)和FragmentManage(管理),FragmentTransaction的主要功能有:
transaction.add()
往Activity中添加一个Fragment
transaction.remove()
从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈(回退栈后面会详细说),这个Fragment实例将会被销毁。
transaction.replace()
使用另一个Fragment替换当前的,实际上就是remove()然后add()的合体~
transatcion.commit()
提交一个事务
附上一段简单的代码
<?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">
<FrameLayout
android:id="@+id/mian_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="10dp"
android:layout_weight="1"></FrameLayout>
</LinearLayout>
<?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">
<FrameLayout
android:id="@+id/mian_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="10dp"
android:layout_weight="1"></FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="拨号" />
<Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:text="联系人" />
<Button
android:id="@+id/btn3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="短信" />
</LinearLayout>
</LinearLayout>
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn1;
private Button btn2;
private Button btn3;
private FragmentTransaction fragmentTransaction;
private FragmentManager fragmentManager;
private OneFragment oneFragment;
private TwoFragment twoFragment;
private ThreeFragment threeFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Binn();
}
private void Binn() {
btn1=findViewById(R.id.btn1);
btn2=findViewById(R.id.btn2);
btn3=findViewById(R.id.btn3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
fragmentManager=getFragmentManager();
}
@Override
public void onClick(View v) {
fragmentTransaction=fragmentManager.beginTransaction();
switch (v.getId()){
case R.id.btn1:
if (oneFragment==null){
oneFragment=new OneFragment();
}
fragmentTransaction.replace(R.id.mian_fragment,oneFragment);
break;
case R.id.btn2:
if (twoFragment==null){
twoFragment=new TwoFragment();
}
fragmentTransaction.replace(R.id.mian_fragment,twoFragment);
break;
case R.id.btn3:
if (threeFragment==null){
threeFragment=new ThreeFragment();
}
fragmentTransaction.replace(R.id.mian_fragment,threeFragment);
break;
default:
break;
}
fragmentTransaction.commit();
}
}
ViewPager+Fragment实现页卡滑动
要想实现这个效果,必须学会自定义Adapter(适配器);
首先再MainActivity创建一个class并且继承FragmentPagerAdapter
代码如下:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;
public class MyAdapter extends FragmentPagerAdapter {
List<Fragment> fragmentList=new ArrayList<>();
//添加Fragment数组至适配器
public MyAdapter(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();
//返回当前页卡的页数
}
}
然后是ViewPager的创建
<?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">
//创建一个ViewPager(v4包)
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
</LinearLayout>
下面是主函数
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private CallFragment callFragment;
private FriendFragment friendFragment;
private DuanxiFragment duanxiFragment;
private List<Fragment> fragmentList;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bind();
fragmentList.add(callFragment);
fragmentList.add(friendFragment);
fragmentList.add(duanxiFragment);
MyAdapter myAdapter=new MyAdapter(getSupportFragmentManager(),fragmentList);
//传递Fragment数组
viewPager.setAdapter(myAdapter);
//绑定适配器
}
private void Bind() {
viewPager=findViewById(R.id.viewpager);
friendFragment=new FriendFragment();
callFragment=new CallFragment();
duanxiFragment=new DuanxiFragment();
fragmentList=new ArrayList<>();
}
}
Fragment的生命周期
onAttach()
依附,即Fragment依附Activity
onCreate()
创建Fragment
onCreateView()
创建Fragment视图
onstart()
Fragment正在启动
onResume()
创建完成,实现用户交互
onPause()
Fragment正在暂停活动,与onResume相对
onStop()
Fragment即将停止,与onStart相对
onDestroyView()
Fragment的视图被移除,与onCreateView相对
onDestroy()
Fragment即将被消除,与onCreate相对
onDetach()
Fragment取消依附,与onAttach相对
预加载
用到ViewPager+Fragment,Fragment会进行预加载,例如你在ViewPager中添加了三个Fragment,当加载ViewPager中第一个Fragment时,它会默认帮你预先加载了第二个Fragment,当你加载第二个Fragment时,它会帮你加载第三个Fragment。如图:当滑动到FragmentB时,FragmentC也进行了预加载。预加载只会加载下一页,但是它会销毁上一页的前面那页