Java tutorial
/* * Copyright (C) 2014-10-14 The Android Open Source Project * * 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.zepan.android.widget; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.LinearLayout; /** * ???LinearLayout?2???Activity * XML???attachContent(Fragment ...)?tabfragment? * ????2? * @author zhanglei * @version 1.0 * @date 2015-08-20 */ public class SwitchPageView extends LinearLayout { private static final String TAG = "SwitchPageView"; private FragmentManager frmMgr = null; private Fragment[] mStoredFragments = null; /**XML?id*/ private int mContentResId = 0; /**????*/ private int mPage = 0; /** * Fragment?? * */ private List<Class<? extends Fragment>> mFrmCls = new ArrayList<Class<? extends Fragment>>(); /** * @param context * @param attrs */ public SwitchPageView(Context context, AttributeSet attrs) { super(context, attrs); FragmentActivity ac = (FragmentActivity) context; frmMgr = ac.getSupportFragmentManager(); } public void attachContent(int contentId, Class<? extends Fragment>... frmCls) { if (contentId == 0) { Log.w(TAG, "invalid content id"); return; } this.mContentResId = contentId; if (frmCls == null || frmCls.length == 0) { Log.w(TAG, "?Fragment?"); } for (Class<? extends Fragment> c : frmCls) { mFrmCls.add(c); } mStoredFragments = new Fragment[mFrmCls.size()]; defineEvent(); } public void setCurrentView(int index) { // ??? if (index == 0) { getChildAt(0).setVisibility(View.GONE); } else if (index == mFrmCls.size() - 1) {//???? getChildAt(1).setVisibility(View.GONE); } else { getChildAt(0).setVisibility(View.VISIBLE); getChildAt(1).setVisibility(View.VISIBLE); } // ?Fragment if (mStoredFragments[index] == null) { try { // instantiate the fragment by the class at the [index] of // showFrms mStoredFragments[index] = mFrmCls.get(index).newInstance(); // Fragment?? Bundle data = new Bundle(); data.putInt("index", index); mStoredFragments[index].setArguments(data); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } // get a fragment transaction and store it. FragmentTransaction trac = frmMgr.beginTransaction().setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out); Fragment currentFragment = mStoredFragments[index]; if (currentFragment != null) { // if the fragment is not added,add it to layout,else show it. if (!currentFragment.isAdded()) { trac.add(mContentResId, currentFragment); } else { trac.show(currentFragment); } } // hide other fragments. for (int i = 0; i < mStoredFragments.length; i++) { if (i != index && mStoredFragments[i] != null) { trac.hide(mStoredFragments[i]); } } // trac.addToBackStack(null); trac.commit(); } public Fragment getFragment(int index) { return mStoredFragments[index]; } private void defineEvent() { for (int i = 0; i < getChildCount(); i++) { final View view = getChildAt(i); view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (v == getChildAt(0)) { if (--mPage < 0) { mPage = 0; } setCurrentView(mPage); } else { if (++mPage > mFrmCls.size() - 1) { mPage = mFrmCls.size() - 1; } setCurrentView(mPage); } } }); } } /**????0*/ public int getPage() { return mPage; } /**??fragment*/ public Fragment getCurrentFragment() { return getFragment(this.mPage); } }