2013-07-08 43 views
1

事情是這樣的:我fragmentpageradapter時,這一佈局被裝載在縱向正常工作:FragmentPagerAdapter實例的所有片段

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/mainLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 

    <com.viewpagerindicator.TitlePageIndicator 
     android:id="@+id/indicator" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="10dip" 
     app:linePosition="top" 
     app:selectedColor="#000000" /> 

</LinearLayout> 

它實例化的項目懶洋洋,當它需要他們只

但隨後,當我切換到橫向和加載這個佈局(基本上只是把右邊的列表):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:id="@+id/mainLayout" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="2" 
     android:orientation="vertical" > 

     <android.support.v4.view.ViewPager 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 

     <com.viewpagerindicator.TitlePageIndicator 
      android:id="@+id/indicator" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="10dip" 
      app:linePosition="top" 
      app:selectedColor="#000000" /> 
    </LinearLayout> 

    <View 
     android:layout_width="1dp" 
     android:layout_height="fill_parent" 
     android:background="@color/light_grey" /> 

    <ListView 
     android:id="@+id/listStoryView" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" > 
    </ListView> 

</LinearLayout> 

事情變得一團糟

我的適配器實例化所有30個項目!並在每個頁面更改所有30個片段獲取他們的onCreateView調用。

這是堅果!我究竟做錯了什麼?

這裏是我的適配器代碼:

package ie.breakingnews.mobile.adapters; 

import ie.breakingnews.mobile.R; 
import ie.breakingnews.mobile.fragments.ArticleFragment; 
import ie.landmarkdigital.shared.models.Article; 

import java.util.List; 
import java.util.Locale; 

import android.content.Context; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.util.Log; 

public class ArticlePagerAdapter extends FragmentPagerAdapter { 

    private final List<Article> articles; 
    private final boolean hideTime; 
    private int selected; 

    private final String of; 
    private final String prev; 
    private final String next; 

    private final String ads; 

    public ArticlePagerAdapter(FragmentManager fm, List<Article> articles, boolean hideTime, Context context, String ads) { 
     super(fm); 

     this.articles = articles; 
     this.hideTime = hideTime; 

     this.of = context.getString(R.string.of).toUpperCase(Locale.ENGLISH); 
     this.prev = context.getString(R.string.previous).toUpperCase(Locale.ENGLISH); 
     this.next = context.getString(R.string.next).toUpperCase(Locale.ENGLISH); 

     this.ads = ads; 
    } 

    @Override 
    public Fragment getItem(int arg0) { 
     Log.e("TCH", "creating an articlefragment for position: " + arg0); 
     return ArticleFragment.newInstance(articles.get(arg0), hideTime, ads); 
    } 

    @Override 
    public int getCount() { 
     return articles != null ? articles.size() : 0; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     if (position == selected) { 
      return String.format(Locale.getDefault(), "%d %s %d", position + 1, of, getCount()); 
     } else if (position + 1 == selected) { 
      return prev; 
     } else if (position - 1 == selected) { 
      return next; 
     } 
     return super.getPageTitle(position); 
    } 

    public void setSelected(int selected) { 
     this.selected = selected; 
    } 
} 
+0

只是一個方面說明,你不應該再在第一個佈局的'ViewPager'上使用'fill_parent'。改用'match_parent'。 – Andre

回答

2

在你的第一個佈局,ViewPager的母公司擁有的match_parent的寬度,這看起來是正確的。

在你的風景佈局中,ViewPager的父母的寬度爲0dp。這可能導致所有項目都被實例化的問題?

+0

!必須明確指定我的佈局寬度爲大型平板電腦爲700 dp,而大型平板電腦爲900dp。 謝謝! –

相關問題