Android: Change text within ViewPager(Android:在 ViewPager 中更改文本)
问题描述
背景
我正在编写一个使用 ViewPager 并且运行良好的应用程序.我需要能够定位其中一种布局(xml 文件)并更新其上的 textviews 和 imageviews.我尝试使用v.findViewById"来放大视图和定位,这似乎是我研究的解决方案,但也许我将它放在正确的位置并且需要更好地向我解释.
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.
我的代码(不尝试定位)
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;
}
}
仅供参考:ImageViewcicle"是屏幕底部的 4 个圆圈的图像,会在页面更改时更新以显示您所在的屏幕.工作正常,只是想解释一下.
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.
目标
在名为forecast"的布局上更新名为city"的文本视图.
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.
推荐答案
好吧,我不保证这可行,但我在 ViewPager 使用兼容性包时遇到了类似的问题,我用这个解决了这个问题:
Well I don't garantee that this works but I had a similar problem with ViewPager using compatibility pack that I solved using this:
在您的 instatiateItem
函数视图中,使用您的 resId 执行 setTag:
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);
其中 R.id.city_id
是您的 TextView 的 id.
Where R.id.city_id
is the id of your TextView.
这篇关于Android:在 ViewPager 中更改文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android:在 ViewPager 中更改文本
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01