我想創建兩個ViewPager內部滾動查看佈局。第一個將高度設置爲所需的大小,第二個應與其餘佈局匹配。問題是第二個ViewPager不可見。我究竟做錯了什麼?這是我的佈局:兩個ViewPagers裏面滾動查看 - 第二不可見
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="200dp" />
<android.support.v4.view.ViewPager
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" />
</LinearLayout>
</ScrollView>
</FrameLayout>
我使用第二ViewPager顯示片段(使用FragmentPagerAdapter),這是我的FragmentPagerAdapter代碼:
public class MainPagerAdapter extends FragmentPagerAdapter {
public MainPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FirstFragment();
case 1:
return new SecondFragment();
default:
return new FirstFragment();
}
}
@Override
public int getCount() {
return 2;
}
}
代碼FirstFragment的:
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.first_step, container, false);
}
}
first_step.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@android:color/holo_blue_light" />
<View
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@android:color/holo_green_light" />
</LinearLayout>
一切都沒變,只有第一個是可見的。 – user3626048
我編輯了我的文章。問題是你把一個帶有「match_parent」高度的LinearLayout放到一個ScrollView中。 – Gordak
感謝您的回覆。當我將第二個ViewPager的高度設置爲某個尺寸時,它就可以看到,但我想將它匹配到佈局的其餘部分。 – user3626048