2

I'm rebuilding my app design and would like to use a ViewPager for it. The implementation wasn't quite the problem, but I can't figure out how to change my TextViews inside the pages. Hours of googling didn't help me either. Maybe you can help me. I simply need to know how to change the text, textcolor and stuff of my TextViews. main.xml includes my ViewPager and speiseplan.xml includes the TextViews inside a ScrollView, each with an id.

main.xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

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

</LinearLayout>

I won't write the whole speiseplan.xml here, since it simply includes a ScrollView and a bunch of TextViews.

Implementation in Java is the following:

public class Speiseplan extends Activity {

    private ViewPager myPager;
private static int NUM_VIEWS = 14;
private MyPagerAdapter myAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myAdapter = new MyPagerAdapter();
    myPager = (ViewPager) findViewById(R.id.awesomepager);
    myPager.setAdapter(myAdapter);
}

private class MyPagerAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return NUM_VIEWS;
        }


        @Override
        public Object instantiateItem(View collection, int position){
            LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = inflater.inflate(R.layout.speiseplan, null);

            ((ViewPager) collection).addView(view, 0);

            return view;
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2){
            ((ViewPager) arg0).removeView((View) arg2);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == ((View) arg1);
        }

    @Override
    public Parcelable saveState() {
        return null;
    }

}
}

Now how can I access my TextViews and change them. And how can I detect the user sqiping to another page.

Thanks for helping!

TheBrillowable
  • 319
  • 1
  • 6
  • 17

1 Answers1

1

I think you will have to change the TextViews in the instantiateItem() method. It works almost like the getView() method in other adapters.

The PagerAdapter does not automatically recycle views like other adapters such as BaseAdapter or ArrayAdapter. You may want to implement the recycling yourself. Take a look at the documentation which gives some hints about how to do this.

To detect swipe, you can use the OnPageChangeListener interface.

Kaloer
  • 3,773
  • 2
  • 27
  • 33
  • Somehow I can't get it to work. I just can't acces the TextViews I have defined in my layout. Maybe you could provide me with some sample code? – TheBrillowable Dec 25 '11 at 18:39
  • Hmm.. You should be able to call 'view.findViewById()' with your TextView ids declared in your xml file. What exactly do you mean by not being able to "access" the TextViews? – Kaloer Dec 25 '11 at 20:12
  • When I call them like you said it, it seems assigning it to a variable works, but I can't change the text or something like I would normally do. – TheBrillowable Dec 25 '11 at 20:55
  • Strange. Do you do the same as in [this answer](http://stackoverflow.com/a/7018289/236130)? You may also want to change your initialization of the LayoutInflator to something like this: `[...] = LayoutInflator.from(Speiseplan.this)`. I'm not sure it will do anything different, but at least it is a bit simpler than your current way to do it. – Kaloer Dec 25 '11 at 21:39
  • Okay, changing the text inside of the instantiateItem() method now works, thanks a lot. I'll try get it to work somehow. – TheBrillowable Jan 01 '12 at 19:13