【发布时间】:2015-07-10 08:17:09
【问题描述】:
我正在尝试在 ScrollView 中使用两个 ViewPager 创建布局。第一个已将高度设置为所需的大小,第二个应与布局的其余部分匹配。问题是第二个 ViewPager 不可见。我究竟做错了什么?这是我的布局:
<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>
【问题讨论】:
标签: android android-layout android-viewpager