我创建了一个包含三个“页面”的 ViewPager。代码是这样的
MainActivity.java
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.pager_tab_strip);
FragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(
getSupportFragmentManager());
viewPager.setAdapter(fragmentPagerAdapter);
pagerTabStrip.setDrawFullUnderline(true);
pagerTabStrip.setTabIndicatorColor(Color.DKGRAY);
}
}
MyFragmentPageAdapter.java
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private String[] pageTitle = {
"Page1", "Page2", "Page3"
};
public MyFragmentPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new PageFragment();
Bundle arguments = new Bundle();
arguments.putString("pageIndex", Integer.toString(position + 1));
fragment.setArguments(arguments);
return fragment;
}
@Override
public int getCount() {
return pageTitle.length;
}
@Override
public CharSequence getPageTitle(int position) {
return pageTitle[position];
}
}
PageFragment.java
public class PageFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_page, null);
return view;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.support.v4.view.PagerTabStrip
android:id="@+id/pager_tab_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33B5E5"
android:textColor="#FFFFFF"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
</android.support.v4.view.ViewPager>
</RelativeLayout>
片段页面.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:textSize="16sp"
android:textStyle="italic"
android:gravity="center_horizontal"
android:textColor="@color/red"
android:text="@string/inf" />
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="60dp"
android:textSize="28sp"
android:gravity="center_horizontal"
android:textStyle="bold"
android:text="@string/ben" />
<TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="130dp"
android:gravity="center_horizontal"
android:textSize="18sp"
android:text="Prova"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_centerInParent="true"
android:text="@string/verifica" />
</RelativeLayout>
但现在我在所有三个页面中都看到了同样的事情。例如,如果我在第 2 页上我想要一个带有文本“这是第 2 页”的 TextView,在第三页上是一个带有文本“这是第 3 页”的 TextView,在第一页中是两个带有按钮的 TextView.. 。 我怎样才能?我快疯了,请把代码传给我来做这件事。请。