-1

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);
}
}
Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
BCuracao
  • 25
  • 6
  • Welcome to SO! Check out this post on how to ask a good question: https://stackoverflow.com/help/how-to-ask. In this case I think you need to tell us what error you are getting. – Robert Moskal Jul 29 '17 at 20:31

2 Answers2

1

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.

Bruno Ferreira
  • 1,561
  • 1
  • 10
  • 17
0

let me give you the basic idea

  1. ViewPager that the Activity layout is binded to.
  2. ImageAdapter that extends ViewPager/FragmentPager that basically adds views or fragments into the viewpager so that the activity can show it to you.
  3. the activity connects the two objects by setting the viewpager's adapter to your ImageAdapter Like I said in #2 there are many ways you u can implement the ViewPager there are many sites out there. refer to https://www.bignerdranch.com/ it explain in great detail
Aelaf
  • 118
  • 1
  • 11