1.生命周期
启动Fragment时:
onAttach
onCreate
onCreateView
onViewCreated
onActivityCreated
onStart
onResume
启动后点击home键:
onPause
onSaveInstanceState
onStop
从home键再次进入:
onStart
onResume
Fragment1-->Fragment2:
fragment1: onPause onStop onDestroyView
fragment2:
onAttach
onCreate
onCreateView
onViewCreated
onActivityCreated
onStart
onResume
在回退Fragment2-->Fragment1:
fragment1:onCreateView onActivityCreated onStart onResume
点击back键退出:
onPause
onStop
onDestroyView
onDestroy
onDetach
如果启动2个Fragment后,点击back退出:
两个Fragment交替调用 onPause,onStop。然后一一调用onDestoryView,onDestory,onDetach.
2.Fragent底部导航的使用<忘了看的谁的博客源码了……>
<1>结构图:
<2> A-E fragment
public class Tab_AFragment extends Fragment { private static final String TAG = "Tab_AFragment"; public Tab_AFragment() { // Required empty public constructor } @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_tab, container, false); } }
Main.xml 布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#ffffff" tools:context="myapplication.com.myfragment.MainActivity"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/tab_content" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1.0" android:background="#77557799" /> <RadioGroup android:id="@+id/tabs_rg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center" > <RadioButton android:id="@+id/tab_rb_a" android:layout_width="0dp" android:layout_height="wrap_content" android:drawableTop="@drawable/tablatestalert" android:button="@null" android:text="Tab1" android:textColor="#000000" android:textSize="13sp" android:layout_weight="1.0" android:gravity="center" android:singleLine="true" android:checked="true" android:background="@drawable/selector_tab" /> <RadioButton android:id="@+id/tab_rb_b" android:layout_width="0dp" android:layout_height="wrap_content" android:drawableTop="@drawable/tabsearch" android:button="@null" android:text="Tab2" android:textColor="#000000" android:textSize="13sp" android:layout_weight="1.0" android:gravity="center" android:singleLine="true" android:background="@drawable/selector_tab" /> <RadioButton android:id="@+id/tab_rb_c" android:layout_width="0dp" android:layout_height="wrap_content" android:drawableTop="@drawable/tabrecommd" android:button="@null" android:text="Tab3" android:textColor="#000000" android:textSize="13sp" android:layout_weight="1.0" android:gravity="center" android:singleLine="true" android:background="@drawable/selector_tab" /> <RadioButton android:id="@+id/tab_rb_d" android:layout_width="0dp" android:layout_height="wrap_content" android:drawableTop="@drawable/tabconfigicon" android:button="@null" android:text="Tab4" android:textColor="#000000" android:textSize="13sp" android:layout_weight="1.0" android:gravity="center" android:singleLine="true" android:background="@drawable/selector_tab" /> <RadioButton android:id="@+id/tab_rb_e" android:layout_width="0dp" android:layout_height="wrap_content" android:drawableTop="@drawable/tababoutus" android:button="@null" android:text="Tab5" android:textColor="#000000" android:textSize="13sp" android:layout_weight="1.0" android:gravity="center" android:singleLine="true" android:background="@drawable/selector_tab" /> </RadioGroup> </LinearLayout> </LinearLayout>
选择:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 默认时的图片 --> <item android:drawable="@android:color/transparent" android:state_checked="false"/> <!-- 选择时的图片 --> <item android:drawable="@color/tab_selected_bg" android:state_checked="true"/> </selector>
MainActivity
public class MainActivity extends AppCompatActivity { private RadioGroup rgs; public List<Fragment> fragments = new ArrayList<Fragment>(); public String hello = "hello "; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragments.add(new Tab_AFragment()); fragments.add(new Tab_BFragment()); fragments.add(new Tab_CFragment()); fragments.add(new Tab_DFragment()); fragments.add(new Tab_EFragment()); rgs = (RadioGroup) findViewById(R.id.tabs_rg); FragmentTabAdapter tabAdapter = new FragmentTabAdapter(this, fragments, R.id.tab_content, rgs); tabAdapter.setOnRgsExtraCheckedChangedListener(new FragmentTabAdapter.OnRgsExtraCheckedChangedListener(){ @Override public void OnRgsExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index) { System.out.println("Extra---- " + index + " checked!!! "); } }); } }
FragmentTabAdapter
public class FragmentTabAdapter implements RadioGroup.OnCheckedChangeListener{ /** * tab对应的fragments */ private List<Fragment> fragments; /** * 单选按钮组,用于切换tab */ private RadioGroup rgs; /** * fragment 所属的activity */ private FragmentActivity fragmentActivity; /** * activity 中展示fragment的区域id */ private int fragmentContentId; /** * 当前tab页面的索引 */ private int currentTab; /** * 用于让调用者在切换tab时候增加新的功能 */ private OnRgsExtraCheckedChangedListener onRgsExtraCheckedChangedListener; public FragmentTabAdapter(FragmentActivity fragmentActivity, List<Fragment> fragments, int fragmentContentId, RadioGroup rgs) { this.fragments = fragments; this.rgs = rgs; this.fragmentActivity = fragmentActivity; this.fragmentContentId = fragmentContentId; // 默认显示第一页 FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction(); ft.add(fragmentContentId, fragments.get(0)); ft.commit(); rgs.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(RadioGroup radioGroup, int checkedId) { for(int i = 0; i < rgs.getChildCount(); i++){ if(rgs.getChildAt(i).getId() == checkedId){ Fragment fragment = fragments.get(i); FragmentTransaction ft = obtainFragmentTransaction(i); getCurrentFragment().onPause(); // 暂停当前tab // getCurrentFragment().onStop(); // 暂停当前tab if(fragment.isAdded()){ // fragment.onStart(); // 启动目标tab的onStart() fragment.onResume(); // 启动目标tab的onResume() }else{ ft.add(fragmentContentId, fragment); } showTab(i); // 显示目标tab ft.commit(); // 如果设置了切换tab额外功能功能接口 if(null != onRgsExtraCheckedChangedListener){ onRgsExtraCheckedChangedListener.OnRgsExtraCheckedChanged(radioGroup, checkedId, i); } } } } /** * 切换tab * @param idx */ private void showTab(int idx){ for(int i = 0; i < fragments.size(); i++){ Fragment fragment = fragments.get(i); FragmentTransaction ft = obtainFragmentTransaction(idx); if(idx == i){ ft.show(fragment); }else{ ft.hide(fragment); } ft.commit(); } currentTab = idx; // 更新目标tab为当前tab } /** * 获取一个带动画的FragmentTransaction * @param index * @return */ private FragmentTransaction obtainFragmentTransaction(int index){ FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction(); // 设置切换动画 if(index > currentTab){ ft.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_left_out); }else{ ft.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_right_out); } return ft; } public int getCurrentTab() { return currentTab; } public Fragment getCurrentFragment(){ return fragments.get(currentTab); } public OnRgsExtraCheckedChangedListener getOnRgsExtraCheckedChangedListener() { return onRgsExtraCheckedChangedListener; } public void setOnRgsExtraCheckedChangedListener(OnRgsExtraCheckedChangedListener onRgsExtraCheckedChangedListener) { this.onRgsExtraCheckedChangedListener = onRgsExtraCheckedChangedListener; } /** * 切换tab额外功能功能接口 */ static class OnRgsExtraCheckedChangedListener{ public void OnRgsExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index){ } } }
<3>效果图
<4>code:
http://pan.baidu.com/s/1gf3KKSz