Establecer página predeterminada para ViewPager en Android


Estoy usando el siguiente código, MAX es de 2 páginas. Por defecto la posición es 0 y añade una nueva página a la derecha. Inflo dos archivos de diseño.

¿Cómo puedo mostrar la page1 cuando se inicia la aplicación y agregar una nueva página a la izquierda ? Gracias.

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <android.support.v4.view.ViewPager
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:id="@+id/pagerView" />


</LinearLayout>

Código Java

public class MyPagerActivity extends Activity {
    private Context context;    
    private int pageNumber;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context = this;

        ViewPager pagerView = (ViewPager)findViewById(R.id.pagerView);
        pagerView.setAdapter(new AwesomePagerAdapter());    

    }


    private class AwesomePagerAdapter extends PagerAdapter{

        @Override
        public void destroyItem(View collection, int position, Object view) {
            ((ViewPager) collection).removeView((View)view);            
        }

        @Override
        public void finishUpdate(View arg0) {
            //setPageTitles(getPageNumber());
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public Object instantiateItem(View collection, int position) {
            /*  TextView tv = new TextView(MyPagerActivity.this);
        tv.setText("Bonjour PAUG " + position);
        tv.setTextColor(Color.WHITE);
        tv.setTextSize(30);

        ((ViewPager) collection).addView(tv,0);

        return tv;*/

            View view = getViewToShow(position);
            ((ViewPager) collection).addView(view,0);
            return view;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==((View)object);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public Parcelable saveState() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
            // TODO Auto-generated method stub

        }

    }


    private View getViewToShow(int position){
        View view = null;
        View layout; 
        LayoutInflater mInflater =  (LayoutInflater)
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        switch(position){

        case 0: 
            layout  = mInflater.inflate(R.layout.elements, null);
            view = layout;
            break;
        case 1: view = 
            layout  = mInflater.inflate(R.layout.elements2, null);
        view = layout;

        break;
        }
        return view;
    }

}
Author: Jamie Hutton, 2011-09-13

4 answers

¿Ha intentado usar el método setCurrentItem?

 116
Author: Cristian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-09-13 00:50:12

La solución aceptada está bien, pero es importante llamar a este método después de configurar el adaptador.

    viewPager.setAdapter(adapterViewPager);
    viewPager.setCurrentItem(1);
 45
Author: Anor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-17 19:34:12

De forma predeterminada, debe establecer el valor actual 1 para mostrar la única vista predeterminada. Si no establece setCurrentItem en 1, no puede ver nada.

viewPager.setAdapter(adapterViewPager);
viewPager.setCurrentItem(1)
 5
Author: Farruh Habibullaev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-22 01:09:23

Esta solución ofrece la capacidad de establecer la página predeterminada sin interferencia desde fuera de la clase adaptador de paginador.

@Override
public Object instantiateItem(ViewGroup container, int position) {
    View currentPage = null;
    switch(position){
        case 0:
            currentPage = LayoutInflater.from(context).inflate(R.layout.page0, null)    
            break;
        case 1:
            currentPage = LayoutInflater.from(context).inflate(R.layout.page1, null)    
            ///////////// This page will be default ////////////////////
            ((ViewPager)container).setCurrentItem(position);
            ////////////////////////////////////////////////////////////
            break;
        case 2:
            currentPage = LayoutInflater.from(context).inflate(R.layout.page2, null)    
            break;
    return currentPage;
}
 -1
Author: Kvant,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-25 02:23:05