gpt4 book ai didi

android - 在 android 中使用 PagerAdapter 在单个选项卡下进行多个 Activity (可滑动选项卡)

转载 作者:太空狗 更新时间:2023-10-29 14:18:07 25 4
gpt4 key购买 nike

如何让一个选项卡包含多个 Activity ?到目前为止,我已经开发了一个小演示(如下图所示)。

enter image description here

这里,“签到”选项卡中只有一项 Activity 。现在,假设我单击“ checkin ”选项卡中的一个按钮,第二个 Activity 应该在同一个“ checkin ”选项卡下提示。下面是我使用寻呼机适配器的代码。

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>

主 Activity .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 的 fragment 主题。我很困惑如何解决这个问题。需要做什么?或者有没有更好的解决方案可以在单个选项卡下进行多个 Activity ?

最佳答案

您需要使用 Fragment为了这个目的..

您可以将按钮点击时的 fragment 替换为:

   FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.rootNode, mFragment);
ft.commit();

编辑:

请引用this实现 fragment (所有 API 都支持)。

关于android - 在 android 中使用 PagerAdapter 在单个选项卡下进行多个 Activity (可滑动选项卡),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19509421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com