我有一個活動,其內容視圖是一個片段,我添加viewpager和viewpagerindicator這個活動。我爲尋呼機創建了一個FragmentPagerAdapter,適配器構造了三個片段用於顯示三個選項卡。但是當我運行這個項目時,只能看到頁面標題顯示出來。爲什麼這三個片段沒有出現?爲什麼片段不顯示與ViewPagerIndicator
我在創建Activity生命週期時創建了尋呼機和指示器。
public class BrowserActivity extends SherlockFragmentActivity
implements ReposFragment.OnFileSelectedListener, StarredFragment.OnStarredFileSelectedListener, OnBackStackChangedListener {
super.onCreate(savedInstanceState);
setContentView(R.layout.seadroid_main);
adapter = new SeafileTabsAdapter(getSupportFragmentManager());
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
indicator.setViewPager(pager);
indicator.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(final int position) {
currentPosition = position;
supportInvalidateOptionsMenu();
disableUpButton();
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
});
適配器構建用於顯示三個片段三個選項卡瀏覽
class SeafileTabsAdapter extends FragmentPagerAdapter implements
IconPagerAdapter {
public SeafileTabsAdapter(FragmentManager fm) {
super(fm);
}
private ReposFragment reposFragment = null;
private ActivitiesFragment activitieFragment = null;
private StarredFragment starredFragment = null;
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
if (reposFragment == null) {
reposFragment = new ReposFragment();
}
return reposFragment;
case 1:
if (starredFragment == null) {
starredFragment = new StarredFragment();
}
return starredFragment;
case 2:
if (activitieFragment == null) {
activitieFragment = new ActivitiesFragment();
}
return activitieFragment;
default:
return new Fragment();
}
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.tabs_library).toUpperCase();
case 1:
return getString(R.string.tabs_starred).toUpperCase();
case 2:
return getString(R.string.tabs_activity).toUpperCase();
default:
return null;
}
}
@Override
public int getIconResId(int index) {
return ICONS[index];
}
@Override
public int getCount() {
return ICONS.length;
}
}
佈局文件seadroid_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.viewpagerindicator.TabPageIndicator
android:id="@+id/indicator"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
UPDATE
我發現ViewPager教程使用LineaLayout作爲根容器。那麼是否有必要在這裏將FragmentLayout更改爲LineaLayout。當我改LineaLayout,應用程序崩潰,並且日誌是
12-02 07:01:13.636: E/AndroidRuntime(3807): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.seafile.seadroid2/com.seafile.seadroid2.ui.activity.BrowserActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class LineaLayout
12-02 07:01:13.636: E/AndroidRuntime(3807):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
感謝您的意見,但我認爲我不能將根視圖更改爲Linealayout,因爲我使用了SherlockFragmentActivity。上面的代碼段並沒有顯示SherlockFragmentActivity方法的細節依賴,比如getSupportFragmentManager。你可以在這裏看到完整的代碼(https://github.com/haiwen/seadroid/blob/master/src/com/seafile/seadroid2/ui/activity/BrowserActivity.java)。 – 2014-12-02 05:43:43
我想你誤解了我。我不希望你改變你的活動類,我希望你改變你的seadroid_main.xml文件。我已將修改的XML添加到我的答案中... – Bruce 2014-12-02 05:54:27
我現在明白你的意思,但仍然無法使用Linealayout,應用程序崩潰。 – 2014-12-02 06:47:03