2

I have create Main_Activity with ViewPager with Android Studio that has a code like this

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 has 3 pages that work fine.My question is,if i want to create custom view like: Page 1: has 3 button 4 radiobutton Page 2: has 2 Button 2 TextView Page 3: has 1 button 2 Imageview

How to do it and where i should put the code in.Sorry for my grammar ,Thank you for all answer.

IZTR
  • 35
  • 1
  • 5
  • Go to this post [http://stackoverflow.com/questions/18413309/how-to-implement-a-viewpager-with-different-fragments-layouts](http://stackoverflow.com/questions/18413309/how-to-implement-a-viewpager-with-different-fragments-layouts) – M D Feb 28 '14 at 09:20
  • Thank you for your question i will check it out – IZTR Feb 28 '14 at 09:22

2 Answers2

4

A very simple implementation of your question in my other post.

How to use swipe gesture in a view pager's page with android?

In that post I have mention how you can customize every layout. In each layout you can add your own buttons or textviews or whatever you want to add. For example, in that post, Layout to put inside the ViewPager in MainLayout, instead of using that image view and all other things, you can add your own desired items.

I am sure that will solve you problem and also you can add as many layouts as you can and just make that change in the adapter like this:

private class MyPagerAdapter extends PagerAdapter {

Here can be your page count.. You can increase you page count depending on your layouts.

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 your layouts you have designed.

  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 .. :)

Community
  • 1
  • 1
mike20132013
  • 5,257
  • 3
  • 28
  • 41
3

In the following method implement like this in 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;
}
RajeshVijayakumar
  • 9,551
  • 11
  • 53
  • 78
  • Thank you for you question.I will test it. – IZTR Feb 28 '14 at 09:23
  • Thank you again.With your implement,mean i should create a fragment layout more than 1? If i want to use only 1 fragment and let it decide to create custom view can it possible?Thank you – IZTR Feb 28 '14 at 10:17
  • If you are representing different views then you have to create different fragments – RajeshVijayakumar Feb 28 '14 at 10:19