ViewPager, Tabhost 및 FragmentActivity를 사용하여 멋진 메뉴를 만드는 작은 라이브러리를 만들기로 결정했습니다.android.support.v4의 ViewPager, TabHost 및 Fragments. 모두 함께 작동하지 않습니다
그리기 메뉴 (모든 XML 것을 그림)
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 클래스
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(); } }
메뉴 클래스를 만듭니다 : 그래서 3 개 소규모 학급이있다.
공용 클래스 AMKMenu는
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();
내가 오류를
을 얻을 나는이 방법이 라이브러리를 사용 후 DrawAMKMenu가 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; }... }
를 구현 확장
java.lang.IllegalArgu mentException : 조각에 대한 ID가 새로 발견되지 않았습니다.
AMKMenu 클래스의 intializeViewPager() 메소드에 문제가 있습니다.
제발 누군가 (안드로이드 전문가) 제가 뭘 잘못하니 도와주세요? 나는 모든 것이 분명하다고 설명했으면한다.
클래스 도움이되기를 바랍니다들을하고있는 충돌합니다 개별 조각에 탭의 상태를 저장하지 않습니다 R.id.main_layout에서 참조를 가져올 수 없습니다. 자신의/다른 레이아웃을 사용하십시오. – Yahor10
intializeViewPager() 메서드를 사용하지 않는 것처럼 내 문제를 해결하는지 잘 모르겠다. TabHost는 훌륭하게 작동한다. 그래서 내 수업은 R.id.main_layout에서 참조를 얻습니다. – devworkstation