Set default page for ViewPager in Android
Asked Answered
Q

5

69

I am using the following code, MAX is 2 pages. By default the position is 0 and adds a new page to the right. I inflate two layout files.

How can I show the page1 when the app starts and add a new page to the left ? Thanks.

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>

Java code

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

}
Quoin answered 13/9, 2011 at 0:45 Comment(0)
T
126

Have you tried using the setCurrentItem method?

Tyrr answered 13/9, 2011 at 0:50 Comment(2)
How to get event of viewpager load?Shue
Just wanted to notice that if you are using Kotlin with ViewPager2, use setCurrentItem(pageNum, false) method instead of currentItem = pageNumMisapply
Q
56

The accepted solution is fine, but it's important to call this method after setting up the adapter.

    viewPager.setAdapter(adapterViewPager);
    viewPager.setCurrentItem(1);
Quoit answered 19/3, 2015 at 3:13 Comment(0)
N
7

By default, you have to set the current value 1 to show the just one default view. If you do not set setCurrentItem to 1, you can not see anything.

viewPager.setAdapter(adapterViewPager);
viewPager.setCurrentItem(1)
Notate answered 22/9, 2016 at 0:40 Comment(0)
B
0

This solution gives the ability of setting default page without interference from outside of the Pager adapter class.

@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;
}
Busyness answered 2/9, 2014 at 10:37 Comment(0)
H
0

You need to set page only during first initialisation of the fragment. Later it will restore a selected by user position from a "saved instance state". So this looks a little better:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    pager.adapter = AwesomePagerAdapter()
    if (savedInstanceState == null) {
        pager.currentItem = 1
    }
}
Hake answered 15/7, 2019 at 14:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.