1

我的 android 应用程序有两个片段 1. 左一个(即菜单列表) 2. 右一个(即左侧菜单一项的详细信息)

我面临的问题是在每个详细信息屏幕上添加标签(右侧)我使用“片段内的标签”或“如何在片段中添加标签主机”等标签进行了很多搜索。

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

4

3 回答 3

1

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

于 2013-02-08T10:44:47.700 回答
1

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

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

片段 1 (tab1)

Fragment2 (tab2) - 此 Fragment 在左侧有 1 个 LinearLayout 以显示权重 = 1 的菜单,在右侧有其他 LinearLayout 以显示详细信息。在右侧的linearLayout中,我设置了一个Radiogroup来显示这个radiogroup,就像标签一样(像这样:https ://github.com/makeramen/android-segmentedradiobutton ,但带有标签资产)然后我将更改线性布局以模拟细节片段中的标签.

我希望你能明白

于 2013-02-08T10:48:54.270 回答
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();
        }
    }
于 2013-02-08T12:31:06.083 回答