Fragment是activity的界面中的一部分或一种行为。你可以把多个Fragment们组合到一个activity中来创建一个多面界面并且你可以在多个activity中重用一个Fragment。你可以把Fragment认为模块化的一段activity,它具有自己的生命周期,接收它自己的事件,并可以在activity运行时被添加或删除。
Fragment不能独立存在,它必须嵌入到activity中,而且Fragment的生命周期直接受所在的activity的影响。例如:当activity暂停时,它拥有的所有的Fragment们都暂停了,当activity销毁时,它拥有的所有Fragment们都被销毁。然而,当activity运行时(在onResume()之后,onPause()之前),你可以单独地操作每个Fragment,比如添加或删除或替代(add(),remove(),replace())它们。当你在执行上述针对Fragment的事务时,你可以将事务添加到一个棧中,这个栈被activity管理,栈中的每一条都是一个Fragment的一次事务。有了这个栈,就可以反向执行Fragment的事务,这样就可以在Fragment级支持“返回”键(向后导航)。
下面是仿微信Tab却换的代码:
主要页面的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="com.chenjun.jun_0614_fragment.MainActivity">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/vp_main_pager"
></android.support.v4.view.ViewPager>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/rg_main_group"
>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:text="微信"
android:button="@null"
android:drawableTop="@drawable/weixin"
android:gravity="center"
android:id="@+id/rb_main_01"
android:background="@drawable/selector"
/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:text="通讯录"
android:button="@null"
android:drawableTop="@drawable/contacts"
android:gravity="center"
android:id="@+id/rb_main_02"
android:background="@drawable/selector"
/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:text="发现"
android:button="@null"
android:drawableTop="@drawable/find"
android:gravity="center"
android:id="@+id/rb_main_03"
android:background="@drawable/selector"
/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:text="我"
android:button="@null"
android:drawableTop="@drawable/my"
android:gravity="center"
android:id="@+id/rb_main_04"
android:background="@drawable/selector"
/>
</RadioGroup>
</LinearLayout>
微信页面(聊天)的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:text="这是微信页面(聊天记录)"
android:textSize="30sp"
android:background="#66ff0000"
/>
</LinearLayout>
通讯录页面的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:text="这是通讯录页面"
android:textSize="30sp"
android:background="#66ff00ff"
/>
</LinearLayout>
发现页面的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:text="这是发现页面"
android:textSize="30sp"
android:background="#660000ff"
/>
</LinearLayout>
我的页面的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:text="这是我的页面"
android:textSize="30sp"
android:background="#66ffff00"
/>
</LinearLayout>
主要java代码:
package com.chenjun.jun_0614_fragment;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewPager vp_main_pager;
private List<Fragment> fragments=new ArrayList<>();
private int[] images={R.id.rb_main_01,R.id.rb_main_02,R.id.rb_main_03,R.id.rb_main_04};
private RadioGroup rg_main_group;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp_main_pager = (ViewPager) findViewById(R.id.vp_main_pager);
WeiXinFragment weiXinFragment=new WeiXinFragment();
ContactsFragment contactsFragment=new ContactsFragment();
FindFragment findFragment=new FindFragment();
MyFragment myFragment=new MyFragment();
fragments.add(weiXinFragment);
fragments.add(contactsFragment);
fragments.add(findFragment);
fragments.add(myFragment);
vp_main_pager.setAdapter(new MyAdapter(getSupportFragmentManager()));
//
rg_main_group = (RadioGroup) findViewById(R.id.rg_main_group);
rg_main_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
if (checkedId==images[0]){
vp_main_pager.setCurrentItem(0);
}else if(checkedId==images[1]){
vp_main_pager.setCurrentItem(1);
}else if(checkedId==images[2]){
vp_main_pager.setCurrentItem(2);
}else if(checkedId==images[3]){
vp_main_pager.setCurrentItem(3);
}
}
});
rg_main_group.check(0);
vp_main_pager.setCurrentItem(0);
vp_main_pager.getCurrentItem();
//
vp_main_pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
if (state==2){
rg_main_group.check(images[vp_main_pager.getCurrentItem()]);
}
}
});
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
}
}
其他碎片页面的Java代码:
package com.chenjun.jun_0614_fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2017/6/15.
*/
public class WeiXinFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_weixin,null);
}
}
通讯录:
package com.chenjun.jun_0614_fragment;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2017/6/15.
*/
public class ContactsFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_contacts,null);
}
}
发现:
package com.chenjun.jun_0614_fragment;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2017/6/15.
*/
public class FindFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_find,null);
}
}
我的:
package com.chenjun.jun_0614_fragment;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2017/6/15.
*/
public class MyFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_my,null);
}
}