如何使用不同的片段/布局实现ViewPager当我启动一个实现viewpager的活动时,viewpager创建了各种片段。我想对每个片段使用不同的布局,但问题是ViewPager在最大时只显示了两个布局(在1之后的所有剩余片段上的第二个布局)。以下是快速活动它实现了viewpager:public class SwipeActivity extends FragmentActivity{
MyPageAdapter pageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe);
pageAdapter = new MyPageAdapter(getSupportFragmentManager());
ViewPager pager=(ViewPager)findViewById(R.id.pager);
pager.setAdapter(pageAdapter);
ActionBar bar = getActionBar();
bar.setDisplayHomeAsUpEnabled(true);
}
/**
* Custom Page adapter
*/
private class MyPageAdapter extends FragmentPagerAdapter
{
public MyPageAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public int getCount()
{
return 5;
}
@Override
public Fragment getItem(int position)
{
switch(position)
{
case 0: return new MyFragment();
case 1: return SecondFragment.newInstance("asdasd");
default : return RamFragment.newInstance("s");
}
}
}}下面是碎片public class MyFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle)
{
return paramLayoutInflater.inflate(R.layout.processorlayout, paramViewGroup, false);
}}我使用了像这样的5个片段,它们都有不同的布局,但视图页最多只显示了2个。
3 回答
Helenr
TA贡献1780条经验 获得超4个赞
public Fragment getItem(int position) { switch (position){ case 0: return new Fragment1(); case 1: return new Fragment2(); case 2: return new Fragment3(); case 3: return new Fragment4(); default: break; } return null;}
public class Fragment1 extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_one, container, false); return view; }}
- 3 回答
- 0 关注
- 732 浏览
添加回答
举报
0/150
提交
取消