在卡片视图中使用 ViewPager 显示 TabLayout 和片段

Displaying TabLayout and fragments with ViewPager inside card view

我想在 cardview 中显示两个片段,我能够显示 tablayout 但不能显示片段,尝试了所有方法,帮助我,因为我猜这可能很简单。我也上传了截图,从截图中可以看出,tablayout 显示正确,但下面的片段显示不正确

代码在 Activity.

 ....
     //Creating our pager adapter
            Pager adapter = new Pager(getSupportFragmentManager(), Titles, 2);
    ...
    viewPager = (ViewPager) findViewById(R.id.srviewpager);
            //Adding adapter to pager
            viewPager.setAdapter(adapter);


        final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setupWithViewPager(viewPager);

..
public class Pager extends FragmentStatePagerAdapter {

        CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
        int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
        private static final String TAG = "ViewPagerAdapter";

        // Build a Constructor and assign the passed Values to appropriate values in the class
        public Pager(FragmentManager fm, CharSequence mTitles[], int mNumbOfTabsumb) {
            super(fm);

            this.Titles = mTitles;
            this.NumbOfTabs = mNumbOfTabsumb;

        }

        //Overriding method getItem
        @Override
        public Fragment getItem(int position) {
            //Returning the current tabs
            switch (position) {
                case 0:
                    return new ShippingFragment();

                case 1:
                    return new ReturnsFragment();

            }
            return null;
        }

        // Overriden method getCount to get the number of tabs
        @Override
        public int getCount() {
            return NumbOfTabs;
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return Titles[position];
        }
    }

这是我的布局文件,包含3个cardview,最后一个cardview要显示两个片段,shipping和returns。到目前为止,我只能显示带有两个选项卡的 tablayout,但不能显示片段。

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

    <android.support.v7.widget.CardView
        android:id="@+id/description_cd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="8dp"
        cardview:cardCornerRadius="8dp"
        cardview:cardElevation="15dp"
        cardview:cardBackgroundColor="@color/cardview_light_background"
        cardview:contentPadding="2dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">



    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
    android:id="@+id/features_cd"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    cardview:cardCornerRadius="8dp"
    cardview:cardElevation="15dp"
    cardview:cardBackgroundColor="@color/cardview_light_background"
    cardview:contentPadding="2dp"
        android:layout_below="@+id/description_cd"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="false"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="false"
        android:layout_margin="8dp">



    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
    android:id="@+id/sp_cd"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    cardview:cardCornerRadius="8dp"
    cardview:cardElevation="15dp"
    cardview:cardBackgroundColor="@color/cardview_light_background"
    cardview:contentPadding="2dp"
        android:layout_below="@+id/features_cd"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="false"
        android:layout_margin="8dp">

    <com.spoorthy.apsaratrendz.thirdpartyviews.RippleView
        android:id="@+id/shipping_payment_rpv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        cardview:rv_color="@color/green"
        cardview:rv_type="rectangle"
        cardview:rv_rippleDuration="@integer/ripduration_rpv">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/shippingandreturns"
            android:id="@+id/textViewsp"
            android:layout_alignParentTop="true"
            android:singleLine="true"
            android:textSize="25sp"
            android:textStyle="bold"
            android:background="#FFFFFF"
            android:textColor="#d52e9e"
            android:inputType="text"
            android:padding="1dp"
            android:paddingEnd="1dp"
            android:paddingStart="1dp" />
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            android:layout_below="@+id/textViewsp"/>
        <android.support.v4.view.ViewPager
        android:id="@+id/srviewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
            android:layout_below="@+id/tabs"/>

    </com.spoorthy.apsaratrendz.thirdpartyviews.RippleView>

    </android.support.v7.widget.CardView>



    </RelativeLayout>

这是我的两个片段布局 shippingfragment.xml

<?xml version="1.0" encoding="utf-8"?>
<com.spoorthy.apsaratrendz.thirdpartyviews.ReadMoreTextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragmentText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:textStyle="bold"
        android:text="@string/returns"
        android:layout_margin="4dp"
    android:padding="4dp" />

returnsfragment.xml

<?xml version="1.0" encoding="utf-8"?>
<com.spoorthy.apsaratrendz.thirdpartyviews.ReadMoreTextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragmentText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:textStyle="bold"
        android:text="@string/returns"
        android:layout_margin="4dp"
    android:padding="4dp" />

还有我的两个片段 java 文件..

public class ReturnsFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.returnsfragment, null);

        return v;
    }

}

public class ShippingFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.shippingfragment, null);

        return v;
    }

}

通过将 CardView 布局高度更改为 350dp 而不是 match_parent 解决了问题。

<android.support.v7.widget.CardView
android:id="@+id/sp_cd"
android:layout_width="match_parent"
android:layout_height="350dp".../>