Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ViewPager Prev/Next Button

Okay, so I'm developing an Android app that utilises a ViewPager to display pages.

Within each page, I have a set of buttons to use for navigating between pages (in addition to the swiping between pages). These buttons are for "first page", "previous page", "next page" and "last page".

What I can't figure out how to do is engineer a mechanism to enable a page change on a button click.

Anyone have any ideas?

ETA: To better explain the setup, the buttons are declared within each page's layout, and are inflated with the rest of the layout within the PagerAdapter. My problem is that I can't reference the ViewPager from within the PagerAdapter. Or at least, I can't think of a way to do it.

like image 844
Yoda Avatar asked Jun 01 '12 15:06

Yoda


3 Answers

Button:

Button yourButton = (Button)findViewById(R.id.button1);
    yourButton.setOnClickListener(new OnClickListener() {

     @Override
           public void onClick(View view) {
               mViewPager.setCurrentItem(getItem(+1), true); //getItem(-1) for previous
           }
        });

Function:

private int getItem(int i) {
       return mViewPager.getCurrentItem() + i;
}

Hope this helps :)

like image 52
sweggersen Avatar answered Nov 05 '22 12:11

sweggersen


yourButton.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View view) {
      yourViewPager.setCurrentItem(page, smoothScroll);
   }
});
like image 45
Alex Orlov Avatar answered Nov 05 '22 12:11

Alex Orlov


1) Make layout

      <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="@color/white"
                android:weightSum="1">



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

                <ImageView
                    android:id="@+id/img_next"
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:src="@drawable/forward_white"
                    android:background="@color/colorPrimary"
                    android:layout_centerVertical="true"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="5dp"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    />


                <ImageView
                    android:id="@+id/img_previous"
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:src="@drawable/back_white"
                    android:background="@color/colorPrimary"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="5dp"
                    android:layout_alignParentLeft="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"/>


            </RelativeLayout>

2) Set custom Adapter

        CustomViewPagerAdapter  custompageradpter;
        ViewPager   mViewPager;
        mViewPager = (ViewPager)shareImagesDialouge.findViewById(R.id.images_pager); 
         custompageradpter = new CustomViewPagerAdapter(this);
         mViewPager.setAdapter(custompageradpter);

    public class CustomViewPagerAdapter  extends PagerAdapter {


        Context mContext;
        LayoutInflater mLayoutInflater;

        int[] mResources = {
                R.drawable.emoji_1,
                R.drawable.emoji_2,
                R.drawable.emoji_3,
                R.drawable.emoji_4,
                R.drawable.emoji_5,
                R.drawable.emoji_6
        };

        public CustomViewPagerAdapter(Context context) {
            mContext = context;
            mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return mResources.length;
        }

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);

            ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
            imageView.setImageResource(mResources[position]);

            container.addView(itemView);

            return itemView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((LinearLayout) object);
        }

    }

3) For next and previous button

         case R.id.img_previous:
                    mViewPager.setCurrentItem(getItemofviewpager(-1), true);
                    break;

                case R.id.img_next:
                    mViewPager.setCurrentItem(getItemofviewpager(+1), true);
                    break;

4) Make this function

  private int getItemofviewpager(int i) {
        return mViewPager.getCurrentItem() + i;
    }
like image 4
priti Avatar answered Nov 05 '22 12:11

priti