片段中的片段ViewPager始终为空白

时间:2014-01-21 10:43:17

标签: android android-fragments android-viewpager

我在这一点上变得有点疯狂 我有Fragment,其中包含ViewPager。 我希望此ViewPager包含一些Fragment。 (始终相同,但参数不同) 创建了Fragments(至少是2个第一),但我的ViewPager内容保持空白,没有任何移动。
我错过了什么吗?

My 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" />

My 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):将堆(frag case)增长到14.546MB for   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

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

1 个答案:

答案 0 :(得分:1)

在我的案例中,问题是我的Fragment的根元素是ViewPager的父元素,是ScrollView。看起来像ViewPager不能包含在ScrollView