2014-01-21 130 views
1

我對這個有點瘋狂。
我有Fragment,其中包含ViewPager。 我想要這個ViewPager包含一些Fragment。 (總是一樣的,但具有不同的參數)。
創建了Fragments(至少2首),但我的ViewPager內容保持空白,沒有任何動作。
我錯過了什麼嗎?片段中的ViewPager始終爲空

我FragmentActivity

public class TrackingActivity extends FragmentActivity { 
    static FragmentManager fragmentManager; 
    static FragmentTransaction fragmentTransaction; 

    static TrackingMensurationsFragment mensurationFragment; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_tracking); 

     fragmentManager = getSupportFragmentManager(); 
     fragmentTransaction = fragmentManager.beginTransaction(); 

     mensurationFragment = new TrackingMensurationsFragment(); 
     fragmentTransaction.add(R.id.fragmentContainerLL, mensurationFragment); 

     fragmentTransaction.commit(); 
    } 
} 

我TrackingMensurationsFragment片段:

public class TrackingMensurationsFragment extends Fragment { 
    View fragmentView; 
    ViewPager pager; 
    FragmentAdapter pageAdapter; 
    TrackingMensurationPagerAdapter viewAdapter; 
    Handler mHandler; 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     fragmentView = inflater.inflate(R.layout.fragment_tracking_mensurations, container, false); 

     // new setAdapterTask().execute(); 
     return fragmentView; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     pager = (ViewPager) getView().findViewById(R.id.pager); 
     String flux = ((MyApp) getActivity().getApplicationContext()).getWebResponse(18); 

     List<Fragment> fragments = new Vector<Fragment>(); 
     try { 
      JSONArray jArray = new JSONObject(flux).getJSONArray("Mensurations"); 
      for (int i = (jArray.length() - 1); i >= 0; i--) { 
       Bundle bundle = new Bundle(); 
       bundle.putString("ARM_SIZE", jArray.getJSONObject(i).getString("ARM_SIZE")); 
       bundle.putString("THIGH_SIZE", jArray.getJSONObject(i).getString("THIGH_SIZE")); 
       bundle.putString("HIP_SIZE", jArray.getJSONObject(i).getString("HIP_SIZE")); 
       bundle.putString("WAIST_SIZE", jArray.getJSONObject(i).getString("WAIST_SIZE")); 
       bundle.putString("DietData_Date", jArray.getJSONObject(i).getString("DietData_Date").substring(0, 5)); 
       fragments.add(Fragment.instantiate(getActivity(), TrackingMensurationPagerFragment.class.getName(), bundle)); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
pageAdapter = new FragmentAdapter(getChildFragmentManager(), fragments); 
     mHandler = new Handler(); 
     mHandler.post(new Runnable() { 
      @Override 
      public void run() { 
       pager.setAdapter(pageAdapter);  
      } 
     }); 
    } 
} 

隨着ViewPager中的XML:

<android.support.v4.view.ViewPager 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

我FragmentAdapter:

public class FragmentAdapter extends FragmentStatePagerAdapter{ 
    private List<Fragment> fragments; 

    public FragmentAdapter(FragmentManager fm, List<Fragment> fragments) { 
     super(fm); 
     this.fragments = fragments; 
     Log.d("FragmentAdapter","constructor"); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     Log.d("FragmentAdapter","getItem "+position); 
     return this.fragments.get(position); 
    } 

    @Override 
    public int getCount() { 
     Log.d("FragmentAdapter","getCount: "+this.fragments.size()); 
     return this.fragments.size(); 
    } 

    @Override 
    public int getItemPosition(Object object){ 
     return PagerAdapter.POSITION_NONE; 
    } 
} 

我logcat的輸出:

d/FragmentAdapter(30805):構造d/FragmentAdapter(30805): getCount將:12 d/dflkjghkldfjghdfkjg(30805):qgsdhgklsqdjghjdfskg d/FragmentAdapter(30805):getCount將:D/FragmentAdapter(30805): getItem 0 D/FragmentAdapter(30805):getItem 1 D/FragmentAdapter(30805):getCount:12 D/dalvikvm(30805):GC_FOR_ALLOC 已釋放322K,30%免費12905K/18311K ,暫停30ms,總共30ms I/dalvikvm-heap(30805): 737536字節分配D/dalvikvm(30805):GC_堆(碎片情況)增加到14.546MB:GC_ FOR_ALLOC釋放1K,29% 免費13624K/19079K,暫停20毫秒,20毫秒總 d/TrackingMensurationPagerFragment(30805):日期:02/11 d/TrackingMensurationPagerFragment(30805):日期:30/09 d/FragmentAdapter(30805) :getCount:12 D/FragmentAdapter(30805): getCount:12 D/FragmentAdapter(30805):getCount:12 D/FragmentAdapter(30805):getCount:12 D/FragmentAdapter(30805): getCount:12 D/FragmentAdapter (30805):getCount:12 D/FragmentAdapter(30805):getCount:12 D/FragmentAdapter(30805): getCount:12 D/FragmentAdapter(30805):getCount:12 D/FragmentAdapter(30805):getCount:12

編輯:我而片段的XML:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FFFFFF" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:padding="10dp" > 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/mettreAJourTV" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:layout_centerVertical="true" 
       android:background="@drawable/btn_vert_degrade_fort" 
       android:padding="10dp" 
       android:text="@string/maj_caps" 
       android:textColor="#FFFFFF" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_marginRight="10dp" 
       android:layout_toLeftOf="@id/mettreAJourTV" 
       android:text="@string/mensurations" 
       android:textColor="#d33274" 
       android:textSize="17dp" /> 
     </RelativeLayout> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="5dp" 
      android:text="@string/infos_perso_evolution_poids" /> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

    </LinearLayout> 

</ScrollView> 
+0

http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html。有一個例子 – Raghunandan

+0

@Raghunandan在這個例子中,ViewPager在FragmentActivity中,我的是在Fragment中。在ACtivity/FragmentActivity中,我沒有任何問題。 –

+0

你有一個viewpager在片段? – Raghunandan

回答

1

在我的情況下,問題是,我Fragment根元素,這是ViewPager的母公司,是ScrollView。看起來像一個ViewPager不能包含在ScrollView