onCreateView没有被称为tab片段

时间:2017-04-26 06:24:42

标签: android android-fragments android-tablayout fragment-oncreateview

我目前有这个标签视图,我从this教程开始。一切正常,直到我注意到任何标签上都没有调用CONSTRAINT FOREIGN KEY fk_address_owner ADDRESS.owner_id REFERENCES (which table?) (id) 。我在这里和那里寻找解决方案,但我仍然无法解决。 这是我的代码:

onCreateView

activity_pref_main_container.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/main_layout" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".PrefActivityMain"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:background="?attr/colorPrimary" android:elevation="6dp" android:minHeight="?attr/actionBarSize" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/toolbar" android:background="?attr/colorPrimary" android:elevation="6dp" android:minHeight="?attr/actionBarSize" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_below="@id/tab_layout"/> </RelativeLayout>

PrefMainActivity.class

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pref_main_container); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle(R.string.preference_title); setSupportActionBar(toolbar); new PrefActivityListAsync(this,this,specialization).execute(); new PrefActivityListAsync(this,this,position).execute(); new PrefActivityListAsync(this,this,type).execute(); TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); tabLayout.addTab(tabLayout.newTab().setText("Specialization")); tabLayout.addTab(tabLayout.newTab().setText("Position")); tabLayout.addTab(tabLayout.newTab().setText("Type")); tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); final PrefAdapter adapter = new PrefAdapter(getSupportFragmentManager(), tabLayout.getTabCount()); viewPager.setAdapter(adapter); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); viewPager.setOffscreenPageLimit(2); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); }

PrefAdapder.class

三分之一的片段类 public class PrefAdapter extends FragmentPagerAdapter { int mNumOfTabs; public PrefAdapter(FragmentManager fm, int NumOfTabs) { super(fm); this.mNumOfTabs = NumOfTabs; } @Override public Fragment getItem(int position) { switch (position) { case 0: PrefActivitySpecialization tab1 = new PrefActivitySpecialization(); return tab1; case 1: PrefActivityPosition tab2 = new PrefActivityPosition(); return tab2; case 2: PrefActivityType tab3 = new PrefActivityType(); return tab3; default: return null; } } @Override public int getCount() { return 0; } }

PrefActivitySpecialization.class

4 个答案:

答案 0 :(得分:6)

您应该在两个地方进行更改:

1)改变

--collapse

@Override
public int getCount() {
return 0;
}

2)改变

@Override
public int getCount() {
return mNumOfTabs;
}

return inflater.inflate(R.layout.activity_pref_specialization_container, container, false);

答案 1 :(得分:2)

  

public PrefAdapter(FragmentManager fm,int NumOfTabs)

您必须声明Count No NumOfTabs

  

int getCount() - &gt;由此表示的数据集中有多少项   适配器。

 @Override
public int getCount() 
{
    return mNumOfTabs;
}

不要 '

return inflater.inflate(R.layout.activity_pref_specialization_container, container, false);

<强> 待办事项

return view ;

答案 2 :(得分:1)

您的getCount()方法返回0;

答案 3 :(得分:0)

还有另一种(可能更简单的方法)将视图寻呼机与标签布局连接起来 -

首先,在您的PrefAdapter课程中,覆盖getPageTitle()方法,然后从那里返回相应标签位置的标题标题。

PrefMainActivity课程中,删除以下所有行:     tabLayout.addTab(...)

添加以下行代替 -     tabLayout.setupWithViewPager(viewPager);