3

I am using Viewpager and viewpagerindicator to display some content. Currently if we swipe the indicator selected item will change. But I want the vice versa i.e when I click on circle I need to change viewpager item.

Here is the code

 <android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    />
<com.viewpagerindicator.CirclePageIndicator
    android:id="@+id/indicator"
    android:padding="10dip"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    app:fillColor="#FFCC0000"
    app:snap="true"

    /> 
nucleons
  • 742
  • 5
  • 21

3 Answers3

1

Use

Viewpager.setCurrentItem(pageNo)

on clicking

Yashwanth Kumar
  • 28,931
  • 15
  • 65
  • 69
1

You need to modify the corresponding class's source code, for example this is my modifiedIconPageIndicator :

public void notifyDataSetChanged() {
    mIconsLayout.removeAllViews();
    IconPagerAdapter iconAdapter = (IconPagerAdapter) mViewPager.getAdapter();
    int count = iconAdapter.getCount();
    for (int i = 0; i < count; i++) {
        ImageView view = new ImageView(getContext(), null, R.attr.vpiIconPageIndicatorStyle);
        view.setImageResource(iconAdapter.getIconResId(i));

        //custom bigger indicator
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
        view.setLayoutParams(layoutParams);

        //custom onClick
        view.setTag(""+i);
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int pos = Integer.parseInt(v.getTag().toString());
                mViewPager.setCurrentItem(pos);
            }
        });

        mIconsLayout.addView(view);
    } 

Many Thanks to the source :

https://github.com/JakeWharton/Android-ViewPagerIndicator/pull/321/files

Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
0

You can do that inside onclick listener of a button.Like,

ViewPager.setCurrentItem(0); //here I set first page
chue x
  • 18,573
  • 7
  • 56
  • 70
Basim Sherif
  • 5,384
  • 7
  • 48
  • 90