如何在没有任何Intent或Views的情况下在android简单标签中实现(比如在TabHost中)?
我只需要标签标题,并希望了解标签索引的更改方式。
或者告诉我如何实现多位置开关。
UPD:我使用已经用于其他目的的api v14和ActionBar标签。
答案 0 :(得分:1)
最简单的解决方案(当然,我认为)只需使用三种不同的ImageView或Buttons来模仿“标签”。您可以根据需要替换“选定选项卡”图像的“未选定选项卡”图像。
据我所知,TabHosts和其他此类解决方案之外没有简单的方法需要不同选项卡的活动,片段或视图。
答案 1 :(得分:0)
我有一个类似的项目,我只需要听取标签更改。以下是Google的一个很好的例子:
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
中用于创建height
和width
为0的内容视图,您可以在布局中的TabHost下添加实际内容。
答案 2 :(得分: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);
}
}
});