我想在cardview中显示两个片段,我能够显示tablayout而不是片段,尝试了一切,帮助我,因为这可能很简单我想。我也上传了屏幕截图,如屏幕截图所示,tablayout显示正确,但不是下面的片段
活动中的代码。
....
//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必须显示两个片段,发货和退货。到目前为止,我只能显示带有两个选项卡的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;
}
}
答案 0 :(得分:1)
通过将CardView布局高度更改为350dp而不是match_parent来解决问题。
<android.support.v7.widget.CardView
android:id="@+id/sp_cd"
android:layout_width="match_parent"
android:layout_height="350dp".../>