简体   繁体   中英

ViewPager doesn't show Fragment

I'm trying to implement a Slinding Menu by using Pager Sliding Tab Strip (com.astuetz:pagerslidingtabstrip). I set up my xml layout of the main screen:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight=".97">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/logo_b"
        android:src="@drawable/logo_b"
        android:layout_weight=".05"
        android:layout_gravity="right"
        android:layout_marginRight="-15dp" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollMe"
        android:layout_weight=".95"
        android:fillViewport="false">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal">

            <com.astuetz.PagerSlidingTabStrip
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="48dip"
                app:pstsIndicatorColor="#19B5FE"
                app:pstsShouldExpand="true"
                app:pstsTextAllCaps="false"
                app:pstsIndicatorHeight="1dp"
                />


            <android.support.v4.view.ViewPager
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </LinearLayout>
    </ScrollView>
</LinearLayout>

This xml is included into another View, but it handle the View Pager.
To handle the init of ViewPager and PagerSlidingTapStrip i have a FragmentActivity where during onCreate i have:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sliding_menu);

    pager = (ViewPager) findViewById(R.id.pager);
    CustomPagerAdapteradapter = new  CustomPagerAdapter(getSupportFragmentManager());
    adapter.setContext(getApplicationContext());
    pager.setAdapter(adapter);

    tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabs.setViewPager(pager);
}

And the adapter is:

public static class CustomPagerAdapteradapter extends FragmentPagerAdapter implements IconTabProvider {
    private int[] ICONS = {
            R.drawable.icon_menu0,
            R.drawable.icon_menu1,
            R.drawable.icon_menu2,
            R.drawable.icon_menu3,
            R.drawable.icon_menu4
    };
    private static Context CONTEXT;
    private Fragment fragment[] = new Fragment[5];

    public CustomPagerAdapteradapter (FragmentManager fm) {
        super(fm);
        for(int i = 0; i < 5; ++i) {
            fragment[i] = Profile.newInstance();
        }
    }

    public void setContext(Context c) { CONTEXT = c; }

    @Override
    public int getCount() {
        return ICONS.length;
    }

    @Override
    public Fragment getItem(int position) {
        Toast.makeText(CONTEXT, "" + position, Toast.LENGTH_SHORT).show();
        switch (position) {
            case 0:
                return fragment[position];
            case 1:
                return fragment[position];
            case 2:
                return fragment[position];
            case 3:
                return fragment[position];
            case 4:
                return fragment[position];
            case 5:
                return fragment[position];
            default:
                return fragment[position];
        }
    }

    @Override
    public int getPageIconResId(int position) {
        return ICONS[position];
    }
}

The Profile Fragment xml is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">


<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight=".90">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_weight=".20">

        <!-- Not filled yet -->

    </RelativeLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".70"
        android:background="@drawable/radius_background"
        android:layout_margin="15dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="15dp"
            android:textColor="#FFF"
            android:textSize="16dp"
            android:text="info@example.com"
            android:id="@+id/user_mail" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="5dp"
            android:textColor="#FFF"
            android:textSize="16dp"
            android:text="15/10/54"
            android:id="@+id/user_birth" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="5dp"
            android:textColor="#FFF"
            android:textSize="16dp"
            android:text="Italy"
            android:id="@+id/user_country" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="5dp"
            android:textColor="#FFF"
            android:textSize="16dp"
            android:text="Pa"
            android:id="@+id/user_town_short" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="5dp"
            android:textColor="#FFF"
            android:textSize="16dp"
            android:text="Palermo"
            android:id="@+id/user_town" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="5dp"
            android:textColor="#FFF"
            android:textSize="16dp"
            android:text="Male"
            android:id="@+id/user_sex" />

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_weight=".05">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:textColor="#FFF"
            android:background="#18333E"
            android:text="Edit/Update Data"
            android:id="@+id/updateData"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>

   </LinearLayout>
</LinearLayout>

Profile fragment onCreateView just inflate the xml:

return inflater.inflate(R.layout.fragment_profile, container, false);

Even if the code seems to be ok, I never see profile on the View Pager.
What's wrong?

最终答案:删除ScrollView,因为它已被ViewPager阻止。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM