片段中的标签

时间:2013-02-08 10:42:21

标签: android android-fragments android-tabhost

我的android应用程序拥有两个片段  1.左一个(这是一个菜单列表)  2.右边一个(这是左侧菜单中一个项目的细节)

我面临的问题是在每个细节屏幕上添加标签(右侧) 我搜索了很多标签“片段里面的标签”或“如何在片段中添加tabhost”等等。

请帮帮我。 我被困在里面! : - (

3 个答案:

答案 0 :(得分:1)

使用最新的支持库,您可以在片段中使用ViewPager。 ViewPager在类似标签的视图中显示片段(例如在Play商店中)。 ViewPager中的“标签”也是片段。

答案 1 :(得分:1)

我面临同样的问题,这个解决方案对我有用:

MainActivity - 此活动包含操作栏选项卡(不是tabhost)

Fragment1(tab1)

Fragment2(tab2) - 此片段左侧有1个LinearLayout,用于显示权重= 1的菜单和右侧的其他LinearLayout以显示详细信息。在右侧linearLayout内部,我设计了一个Radiogroup来显示这个放射组像选项卡(类似这样:https://github.com/makeramen/android-segmentedradiobutton但是有标签资产)然后我将改变线性布局来模拟细节片段内的标签。

我希望你明白这个想法

答案 2 :(得分:0)

试试这个。

public class TabBar extends FragmentActivity implements
            TabHost.OnTabChangeListener {

        public static Context mContext;
        private TabHost mTabHost;
        private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabBar.TabInfo>();
        private TabInfo mLastTab = null;

        private class TabInfo {
            private String tag;
            private Class<?> clss;
            private Bundle args;
            private Fragment fragment;

            TabInfo(String tag, Class<?> clazz, Bundle args) {
                this.tag = tag;
                this.clss = clazz;
                this.args = args;
            }

        }

        class TabFactory implements TabContentFactory {

            private final Context mContext;

            public TabFactory(Context context) {
                mContext = context;
            }

            /**
             * (non-Javadoc)
             * 
             * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
             */
            public View createTabContent(String tag) {
                View v = new View(mContext);
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }

        }

        /**
         * (non-Javadoc)
         * 
         * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
         */
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.tabs_layout);

            initialiseTabHost(savedInstanceState);
            if (savedInstanceState != null) {
                mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); 
            }
        }


        protected void onSaveInstanceState(Bundle outState) {
            outState.putString("tab", mTabHost.getCurrentTabTag()); // save the tab
                                                                    // selected
            super.onSaveInstanceState(outState);
        }

        private void initialiseTabHost(Bundle args) {
            mTabHost = (TabHost) findViewById(android.R.id.tabhost);
            mTabHost.setup();
            // mTabHost.getTabWidget().setStripEnabled(false);

            TabInfo tabInfo = null;
            TabBar.addTab(this, this.mTabHost,this.mTabHost.newTabSpec("Tab1"), (tabInfo = new TabInfo("Tab1", MyMapFragment.class, args)), "Favourite",
                    R.drawable.frd_tab_select_custom);
            this.mapTabInfo.put(tabInfo.tag, tabInfo);
            TabBar.addTab(this, this.mTabHost,this.mTabHost.newTabSpec("Tab2"), (tabInfo = new TabInfo(
                            "Tab2", PrflTab.class, args)), "Favourite",
                    R.drawable.me_tab_select_custom);
            this.mapTabInfo.put(tabInfo.tag, tabInfo);
            TabBar.addTab(this, this.mTabHost,
                    this.mTabHost.newTabSpec("Tab3"), (tabInfo = new TabInfo("Tab3", RequestFragment.class, args)), "Favourite",
                    R.drawable.request_tab_select_custom);
            this.mapTabInfo.put(tabInfo.tag, tabInfo);


            // Default to first tab

                this.onTabChanged("Tab1");

            mTabHost.setOnTabChangedListener(this);
        }

        /**
         * @param activity
         * @param tabHost
         * @param tabSpec
         * @param clss
         * @param args
         */
        private static View prepareTabView(Context context, String text,
                int drawable) {
            View view = LayoutInflater.from(context).inflate(
                    R.layout.tab_indicator, null);
            ((ImageView) view.findViewById(R.id.icon)).setImageResource(drawable);
            return view;

        }

        private static void addTab(TabBar activity, TabHost tabHost,
                TabHost.TabSpec tabSpec, TabInfo tabInfo, String title, int drawable) {
            // Attach a Tab view factory to the spec
            tabSpec.setContent(activity.new TabFactory(activity));
            String tag = tabSpec.getTag();
            View view = prepareTabView(tabHost.getContext(), title, drawable);
            tabSpec.setIndicator(view);

            tabInfo.fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
            if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
                FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
                ft.detach(tabInfo.fragment);
                ft.commit();
                activity.getSupportFragmentManager().executePendingTransactions();
            }

            tabHost.addTab(tabSpec);
            boolean flag = activity.getIntent().getBooleanExtra("notifi_falg", false);
            if (flag) {
                tabHost.setCurrentTab(2);
            }
        }


        public void onTabChanged(String tag) {
            TabInfo newTab = this.mapTabInfo.get(tag);
            if (mLastTab != newTab) {
                FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
                if (mLastTab != null) {
                    if (mLastTab.fragment != null) {
                        ft.detach(mLastTab.fragment);
                    }
                }
                if (newTab != null) {
                    if (newTab.fragment == null) {
                        newTab.fragment = Fragment.instantiate(this,newTab.clss.getName(), newTab.args);
                        ft.add(R.id.tab_1, newTab.fragment, newTab.tag);
                    } else {
                        ft.attach(newTab.fragment);
                    }
                }

                mLastTab = newTab;
                ft.commit();
                this.getSupportFragmentManager().executePendingTransactions();
            }
        }

        @Override
        public void onStop() {
            super.onStop();
        }
    }