Android: How to create other views in ViewPager?


IZTR

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.

mike20132013

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

Related


How to create tabs dynamically using ViewPager in Android?

Milan Gaella explain: Suppose I have multiple categories from REST. I didn't know it had 5 categories, sometimes 2, etc. I want to put all categories on my labels using viewpager. I want to create a tab based on category size. Let's take a look, for example, l

How to create tabs dynamically using ViewPager in Android?

Milan Gaella: illustrate: Suppose I have multiple categories from REST. I didn't know it had 5 categories, sometimes 2, etc. I want to use viewpager to put all taxonomy on my labels. I want to create a tab based on category size. Let's take a look, for example

How to Create Panorama 360 Views in Android

Ajay Prasad Is it possible to use Panorama 360 View in iOS, also in Android? Can anyone let me know how to create Panorama 360 view in android. Harveys Rush Features that you must see supported in the 0.2 beta of the PanaromaGl library for Android include: SDK

How to Create Panorama 360 Views in Android

Ajay Prasad Is it possible to use Panorama 360 View in iOS, also in Android? Can anyone let me know how to create Panorama 360 view in android. Harveys Rush Features that you must see supported in the 0.2 beta of the PanaromaGl library for Android include: SDK

How to Create Panorama 360 Views in Android

Ajay Prasad Is it possible to use Panorama 360 View in iOS, also in Android? Can anyone let me know how to create Panorama 360 view in android. Harveys Rush Features that you must see supported in the 0.2 beta of the PanaromaGl library for Android include: SDK

There are segmented views that create actions in other views

msweet168 I have an app that tracks your speed. I have a settings page with a segmented control that toggles between mph and kmh. The setup view controller and the main view controller are connected to the same class. It works fine when the segmented control i

There are segmented views that create actions in other views

msweet168 I have an app that tracks your speed. I have a settings page with a segmented control that toggles between mph and kmh. The setup view controller and the main view controller are connected to the same class. It works fine when the segmented control i

Android: ViewPager swipe only on specific views

Simo I have one ViewPager. In this ViewPager I have Fragmentsall the same layouts. The layout contains a header and a header RecyclerView, I just want the header of this layout to be swipeable. So the user can only switch between fragments within the viewpager

Create dynamic views in Android?

Teff In my application, I receive JSON multiple forms like this: "forms":[ {"name": "form1","title": "First Form","order": 1, "fields":[ "name": "firstName","type": "InputText", "maxLength": "10", "restriction

How to show views on other apps like musixmatch? android

chongche hun I want to make a view that can be dragged, resized, expanded, collapsed and displayed on other apps like Musixmatch. Bomb Shot: Any ideas? Tim Marched Take a look at this article, it might be what you're looking for : http://www.piwai.info/chathea

How to create RTL ViewPager?

Itiel Maimon I'm using ViewPager in my app and I want to support RTL, but when I convert the device language to RTl it doesn't seem to work. I want the tabs to start from the right (first fragment on the right). Please help me out. EDIT : Here's a picture of i