我对Android环境非常陌生,因此对于你们这些人来说这个问题可能很简单。请尽量不要对我太苛刻。感谢。
按照本网站上的教程:http://www.linux.com/learn/tutorials/761642-android-app-development-for-beginners-navigation-with-tabs,我已经成功创建了一个应用,其中有三个标签,使其拥有layout
。
然后我找到了另一个关于ViewPager
:http://www.androidbegin.com/tutorial/android-viewpager-gallery-images-and-texts-tutorial/
我试图将这两个应用程序合并为一个,这意味着将ViewPager
放入我的 tab3 。
如链接所示, tab3 需要setTabListener
并期待Fragment类如下所示:
ActionBar.Tab tab3;
Fragment fragmentTab3 = new FragmentTab3();
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tab3.setTabListener(new MyTabListener(fragmentTab3));
actionBar.addTab(tab3);
我完全没有关于如何修改代码的线索,因此它接受了一个扩展PagerAdapter
的类,并允许 tab3 作为{{1}执行}
答案 0 :(得分:5)
我已经构建了一个类似的应用程序,其中所有三个选项卡都包含其中的查看器。
标签实现与您提到的相同:
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
setContentView(R.layout.activity_main);
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.ic_action_file)
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.ic_action_music)
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.ic_action_video)
.setTabListener(this));
将片段分配给各个标签,如下所示:
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Fragment mFragment;
Log.d(TAG, "Inside on tab selected");
switch (tab.getPosition()) {
case 0:
mFragment = Fragment1.newInstance(null);
ft.replace(R.id.content, mFragment, "fragment1");
break;
case 1:
mFragment = Fragment2.newInstance();
ft.replace(R.id.content, mFragment, "fragment2");
break;
case 2:
mFragment = Fragment3.newInstance();
ft.replace(R.id.content, mFragment, "fragment3");
break;
default:
break;
}
}
现在fragment3不需要扩展pageradapter。第3个片段的内部布局,添加一个viewpager
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_music_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/colorBackground"
tools:context="com.gridants.cubeit.ui.MusicFragment" >
<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="match_parent" >
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
</FrameLayout>
在fragment3代码中,声明viewpager和自定义fragmentStatePagerAdapter
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(
getChildFragmentManager());
mViewPager = (ViewPager) view.findViewById(R.id.pager);
mViewPager.setAdapter(mDemoCollectionPagerAdapter);
以下是实现自定义适配器
的示例代码 public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
private final FragmentManager mFragmentManager;
public Fragment mFragmentAtPos0;
private Context context;
public DemoCollectionPagerAdapter(FragmentManager fm) {
super(fm);
mFragmentManager = fm;
}
@Override
public Fragment getItem(int i) {
Fragment mFragment;
Bundle args;
Log.d(TAG, "Inside on tab selected");
switch (i) {
case 0:
mFragment = new InnerFragment1();
break;
case 1:
mFragment = new InnerFragment2();
break;
case 2:
mFragment = new InnerFragment3();
break;
}
return mFragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
String title;
switch (position) {
case 0:
title = "OBJECT " + (position + 1);
break;
case 1:
title = "OBJECT " + (position + 1);
break;
default:
title = "OBJECT " + (position + 1);
break;
}
return title;
}
}
希望它有所帮助。
答案 1 :(得分:1)
这段代码可以帮助你..
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager(), ActionBarActivity.this));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i2) {
}
@Override
public void onPageSelected(int i) {
actionBar.setSelectedNavigationItem(i);
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowHomeEnabled(true);
ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("Client");
tab1.setTabListener(this);
ActionBar.Tab tab2 = actionBar.newTab();
tab2.setText("Supplier");
tab2.setTabListener(this);
ActionBar.Tab tab3 = actionBar.newTab();
tab3.setText("Driver");
tab3.setTabListener(this);
ActionBar.Tab tab4 = actionBar.newTab();
tab4.setText("Owner");
tab4.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
actionBar.addTab(tab4);
}
class MyAdapter extends FragmentPagerAdapter{
Context context;
public MyAdapter(FragmentManager fragmentManager, Context context){
super(fragmentManager);
this.context = context;
}
@Override
public Fragment getItem(int i) {
Fragment fragment = null;
if(i == 0){
fragment = new FragmentClass1(context);
}else if(i == 1) {
fragment = new FragmentClass2(context);
}else if(i == 2){
fragment = new FragmentClass3(context);
}else {
fragment = new FragmentClass4(context);
}
return fragment;
}
@Override
public int getCount() {
return 4;
}
}
答案 2 :(得分:1)
此代码可以帮助您
viewPager = (ViewPager) view.findViewById(R.id.pager);
TabsPagerAdapter mAdapter = new TabsPagerAdapter(getChildFragmentManager(),itemsList);
viewPager.setAdapter(mAdapter);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
}
@Override
public void onPageScrolled(int position, float arg1, int arg2) {
pos = viewPager.getCurrentItem();
//System.out.println("On Page Scrolled :: " +position + " ### :" + arg2);
//System.out.println("On Page Scrolled :: pos : "+pos);
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});