我使用带有Android Studio的ViewPager创建了Main_Activity,其代码如下
public class MainActivity extends Activity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
在这种情况下有3页工作正常。我的问题是,如果我想创建自定义视图,如: 第1页:有3个按钮4个radiobutton 第2页:有2个按钮2 TextView 第3页:有1个按钮2 Imageview
怎么做以及我应该把代码放在哪里。请写我的语法,谢谢你的答案。
答案 0 :(得分:3)
在getItem()中的以下方法实现:
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch(position) {
case 0 :
fragment = new MatchFragment();
break;
case 1 :
fragment = new Fragment1();
break;
case 2 :
fragment = new MFragment();
break;
}
return fragment;
}
答案 1 :(得分:3)
在我的其他帖子中非常简单地实现您的问题。
How to use swipe gesture in a view pager's page with android?
在那篇文章中,我提到了如何自定义每个布局。在每个布局中,您可以添加自己的按钮或文本视图或任何您想要添加的内容。例如,在该帖子中,布局要放在MainLayout中的ViewPager中,而不是使用该图像视图和所有其他内容,您可以添加自己想要的项目。
我相信这会解决您的问题,您也可以添加尽可能多的布局,只需在适配器中进行更改,如下所示:
private class MyPagerAdapter extends PagerAdapter {
这可以是您的页数。您可以根据布局增加页数。
public int getCount() {
return 3;
}
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Using different layouts in the view pager instead of images.
int resId = -1;
//Getting my layout's in my adapter. Three layouts defined.
您可以在此添加您设计的布局。
switch (position) {
case 0:
resId = R.layout.tutorial1;
break;
case 1:
resId = R.layout.tutorial2;
break;
case 2:
resId = R.layout.tutorial3;
break;
}
View view = inflater.inflate(resId, container, false);
((ViewPager) container).addView(view, 0);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
}
希望我回答你的问题.. :)