我不知道这是一个错误,还是我不应该以这种方式做事,但我试图在一个页面上包含两次viewpager而第二个viewpager永远不会出现。
ActivityMain.java
//These are my two includes in activity_main
View includeOne = findViewById(R.id.activity_main_include_primary);
View includeTwo = findViewById(R.id.activity_main_include_secondary);
//Set viewpager one
ViewPager pagerOne = (ViewPager) includeOne.findViewById(R.id.pager_viewpager);
CustomAdapter adapterOne = new CustomAdapter(getSupportFragmentManager());
pagerOne.setAdapter(adapterOne);
//set viewpager two
ViewPager pagerTwo = (ViewPager) includeTwo.findViewById(R.id.pager_viewpager);
CustomAdapter adapterTwo = new CustomAdapter(getSupportFragmentManager());
pagerTwo.setAdapter(adapterTwo);
activity_main.xml中
<include
android:id="@+id/activity_main_include_primary"
layout="@layout/pager"/>
<include
android:id="@+id/activity_main_include_secondary"
layout="@layout/pager"/>
pager.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager_viewpager"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
CustomAdapter.java
public class CustomAdapter extends FragmentPagerAdapter {
public CustomAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 2;
}
@Override
public Fragment getItem(int position) {
switch(position){
case 0:
return FragmentOne.newInstance();
default:
return FragmentTwo.newInstance();
}
}
}
这两个片段是标准的。
我一整天都在尝试这个,除非我放弃包含(我不想这样做),否则我无法让第二个观看者显示出来。我使用的任何其他android视图都可以。
非常感谢任何帮助。
答案 0 :(得分:0)
如果您想要展示2个观看者,则需要有2个不同的取景器,并将它们设置在另一个之上。
在activity_main.xml中:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.viewpager_test.app.MainActivity">
<include
android:id="@+id/activity_main_include_primary"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/pager_one"/>
<include
android:id="@+id/activity_main_include_secondary"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/activity_main_include_primary"
layout="@layout/pager_two"/>
</RelativeLayout>
对于pager_one.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager_viewpager_one"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
对于pager_two.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager_viewpager_two"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
并在ActivityMain.java文件中将pagerOne设置为viewpager_one id,将pagerTwo设置为viewpager_two id:
ViewPager pagerOne = (ViewPager) includeOne.findViewById(R.id.pager_viewpager_one);
...
ViewPager pagerTwo = (ViewPager) includeTwo.findViewById(R.id.pager_viewpager_two);
...