0

如何在没有任何意图或视图的 android 简单选项卡中实现(如 TabHost 中)?

我只需要标签标题并想听标签索引如何变化。

或者告诉我如何实现多位置开关。

UPD:我使用 api v14 和 ActionBar 选项卡已用于其他目的。

4

3 回答 3

1

最简单的解决方案(当然,在我看来)是简单地使用三个不同的 ImageViews 或 Buttons 来模仿“标签”。您可以根据需要将“未选择的选项卡”图像换成“选定的选项卡”图像。

据我所知,除了 TabHosts 和其他需要针对不同选项卡的活动、片段或视图的此类解决方案之外,没有简单的方法。

于 2012-07-10T03:56:24.327 回答
0

不久前我有一个类似的项目,我只需要监听选项卡的变化。这是谷歌的一个很好的例子:

public class MyPagerAdapter extends FragmentPagerAdapter
        implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
    private final Context mContext;
    private final TabHost mTabHost;
    private final ViewPager mViewPager;

private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

private class TabInfo {
    @SuppressWarnings("unused")
    private final String tag;
    private final Class<?> clss;
    private final Bundle args;

    public TabInfo(String _tag, Class<?> _clss, Bundle _args) {
        tag = _tag;
        clss = _clss;
        args = _args;       
            }
}

private class DummyFactory implements TabHost.TabContentFactory {

    private final Context mContext;

    public DummyFactory(Context context) {
        mContext = context;
    }
    public View createTabContent(String tag) {
        View v = new View(mContext);
        v.setMinimumHeight(0);
        v.setMinimumWidth(0);
        return v;
    }

}
public MyPagerAdapter(FragmentActivity activity, TabHost tabHost, ViewPager viewPager) {
    super(activity.getSupportFragmentManager());

    mContext = activity;
    mTabHost = tabHost;
    mViewPager = viewPager;

    mTabHost.setOnTabChangedListener(this);
    mViewPager.setAdapter(this);
    mViewPager.setOnPageChangeListener(this);
}

public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
    tabSpec.setContent(new DummyFactory(mContext));

    String tag = tabSpec.getTag();

    TabInfo tab = new TabInfo(tag, clss, args);
    mTabs.add(tab);
    mTabHost.addTab(tabSpec);
    this.notifyDataSetChanged();

}

@Override
public Fragment getItem(int i) {
    TabInfo tab = mTabs.get(i);
    Fragment fragment = Fragment.instantiate(mContext, tab.clss.getName(), tab.args);
    Log.d("DEBUG", "getItem from view pager called returning hash: " + fragment.hashCode());
    return fragment;
}

@Override
public int getCount() {

    return mTabs.size();
}

public void onPageScrollStateChanged(int arg0) {
    // TODO Auto-generated method stub

}

public void onPageScrolled(int arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub

}


public void onPageSelected(int position) {
    // Unfortunately when TabHost changes the current tab, it kindly
    // also takes care of putting focus on it when not in touch mode.
    // The jerk.
    // This hack tries to prevent this from pulling focus out of our
    // ViewPager.
    TabWidget widget = mTabHost.getTabWidget();
    int oldFocusability = widget.getDescendantFocusability();
    widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
    mTabHost.setCurrentTab(position);
    widget.setDescendantFocusability(oldFocusability);
}

public void onTabChanged(String tabId) {
    int position = mTabHost.getCurrentTab();

    mViewPager.setCurrentItem(position);

}
 }

这里的相关部分是适配器实现TabHost.OnTabChangeListener并且在构造函数的主体中,相应地设置了 onTabChangedListener ( mTabHost.setOnTabChangedListener(this))

您还会注意到内部类DummyFactory

private class DummyFactory implements TabHost.TabContentFactory {

    private final Context mContext;

    public DummyFactory(Context context) {
        mContext = context;
    }
    public View createTabContent(String tag) {
            View v = new View(mContext);
        v.setMinimumHeight(0);
        v.setMinimumWidth(0);
        return v;
    }
}

用于addTab创建0heightwidth0 的内容视图,允许您在布局中的 TabHost 下添加实际内容。

于 2012-07-10T05:43:15.397 回答
0

这是一个例子

假设 se01、se02 和 se03 是水平 LinearLayout 或 ScrollView 中的三个按钮(如果您想支持更多选项卡)。因此,您可以在每个按钮上设置 onClickListeners 并对其进行编程以在单击时切换到特定背景(这可能显示选项卡已被选中)。这是最简单的方法。这是一个代码片段,很容易理解。希望这可以帮助 :-)

            //initialize the first button to be 'selected' when the activity is started by a background which is similar to the other buttons but shows the selected button/tab as highlighted.

    se01.setBackgroundResource(R.drawable.popup_full_dark2);
    lastClicked = (Button) findViewById(R.id.ph_s01);//this keeps a track of the last tab/button pressed
    toSe01();// a function which performs the changes to the view with respect to the tab selected

    //Listeners
    se01.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (v.getId() != lastClicked.getId()) {
                lastClicked
                        .setBackgroundResource(R.drawable.popup_bottom_dark);//changes the background to show the tab is selected
                v.setBackgroundResource(R.drawable.popup_full_dark2);
                lastClicked = se01;
                s.startAnimation(new Animation9());
                toSe01();
                s.scrollTo(0, 0);
            }
        }
    });
    se02.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub
            if (v.getId() != lastClicked.getId()) {
                lastClicked
                        .setBackgroundResource(R.drawable.popup_bottom_dark);
                v.setBackgroundResource(R.drawable.popup_full_dark2);
                lastClicked = se02;
                s.startAnimation(new Animation9());
                toSe02();
                s.scrollTo(0, 0);
            }
        }
    });
    se03.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub
            if (v.getId() != lastClicked.getId()) {
                lastClicked
                        .setBackgroundResource(R.drawable.popup_bottom_dark);
                v.setBackgroundResource(R.drawable.popup_full_dark2);
                lastClicked = se03;
                s.startAnimation(new Animation9());
                toSe03();
                s.scrollTo(0, 0);
            }
        }
    });
于 2012-07-10T05:46:30.933 回答