我已经把我之前的问题改成了这个。我找到了ListView ViewPager 的示例,但我的问题是该示例以编程方式添加了 ListView,我想要的内容已经在我的 xml 中设置。
示例代码:
主要活动:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.activity_main);
ListView listview1 = new ListView(mContext);
ListView listview2 = new ListView(mContext);
ListView listview3 = new ListView(mContext);
Vector<View> pages = new Vector<View>();
pages.add(listview1);
pages.add(listview2);
pages.add(listview3);
ViewPager vp = (ViewPager) findViewById(R.id.viewpager);
CustomerPagerAdapter adapter = new CustomerPagerAdapter(mContext, pages);
vp.setAdapter(adapter);
listview1.setAdapter(new ArrayAdapter<String>(mContext,
android.R.layout.simple_list_item_1, new String[] { "A1", "B1",
"C1", "D1" }));
listview2.setAdapter(new ArrayAdapter<String>(mContext,
android.R.layout.simple_list_item_1, new String[] { "A2", "B2",
"C2", "D2" }));
listview3.setAdapter(new ArrayAdapter<String>(mContext,
android.R.layout.simple_list_item_1, new String[] { "A3", "B3",
"C3", "D3" }));
}
}
客户页面适配器:
public class CustomerPagerAdapter extends PagerAdapter {
private Context mContext;
private Vector<View> pages;
public CustomerPagerAdapter(Context context, Vector<View> pages) {
this.mContext = context;
this.pages = pages;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View page = pages.get(position);
container.addView(page);
return page;
}
@Override
public int getCount() {
return pages.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
活动主:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
如何使用此代码在 XML 中设置 ListView?