问题
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.
回答1:
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.
回答2:
As a small addition to the above suggestion:
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.
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.
来源:https://stackoverflow.com/questions/10678950/android-change-text-within-viewpager