Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Change text within ViewPager

Background

I'm writing an app that uses the ViewPager and that's working nicely. I need to be able to target one of the layouts (xml file) and update the textviews and imageviews on it. I've tried inflating the view and targeting with "v.findViewById", which seems to be the solution from my research but maybe I'm putting it in the correct place and need it explained better to me.

My Code (without trying to target)

private ImageView circle;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    circle = (ImageView) findViewById(R.id.circles);

    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.myViewPager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(1);
    myPager.setOnPageChangeListener(new MyPageChangeListener()); 
}

private int focusedPage = 0;
private class MyPageChangeListener extends ViewPager.SimpleOnPageChangeListener {
    @Override
    public void onPageSelected(int position) {
        focusedPage = position;

        switch (focusedPage) {
            case 0: circle.setImageResource(R.drawable.circles1); break;
            case 1: circle.setImageResource(R.drawable.circles2); break;
            case 2: circle.setImageResource(R.drawable.circles3); break;
            case 3: circle.setImageResource(R.drawable.circles4); break;
        }
    }
}

private class MyPagerAdapter extends PagerAdapter {

    public int getCount() {
        return 4;
    }

    public Object instantiateItem(View collection, int position) {

        LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        int resId = 0;
        switch (position) {
            case 0:
                resId = R.layout.zip;
                break;
            case 1:
                resId = R.layout.forecast;
                break;
            case 2:
                resId = R.layout.settings;
                break;
            case 3:
                resId = R.layout.about;
                break;
        }

        View view = inflater.inflate(resId, 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) {
        return arg0 == ((View) arg1);

    }

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

FYI: the ImageView "cicle" is an image of 4 circles at the bottom of the screen that gets updated on page change to show what screen you're on. working fine, just wanted to explain.

GOAL

Update a textview named "city" on the layout named "forecast".

Thanks in advance for any help! After this little hurdle, it should be easy sailing.

like image 644
plowdk51 Avatar asked May 13 '26 04:05

plowdk51


1 Answers

Well I don't garantee that this works but I had a similar problem with ViewPager using compatibility pack that I solved using this:

In your view at the instatiateItem function do a setTag with your resId:

view.setTag(resId);

Then when you need to change your "city" do:

View baseLayout = myPager.findViewWithTag(R.layout.forecast);
TextView city = (TextView) baseLayout.findViewById(R.id.city_id);

Where R.id.city_id is the id of your TextView.

like image 132
Tiago Almeida Avatar answered May 14 '26 16:05

Tiago Almeida



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!