Search code examples
androidandroid-pageradapter

PagerAdapter change TextView when swiping


I'm using a PagerAdapter to swipe through a couple of ImageViews which works perfectly fine. I have a bunch of people in the gallery and I wish to add an individual name/description (id: speaker_name) to them. All I got to work is the same description for all of them. Im absolutely new to this and I am struggling for a day now to get it to work. All the solutions I found used fragments or an OnPageChangeListener but i couldnt figure out how to implement it.

This is what i've got:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true">

            <android.support.v4.view.ViewPager
                android:id="@+id/view_pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/basicBackground">
            </android.support.v4.view.ViewPager>

            <TextView
                android:id="@+id/speaker_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="50sp"
                android:text="The Name"
                android:textAlignment="center"
                android:textColor="@android:color/white"
                android:textSize="30sp" />
        </FrameLayout>
    </RelativeLayout>`

PagerAdapter:

public class ImageAdapter extends PagerAdapter {

SpeakerActivity sa;
Context context;
int i = 0;

public int[] GalImages = new int[] {
        R.drawable.ben,
        R.drawable.brett,
        R.drawable.mark,
        R.drawable.dusan,
        R.drawable.michael,
        R.drawable.mike,
        R.drawable.ollie,
        R.drawable.rebecca,
        R.drawable.sebastian,
        R.drawable.thomas,
        R.drawable.tomasz,
        R.drawable.toni,
};
ImageAdapter(Context context){
    this.context=context;
}
@Override
public int getCount() {
    return GalImages.length;
}

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
    ImageView imageView = new ImageView(context);
    int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
    imageView.setPadding(padding, padding, padding, padding);
    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    imageView.setImageResource(GalImages[position]);
    ((ViewPager) container).addView(imageView, 0);
    return imageView;
}

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

Activity:

public class SpeakerActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.speaker_activity);

    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    ImageAdapter adapter = new ImageAdapter(this);
    viewPager.setAdapter(adapter);
}
}

Solution

  • To change textview text when swiping you can do this inside oncreate() method:

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
                    }
    
                    @Override
                    public void onPageSelected(int position) {
                        switch (position){
                            case 0:
                                 //Do stuff
                                textview.setText("Ben");
                                break;
                            case 1:
                                //Do stuff
                                textview.setText("Brett");
                                break;
                         //Add other cases for the pages
    
                        }
    
                    }
    
                    @Override
                    public void onPageScrollStateChanged(int state) {
    
                    }
    
                });
    

    This is the implementation of viewpager OnPageChangeListener() for me the best way.