Android: Change text within ViewPager

☆樱花仙子☆ 提交于 2019-12-03 09:03:34

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.

As a small addition to the above suggestion:

  1. When the tag was a String, for some weird reason it never assigned the tag to the View and it always returned null for findViewWithTag. Using the layout id worked though.

  2. Make sure you retrieve the views outside the onCreate(), preferably in the method where u intend to change the text for the text views. Trying to access them in onCreate returns null. My previous understanding was that, as soon as you call the MyPageAdapter, it will call the instantiateItem and so one should be able to find the views soon after that. But that isnt the case.

I just burned a whole day the above 2 issues, since it wasnt really mentioned anywhere. Hope it helps people in the future.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!