I decided to create some small library which create awesome menu using ViewPager, Tabhost and FragmentActivity. So I have 3 small classes:
-
Draw menu (drawing all xml things)
public class DrawAMKMenu extends FragmentActivity { protected LinearLayout mLinearLayout; protected static TabHost mTabHost; protected static Context mContext; protected ViewPager mViewPager; protected RelativeLayout mMenuBar; protected Resources res; protected int screenwidth; protected Drawable ThumbResource = null; protected Drawable menuBg = null; protected TabWidget tw; /** * constructor * @param c * @param main_layout */ protected DrawAMKMenu(FragmentActivity fa, LinearLayout main_layout){ mContext = fa.getApplicationContext(); mLinearLayout = main_layout; res = fa.getResources(); Display display = fa.getWindowManager().getDefaultDisplay(); screenwidth = display.getWidth(); } /** *Draw main LinearLayout */ protected void DrawMenu() { mLinearLayout.removeAllViews(); mLinearLayout.setBaselineAligned(true); mLinearLayout.setOrientation(LinearLayout.VERTICAL); mLinearLayout.addView(createTabHost()); } /** * @return TabHost * draw the TabHost view */ protected TabHost createTabHost(){ mTabHost = new TabHost(mContext); mTabHost.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); mTabHost.addView(createTabContentLayout()); mTabHost.addView(createViewPager()); mTabHost.addView(createMenuBar()); mTabHost.setTag("TabHost"); SetupTabs(mTabHost); return mTabHost; } /** * Create FrameLayout for Content * @return FrameLayout */ protected FrameLayout createTabContentLayout(){ /*Frame layout for Content*/ FrameLayout fl = new FrameLayout(mContext); fl.setLayoutParams(new LinearLayout.LayoutParams(0,0,1)); fl.setId(android.R.id.tabcontent); /*------------------------*/ return fl; } /** * @return ViewPager * draw ViewPager element */ protected ViewPager createViewPager(){ mViewPager = new ViewPager(mContext); mViewPager.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1)); mViewPager.setBackgroundColor(Color.WHITE); mViewPager.setTag("viewpager"); return mViewPager; } /** * @return RelativeLayout * draw Menu bar */ protected RelativeLayout createMenuBar(){ /* Layout for Menu Bar */ mMenuBar = new RelativeLayout(mContext); mMenuBar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); //mMenuBar.setGravity(Gravity.BOTTOM); /* ------------------ */ /* Tab Widget*/ tw = new TabWidget(mContext); tw.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,0)); tw.setOrientation(LinearLayout.HORIZONTAL); tw.setId(android.R.id.tabs); if(menuBg != null) tw.setBackgroundDrawable(menuBg); /*-----------*/ /* Scroll bar for menu */ HorizontalScrollView hsv = new HorizontalScrollView(mContext); hsv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,0)); hsv.setScrollBarStyle(View.GONE); hsv.setFillViewport(true); /* ----------------- */ //add tabWidget to HorizontalScrollView hsv.addView(tw); /*Seek Bar*/ SeekBar sb = new SeekBar(mContext); sb.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); sb.setBackgroundColor(Color.TRANSPARENT); sb.setProgressDrawable(res.getDrawable(android.R.id.empty)); if(ThumbResource!=null) sb.setThumb(ThumbResource); else sb.setThumb(res.getDrawable(android.R.id.empty)); /*-------*/ /*Frame layout for SeekBar*/ FrameLayout fl = new FrameLayout(mContext); fl.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); fl.addView(sb); /*------------------------*/ mMenuBar.addView(hsv); mMenuBar.addView(fl); return mMenuBar; } /** * Delete bottom strip from tabhost * @param tabHost */ protected void SetupTabs(TabHost tabHost) { Field mBottomLeftStrip; Field mBottomRightStrip; try { mBottomLeftStrip = tw.getClass().getDeclaredField("mBottomLeftStrip"); mBottomRightStrip = tw.getClass().getDeclaredField("mBottomRightStrip"); if (!mBottomLeftStrip.isAccessible()) { mBottomLeftStrip.setAccessible(true); } if (!mBottomRightStrip.isAccessible()) { mBottomRightStrip.setAccessible(true); } mBottomLeftStrip.set(tw, res.getDrawable(Color.TRANSPARENT)); mBottomRightStrip.set(tw, res.getDrawable(Color.TRANSPARENT)); } catch (java.lang.NoSuchFieldException e) { // possibly 2.2 try { Method stripEnabled = tw.getClass().getDeclaredMethod("setStripEnabled", boolean.class); stripEnabled.invoke(tw, false); } catch (Exception e1) { e1.printStackTrace(); } } catch (Exception e) {} } }
-
PagerAdapter class
public class PagerAdapter extends FragmentPagerAdapter private List<Fragment> pFragments; /** * @param fm * @param fragments */ public PagerAdapter(FragmentManager fm, List<Fragment> fr) { super(fm); this.pFragments = fr; } /* (non-Javadoc) * @see android.support.v4.app.FragmentPagerAdapter#getItem(int) */ @Override public Fragment getItem(int position) { return this.pFragments.get(position); } /* (non-Javadoc) * @see android.support.v4.view.PagerAdapter#getCount() */ @Override public int getCount() { return this.pFragments.size(); } }
-
Create menu class.
public class AMKMenu extends DrawAMKMenu implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
private PagerAdapter mPagerAdapter;
private Vector<Fragment> fragments;
private FragmentManager mFragmentManager;
private FragmentActivity mFragmentActivity;
private static HashMap<Integer, Class<?>> TabsClass;
private static HashMap<Integer, String> TabsTabName;
private static HashMap<Integer, Drawable> TabsIcon;
/**
* CONSTRUCTOR
* Create AMKMenu object to setup settings and
* also initialize menu
* @param c
* @param main_layout
* @param fm
* @param screenWidth
*/
public AMKMenu(FragmentActivity fa, LinearLayout main_layout) {
super(fa, main_layout);
mFragmentActivity = fa;
mFragmentManager = fa.getSupportFragmentManager();
TabsClass = new HashMap<Integer, Class<?>>();
TabsTabName = new HashMap<Integer, String>();
TabsIcon = new HashMap<Integer, Drawable>();
}
/**
* Final method to create menu
* Use it after you setup all settings
*/
public void createMenu(){
super.DrawMenu();
initializeTabHost();
intializeViewPager();
}
/**
* A simple factory that returns dummy views to the Tabhost
* @author Gavryschuk Anatoliy V.
*/
public class TabFactory implements TabContentFactory {
private final Context fContext;
/**
* @param context
*/
public TabFactory(Context context) {
fContext = context;
}
/** (non-Javadoc)
* @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
*/
public View createTabContent(String tag) {
View v = new View(fContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
/**
* Initialize ViewPager
*/
protected void intializeViewPager() {
fragments = new Vector<Fragment>();
if(getTabsClass().size()>0)
{
for(int i=0;i<getTabsClass().size();i++)
{
fragments.add(Fragment.instantiate(mFragmentActivity, getTabsClass(i).getName()));
}
}
setPagerAdapter(new PagerAdapter (mFragmentManager, fragments));
mViewPager.setAdapter(getPagerAdapter());
mViewPager.setOnPageChangeListener(this);
}
/**
* Initialize the Tab Host
*/
protected void initializeTabHost() {
if(getTabsClass().size()>0)
{
TabHost mTabHost = (TabHost) mLinearLayout.findViewWithTag("TabHost");
mTabHost.setup();
for(int i=0;i<getTabsClass().size();i++)
{
TabSpec tabSpec = mTabHost.newTabSpec(getTabsTabName(i));
tabSpec.setIndicator(getTabsTabName(i), getTabsIcon(i));
tabSpec.setContent(new TabFactory(mFragmentActivity));
mTabHost.addTab(tabSpec);
}
mTabHost.setOnTabChangedListener(this);
mTabHost.getTabWidget().setHorizontalScrollBarEnabled(false);
for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
{
//mTabHost.getTabWidget().getChildAt(i).setLayoutParams(new LinearLayout.LayoutParams(65,65));
mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(0);
}
// mTabHost.setCurrentTab(0);
}else
return;
}...
}
after I use this library in this way
AMKMenu menu = new AMKMenu(this, (LinearLayout)findViewById(R.id.main_layout));
menu.setMenuBg(getResources().getDrawable(R.drawable.menu_bg));
menu.setCursor(getResources().getDrawable(R.drawable.new_cursor));
menu.AddTab(Tab1Fragment.class, "tab1", getResources().getDrawable(R.drawable.f_icon));
menu.AddTab(Tab2Fragment.class, "tab2", getResources().getDrawable(R.drawable.s_icon));
menu.AddTab(Tab3Fragment.class, "tab3", getResources().getDrawable(R.drawable.t_icon));
menu.createMenu();
I get error
java.lang.IllegalArgumentException: No new found for id for fragment
The problem is in intializeViewPager() method in AMKMenu class.
Please Somebody (Android guru) help me what do I do wrong?
I hope I explained everything clear.
Best Answer
Hear is my solution for the (Tabs + Fragment + ViewPager) it is works for me as i wanted, hope that works for you as well
hear is the xml file
hear is the code for MainActivity.java I'll post relevant code only so you'll have to manage it
and hear is how i deal with detail fragment not very efficient but kind of working
do not save the state of tab in their individual fragment it will conflict, we are already doing it hear
Hope It Helps