1,什么是fragment??
英文释义:碎片; 片段,未完成的部分; (将文件内容) 分段;
1、Fragment是android3.0引入的心的API,它代表Activity的子模板,所以可以把fragment理解为Activity片段。
2、Fragment必须被“嵌入”Avtivity中使用,因此Fragment也拥有自己的生命周期,不过Fragment的生命周期受Activity所控制,也就是说Activity停止的时候,Activity中所有的Fragment都会被停止。其他状态也是一样。
(可以不看)Fragment也拥有自己的生命周期
onAttace(): 当fragment被加入到activity时调用
onCreate(): 系统创建fragment的时候回调他
onCreateView(): 第一次使用的时候 fragment会在这上面画一个layout出来, 为了可以画控件 要返回一个 布局的view,也可以返回null。
onActivityCreated(): 当Activity中的onCreate方法执行完后调用。
onStart(): 和activity一致,启动Fragement 启动时回调,,此时Fragement可见
onResume(): 和activity一致 在activity中运行是可见的。激活, Fragement 进入前台, 可获取焦点时激活。
onPause(): 和activity一致 其他的activity获得焦点,这个仍然可见
onStop(): 和activity一致, fragment不可见的, 可能情况:activity被stopped了OR fragment被移除但被,加入到回退栈中,一个stopped的fragment仍然是活着的如果长时间不用也会被移除
onDestroyView(): Fragment中的布局被移除时调用。表示fragemnt销毁相关联的UI布局, 清除所有跟视图相关的资源。
onDestroy(): 销毁fragment对象, 跟activity类似了。
onDetach(): Fragment和Activity解除关联的时候调用。 脱离activity。
2.用fragment解决什么问题
1、首先使用Fragment时,可以通过用户交互来执行一些动作,比如增加、移除、替换(add(), remove(), replace()方法,)等。
2、所有这些改变构成一个集合,这个集合被叫做一个transaction。
3、可以调用FragmentTransaction中的方法来处理这个transaction,并且可以将transaction存进由activity管理的back stack中,这样用户就可以进行fragment变化的回退操作。
3.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"
android:orientation="vertical"
tools:context="com.example.lenovo.gaoji.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="Hello World!"
android:textSize="30sp"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
> <fragment
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/f_one"
android:name="com.example.lenovo.gaoji.fragment.fragmentOne">
</fragment>
<fragment
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/f_two"
android:name="com.example.lenovo.gaoji.fragment.fragmentTwo">
</fragment>
</LinearLayout>
</LinearLayout>
效果图
3.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.lenovo.gaoji.TianmaoActivity">
<LinearLayout
android:layout_width="100dp"
android:orientation="vertical"
android:layout_height="match_parent">
<Button
android:id="@+id/nvzhuang_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="女装" />
<Button
android:id="@+id/nanzhuang_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="男装"
/>
</LinearLayout>
<FrameLayout
android:id="@+id/shop_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
效果图
4.ViewPager+Fragment实现页卡切换
这里是微信界面的三个布局,contact为联系人,friend为朋友圈,news为消息
package com.example.lenovo.gaoji.adapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import java.util.List;
/**
* Created by lenovo on 2018/3/6.
*/
public class MyPagerAdapter extends FragmentPagerAdapter{
private List<Fragment> mFragmentList;
public MyPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
super(fm);
this.mFragmentList = fragmentList;
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
}
package com.example.lenovo.gaoji;
import android.app.Fragment;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.lenovo.gaoji.adapter.MyPagerAdapter;
import com.example.lenovo.gaoji.fragment.ContactFragment;
import com.example.lenovo.gaoji.fragment.FriendFragment;
import com.example.lenovo.gaoji.fragment.NewsFragment;
import java.util.ArrayList;
import java.util.List;
public class WeixinActivity extends AppCompatActivity implements View.OnClickListener{
private Button contactBtn;
private Button friendBtn;
private Button newsBtn;
private ViewPager viewPager;
private ContactFragment contactFragment;
private FriendFragment friendFragment;
private NewsFragment newsFragment;
private List<Fragment> fragmentList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weixin);
bindID();
contactFragment = new ContactFragment();
friendFragment = new FriendFragment();
newsFragment = new NewsFragment();
fragmentList.add(contactFragment);
fragmentList.add(friendFragment);
fragmentList.add(newsFragment);
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(),fragmentList);
viewPager.setAdapter(adapter);
}
private void bindID() {
contactBtn = findViewById(R.id.contact_btn);
friendBtn = findViewById(R.id.friend_btn);
newsBtn = findViewById(R.id.news_btn);
viewPager = findViewById(R.id.wx_vp);
contactBtn.setOnClickListener(this);
friendBtn.setOnClickListener(this);
newsBtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.contact_btn:
viewPager.setCurrentItem(0);
break;
case R.id.friend_btn:
viewPager.setCurrentItem(1);
break;
case R.id.news_btn:
viewPager.setCurrentItem(2);
break;
}
}
}
其中一个布局展示