Sort View Pager

3

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);
    }

}

}

    
asked by Zekirak 04.10.2016 в 01:26
source

1 answer

0

Did you try the following way?

int item = 1; //La posicion que le corresponde al view que quieres mostrar primero
viewPager.setCurrentItem(item);

Another way to implement this is to give the adapter's constructor an ArrayList that contains the fragments of your screens ( You must add them in order ).

The adapter example is this:

public class MainFragmentAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public MainFragmentAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
}

And the MainActivity would be like this:

ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

List<Fragment> fragments = new ArrayList<>();

fragments.add(new FragmentPantalla1);
fragments.add(new FragmentPantallaPrincipal); //Item en la posicion 1 del array
fragments.add(new FragmentPantalla2);

FragmentManager fm = getSupportFragmentManager();
MainFragmentAdapter fragmentAdapter = new MainFragmentAdapter(fm, fragments);

viewPager.setAdapter(fragmentAdapter);
viewPager.setCurrentItem(1);

I hope the answer will help you. Any questions, to the orders.

    
answered by 19.10.2016 в 04:51