【发布时间】:2015-02-08 16:41:35
【问题描述】:
使用 Android Developer 示例中的 SlidingTabsBasicFragment 示例,我有 3 个选项卡,我想在其中显示不同的内容。当前的问题是,在单击或滑动到 3 个选项卡中的任何一个时,它只是加载相同的默认 pager_item .xml,显示“Page”和“Page Number”
我知道我应该让每个选项卡完全加载不同的 xml。哪位好心人可以告诉我如何做到这一点?
class SamplePagerAdapter extends PagerAdapter {
/**
* Sets the Titles for the various tabs
*/
private String[] titles={"Browse", "Activity", "Me"};
/**
* @return the number of pages to display
*/
@Override
public int getCount() {
return 3;
}
/**
* @return true if the value returned from {@link #instantiateItem(ViewGroup, int)} is the
* same object as the {@link View} added to the {@link ViewPager}.
*/
@Override
public boolean isViewFromObject(View view, Object o) {
return o == view;
}
// BEGIN_INCLUDE (pageradapter_getpagetitle)
/**
* Return the title of the item at {@code position}. This is important as what this method
* returns is what is displayed in the {@link SlidingTabLayout}.
* <p>
* Here we construct one using the position value, but for real application the title should
* refer to the item's contents.
*/
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
// END_INCLUDE (pageradapter_getpagetitle)
/**
* Instantiate the {@link View} which should be displayed at {@code position}. Here we
* inflate a layout from the apps resources and then change the text view to signify the position.
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Inflate a new layout from our resources
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_main_menu,
container, false);
// Add the newly created View to the ViewPager
container.addView(view);
// Retrieve a TextView from the inflated View, and update it's text
// TextView title = (TextView) view.findViewById(R.id.item_title);
// title.setText(String.valueOf(position + 1));
// Return the View
return view;
}
/**
* Destroy the item from the {@link ViewPager}. In our case this is simply removing the
* {@link View}.
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
我知道这里必须做一些事情才能达到结果。
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Inflate a new layout from our resources
View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item,
container, false);
// Add the newly created View to the ViewPager
container.addView(view);
// Retrieve a TextView from the inflated View, and update it's text
TextView title = (TextView) view.findViewById(R.id.item_title);
title.setText(String.valueOf(position + 1));
// Return the View
return view;
}
【问题讨论】:
标签: android