Java tutorial
/********************************************************************** * All Rights Reserved * * MARVELL CONFIDENTIAL * Copyright 2015 Marvell International Ltd All Rights Reserved. * The source code contained or described herein and all documents related to * the source code ("Material") are owned by Marvell International Ltd or its * suppliers or licensors. Title to the Material remains with Marvell International Ltd * or its suppliers and licensors. The Material contains trade secrets and * proprietary and confidential information of Marvell or its suppliers and * licensors. The Material is protected by worldwide copyright and trade secret * laws and treaty provisions. No part of the Material may be used, copied, * reproduced, modified, published, uploaded, posted, transmitted, distributed, * or disclosed in any way without Marvell's prior express written permission. * * No license under any patent, copyright, trade secret or other intellectual * property right is granted to or conferred upon you by disclosure or delivery * of the Materials, either expressly, by implication, inducement, estoppel or * otherwise. Any license under such intellectual property rights must be * express and approved by Marvell in writing. * **********************************************************************/ package com.android.firewall.ui.adapter; import java.util.ArrayList; import android.app.Fragment; import android.app.FragmentManager; import android.content.Context; import android.support.v13.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; import android.view.ViewGroup; import android.widget.TabHost; import android.widget.TabWidget; import com.android.firewall.ui.fragment.BWSwitchFragment; import com.android.firewall.ui.fragment.PMSwitchFragment; import com.android.firewall.ui.fragment.SettingsFragment; public class MainPagerAdapter extends FragmentPagerAdapter implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener { private final Context context; private final TabHost tabHost; private final ViewPager pager; private final ArrayList<String> tabs = new ArrayList<String>(); // dummy contents class class DummyTabFactory implements TabHost.TabContentFactory { private final Context context; public DummyTabFactory(Context context) { this.context = context; } public View createTabContent(String tag) { View v = new View(context); v.setMinimumWidth(0); v.setMinimumHeight(0); return v; } } public MainPagerAdapter(FragmentManager fm, Context context, TabHost tabHost, ViewPager pager) { super(fm); this.context = context; this.tabHost = tabHost; this.pager = pager; this.tabHost.setOnTabChangedListener(this); this.pager.setAdapter(this); this.pager.setOnPageChangeListener(this); } public void addTab(TabHost.TabSpec tabSpec, String content) { tabSpec.setContent(new DummyTabFactory(this.context)); tabs.add(content); tabHost.addTab(tabSpec); notifyDataSetChanged(); } @Override public Fragment getItem(int position) { if (position == 0) return new BWSwitchFragment(); else if (position == 1) // return new EmptyFragment(Color.GRAY); return new PMSwitchFragment(); else return new SettingsFragment(); } @Override public int getCount() { return 3; } public void onPageScrollStateChanged(int state) { ; } public void onPageScrolled(int position, float positionOffset, int positionOggsetPixesl) { ; } public void onPageSelected(int position) { TabWidget widget = tabHost.getTabWidget(); int oldFocus = widget.getDescendantFocusability(); widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); tabHost.setCurrentTab(position); widget.setDescendantFocusability(oldFocus); } public void onTabChanged(String tabId) { int position = tabHost.getCurrentTab(); pager.setCurrentItem(position); } }