Android: How to create other views in ViewPager?
I created Main_Activity with ViewPager using Android Studio with the following code
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;
}
}
}
In this case, there are 3 pages that work fine. My question is if I want to create a custom view like: Page 1: has 3 buttons 4 radio buttons Page 2: has 2 buttons 2 TextView Page 3: has 1 button 2 Imageview
How to do it and where should I put the code. Sorry for my grammar, thank you all for your answers.
In my other post, the implementation of your problem is very simple.
How to use swipe gesture in view pager page in android?
In that article, I mentioned how to customize each layout. In each layout, you can add your own buttons or text views, or anything. For example, in that article, put the Layout inside the ViewPager of the MainLayout, instead of using that image view and all the other stuff, you can add the items you want.
I believe this will solve your problem and you can add as many layouts as you want, just make changes in the adapter like this:
private class MyPagerAdapter extends PagerAdapter {
This can be your page count. .You can increase the number of pages according to the layout.
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.
Here you can add a layout of your own design.
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;
}
}
}
Hope I answered your question.. :)