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.

2

2 Answers 2

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

6
  • No problem.. M glad this answer was helpful. Mar 1, 2014 at 17:17
  • Is View Pager is suitable for make questionnaire? can it kept value to SQlite?
    – IZTR
    Mar 1, 2014 at 17:40
  • Why not.. you can use view pager in a different way.. not necessarily you just define your layouts. For example, you can have infinite number of views depending on your response you get from sqlite or json or it can be anything. Now in that case, you can set the count as your array list in which you will be getting the values. But yes, answering your question, you can use it for making questionnaires. Mar 1, 2014 at 17:43
  • What exactly you want to do ? Mar 1, 2014 at 17:47
  • Thank you very much.I am on it but i want someone guarantee that will work.Thank you again.
    – IZTR
    Mar 1, 2014 at 18:25
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;
}
2
  • 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, 2014 at 10:17
  • If you are representing different views then you have to create different fragments Feb 28, 2014 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.