Use multiple layouts in ViewPageIndicator

I would like to know how to install the layout adapter in the ViewpageIndicator. I have 3 Layout, I would like to scroll the trough using ViewpageIndicator. And each layout has its own activity.

class SampleCirclesWithListener:

public class SampleCirclesWithListener extends BaseSampleActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_circles); mAdapter = new TestFragmentAdapter(getSupportFragmentManager()); mPager = (ViewPager)findViewById(R.id.pager); mPager.setAdapter(mAdapter); mIndicator = (CirclePageIndicator)findViewById(R.id.indicator); mIndicator.setViewPager(mPager); //We set this on the indicator, NOT the pager mIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { public void onPageSelected(int position) { Toast.makeText(SampleCirclesWithListener.this, "Changed to page " + position, Toast.LENGTH_SHORT).show(); } public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } 

I was looking for a watch to solve. Can anyone help? Thank you very much!

+4
source share
2 answers

To extend CommonsWare an excellent answer and populate it with code so you can imagine:

If you have a Jake Wharton demo project for ViewPageIndicator, you should have TestFragmentAdapter and TestFragment . The TestFragmentAdapter class extends the FragmentPagerAdapter class, and the TestFragment class must extend the Fragment . Currently, the "Actions" paging wand is not possible, but you can scroll through fragments.

Create a Fragment and set it to your layout using the onCreateView() method inside your fragment:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.layout, container, false); } 

Your FragmentPagerAdapter can now have a method called getItem() . Switch Position and set it to fragment:

 @Override public Fragment getItem(int position) { switch(position){ case 0: TestFragment fragment = new TestFragment(); return fragment; case 1: TestFragment2 fragment2 = new TestFragment2(); return fragment2; } return defaultFragment fragment3 = new defaultFragment(); return fragment3; } 

Now it should work the way you want it.

+2
source

I would like to know how to install layout adapter in ViewpageIndicator

You do not install the layout adapter in the ViewpageIndicator. You install the PagerAdapter in the ViewPager and perhaps use the ViewPagerIndicator with this ViewPager .

And each layout has its own activity.

ViewPager does not support swapping between actions. It supports paging between views, where these views are optionally fragment-controlled (for example, FragmentPagerAdapter ).

+2
source

All Articles