I was working with ViewPager and I had a problem. I have 3 layout created (screen1, main screen, screen2) that I want to show that way, that the application opens showing the main screen and that when I slide to the left it shows the screen1 and to the right (from the main screen) show the screen2. The problem is that I can not make them stay that way, I have the screen1 first, the main screen on the right and the screen2 on the right. How can I make it look like I want?
This is the code I have:
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private LinearLayout page1;
private LinearLayout page2;
private LinearLayout page3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MainPageAdapter());
}
class MainPageAdapter extends PagerAdapter
{
@Override
public int getCount()
{
return 3;
}
@Override
public Object instantiateItem(ViewGroup collection, int position)
{
View page = null;
switch (position)
{
case 0:
if (page1 == null)
{
page1 = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.pantalla_tienda, null);
}
page = page1;
break;
case 1:
if (page2 == null)
{
page2 = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.pantalla_principal, null);
}
page = page2;
break;
case 2:
if (page3 == null)
{
page3 = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.pantalla_jardin, null);
}
page = page3;
break;
}
((ViewPager) collection).addView(page, 0);
return page;
}
@Override
public boolean isViewFromObject(View view, Object object)
{
return view == object;
}
@Override
public void destroyItem(View collection, int position, Object view)
{
((ViewPager) collection).removeView((View) view);
}
}
}