如何制作包含多个活动的单个选项卡?到目前为止,我已经开发了一个小演示(如下图所示)。
在这里,“签入”选项卡中只有一项活动。现在,假设我单击“签入”选项卡中的一个按钮,第二个活动应该在同一个“签入”选项卡下提示。下面是我使用寻呼机适配器的代码。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</HorizontalScrollView>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<FrameLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<FrameLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<FrameLayout
android:id="@+id/tab4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<FrameLayout
android:id="@+id/tab5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
</FrameLayout>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</TabHost>
MainActivity.java
public class MainActivity extends Activity implements OnTabChangeListener, OnPageChangeListener{
private TabHost tabHost;
public static ViewPager pager;
HorizontalScrollView mHorizontalScroll;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (TabHost)findViewById(android.R.id.tabhost);
pager = (ViewPager) findViewById(R.id.pager);
mHorizontalScroll = (HorizontalScrollView)findViewById(R.id.scroll);
tabHost.setup();
TabWidget tabwidget=tabHost.getTabWidget();
TabSpec spec = tabHost.newTabSpec("tab1");
spec.setContent(R.id.tab1);
spec.setIndicator("Check In");
tabHost.addTab(spec);
spec = tabHost.newTabSpec("tab2");
spec.setContent(R.id.tab2);
spec.setIndicator("Buddies");
tabHost.addTab(spec);
spec = tabHost.newTabSpec("tab3");
spec.setContent(R.id.tab3);
spec.setIndicator("Recommendation");
tabHost.addTab(spec);
spec = tabHost.newTabSpec("tab4");
spec.setContent(R.id.tab4);
spec.setIndicator("Feed");
tabHost.addTab(spec);
spec = tabHost.newTabSpec("tab5");
spec.setContent(R.id.tab5);
spec.setIndicator("Last");
tabHost.addTab(spec);
pager.setAdapter(new MyPagerAdapter(this));
pager.setOnPageChangeListener(this);
tabHost.setOnTabChangedListener(this);
}
@Override
public void onTabChanged(String tabId)
{
int pageNumber = 0;
if(tabId.equals("tab1"))
{
pageNumber = 0;
}
else if(tabId.equals("tab2"))
{
pageNumber = 1;
}
else if(tabId.equals("tab3"))
{
pageNumber = 2;
}
else if(tabId.equals("tab4"))
{
pageNumber = 3;
}
else if(tabId.equals("tab5"))
{
pageNumber = 4;
}
else
{
pageNumber = 0;
}
pager.setCurrentItem(pageNumber);
}
@Override
public void onPageSelected(int pageNumber) {
tabHost.setCurrentTab(pageNumber);
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
View tabView = tabHost.getTabWidget().getChildAt(position);
if (tabView != null)
{
final int width = mHorizontalScroll.getWidth();
final int scrollPos = tabView.getLeft() - (width - tabView.getWidth()) / 2;
mHorizontalScroll.scrollTo(scrollPos, 0);
} else {
mHorizontalScroll.scrollBy(positionOffsetPixels, 0);
}
}
public void switchTabBar(int tab) {
tabHost.setCurrentTab(tab);
}
}
MyPagerAdaper.java
public class MyPagerAdapter extends PagerAdapter {
private Context ctx;
public MyPagerAdapter(Context ctx) {
this.ctx = ctx;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View parent_view = null;
ctx = container.getContext();
if (position ==0)
{
parent_view = getViewForPageOne();
((ViewPager) container).addView(parent_view, 0);
return parent_view;
}
else if (position == 1)
{
parent_view = getViewForPageTwo();
((ViewPager) container).addView(parent_view, 0);
return parent_view;
}
else
{
TextView tView = new TextView(ctx);
position++;
tView.setText("Page number: " + position);
tView.setTextColor(Color.RED);
tView.setTextSize(20);
container.addView(tView);
return tView;
}
}
@Override
public int getCount() {
return 5;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
private View getViewForPageOne()
{
final LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.first, null);
Button btn = (Button)v.findViewById(R.id.first);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Hello", 2000).show();
MainActivity.pager.setCurrentItem(1);
}
});
return v;
}
private View getViewForPageTwo(){
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.second, null);
return v;
}
}
我正在 api 级别 2.2 上开发我的应用程序。我也希望我的标签可以滑动,我做到了。另外,我遇到了需要 api 级别 11 的片段的主题。我很困惑如何解决这个问题。需要做什么?或者有没有更好的解决方案可以在单个选项卡下进行多项活动?