我对此感到有些疯狂。
我有一个包含FragmentViewPager
我希望此ViewPager包含一些Fragment。 (始终相同,但参数不同)。
Fragments已创建(至少是第2个),但是我的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);
            }
        });
    }
}


使用xml中的ViewPager:

<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:12 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):将堆(片段大小写)增加到14.546MB
  737536字节分配D / dalvikvm(30805):GC_FOR_ALLOC已释放1K,29%
  免费13624K / 19079K,暂停20ms,总计20ms
  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


编辑:我的Fragment的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>

最佳答案

在我的情况下,问题是我的Fragment的根元素(是ViewPager的父元素)是ScrollView。看来ViewPager不能包含在ScrollView中。

10-07 22:24