Java tutorial
/* * Copyright (C) 2014 loQua.Xee <loquaciouser@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.fastexample.adapter; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.widget.RadioGroup; import com.android.fastexample.R; import java.util.List; /** * Do one thing at a time, and do well! * * @Prject: FastAndroid * @Location: com.android.fastexample.adapter.FragmentTabAdapter * @Description: TODO * @author: LoQua.Xee loquaciouser@gmail.com * @date: 2014/9/3 17:09 * @version: V1.0 */ public class FragmentTabAdapter implements RadioGroup.OnCheckedChangeListener { private List<Fragment> fragments; // tab?Fragment private RadioGroup rgs; // ?tab private FragmentActivity fragmentActivity; // FragmentActivity private int fragmentContentId; // Activity??id private int currentTab; // ?Tab? private OnRgsExtraCheckedChangedListener onRgsExtraCheckedChangedListener; // // ?tab 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(); if (fragment.isAdded()) { fragment.onResume(); } else { ft.add(fragmentContentId, fragment); } showTab(i); 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?? */ public static abstract class OnRgsExtraCheckedChangedListener { public abstract void OnRgsExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index); } }