这是我的布局,我在ScrollView中添加了Tablayout和ViewPager。
EveryThing没问题,但是viewPager片段没有显示任何内容。
这是我的xml布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#252525"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar">
<RelativeLayout
android:id="@+id/rel"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/poster"
android:layout_width="96dp"
android:layout_height="128dp"
android:layout_alignParentRight="true"
android:layout_margin="8dp"
android:background="#eee"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/txt_onvan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/poster"
android:layout_marginBottom="8dp"
android:layout_toLeftOf="@id/poster"
android:text="عنوان" />
<TextView
android:id="@+id/txt_sazande"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txt_onvan"
android:layout_marginBottom="8dp"
android:layout_toLeftOf="@id/poster"
android:text="سازنده" />
<TextView
android:id="@+id/txt_nazar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txt_sazande"
android:layout_toLeftOf="@id/poster"
android:text="تعداد نظر" />
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_below="@id/poster" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tablayout"
/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
这是我的ViewPager适配器:
package ir.dink.cordinatorexample;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PagerAdapter extends FragmentStatePagerAdapter {
//============================================ Constructor
public PagerAdapter(FragmentManager fm) {
super(fm);
}
//============================================ GetItem Method ()
@Override
public Fragment getItem(int position) {
Fragment frag = null;
switch (position) {
case 0:
frag = new FragmentOne();
break;
case 1:
frag = new FragmnentTwo();
break;
}
return frag;
}
//============================================= GetCount Method ()
@Override
public int getCount() {
return 2;
}
//============================================= GetPageTitle
@Override
public CharSequence getPageTitle(int position) {
String title = "";
switch (position) {
case 0:
title = "First";
break;
case 1:
title = "Second";
break;
}
return title;
}
}
这是屏幕截图: