我认为要在多个内容上显示相同类型的视图,您应该将片段与 viewpager 一起使用。
我只是向您推荐 viewpager 和片段的代码 -
内容布局(pages.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"
tools:context=".MainActivity"
android:id="@+id/rel" >
<TextView
android:id="@+id/pageContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
主要活动内容(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"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/relativeTextview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/header"
android:padding="5dp" >
<android.support.v4.view.ViewPager
android:id="@+id/reviewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
</RelativeLayout>
用于在viewpager上设置的分页器适配器(ViewPagerAdapter.java)-
public class ViewPagerAdapter extends PagerAdapter {
int size;
Activity act;
View layout;
TextView pagecontent;
Button click;
public ViewPagerAdapter(MainActivity mainActivity, int noofsize) {
// TODO Auto-generated constructor stub
size = noofsize;
act = mainActivity;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return size;
}
@Override
public Object instantiateItem(View container, int position) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.pages, null);
pagecontent = (TextView) layout.findViewById(R.id.pageContent);
int pagenumberTxt=position + 1;
pagecontent.setText("Now your in Page No " +pagenumberTxt );
((ViewPager) container).addView(layout, 0);
return layout;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
// }
}
现在在主要活动中((MainActivity.java)-
public class MainActivity extends Activity {
int noofsize = 6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPagerAdapter adapter = new ViewPagerAdapter(MainActivity.this,
noofsize);
ViewPager myPager = (ViewPager) findViewById(R.id.reviewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
并且不要忘记在 manufest.xml 中提及活动。
在 pages.xml 中,您可以在单击此按钮时添加按钮,您可以使用侦听器更改下一个和上一个(mPager.setCurrentItem(位置))的视图。