通过普通 java 类更新 ViewPager 中的 Textview
我使用兼容包中的 ViewPager 来显示页面片段。 我想要一个后台服务来为片段提供数据。因此,我使用“普通java类”来更新(例如)这样一个片段中的TextView:
public class Updater {
private Activity mActivity=null;
private TextView tv = null;
private Context mContext;
private View mInflatedMenu =null;
public Updater (Activity _activity, Context _context, View inflatedMenu) {
mActivity = _activity;
mContext = _context;
mInflatedMenu = inflatedMenu;
}
public void updateTextView (String _text) {
tv = (TextView) mInflatedMenu.findViewById(R.id.tvFragment1Updater);
tv.setText(_text);
Toast.makeText(mContext, tv.getText(), Toast.LENGTH_SHORT).show();
}
}
Toast说,TextView具有参数_text
的值。没关系!
问题:在屏幕上,TextView 的原始值尚未更改为 _text
。
我想我已经尝试过各种invalidate()
。我做错了什么?您将如何更新 ViewPager 中的片段“视图”?
I use the ViewPager from the compatibility pack to page fragments.
I want to have a background service which serves the fragments with data. Therefore I use a "normal java class" to update (for example) a TextView in such a fragment:
public class Updater {
private Activity mActivity=null;
private TextView tv = null;
private Context mContext;
private View mInflatedMenu =null;
public Updater (Activity _activity, Context _context, View inflatedMenu) {
mActivity = _activity;
mContext = _context;
mInflatedMenu = inflatedMenu;
}
public void updateTextView (String _text) {
tv = (TextView) mInflatedMenu.findViewById(R.id.tvFragment1Updater);
tv.setText(_text);
Toast.makeText(mContext, tv.getText(), Toast.LENGTH_SHORT).show();
}
}
The Toast says, that the TextView has the value of the parameter _text
. That's okay!
The problem: On the screen the original value of the TextView hasn't changed to _text
.
I think I've tried all kinds of invalidate()
. What I'm doing wrong? How would you update your fragment "views" in a ViewPager?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要保存toast实例,当您更改
_text
的vlaue时,您需要隐藏旧的toast并创建一个新的toast。You need to save the toast instance, when you are changing the vlaue of
_text
, you neen to hide the old toast and create a new one.