Android onCreateView更改不会更新
我有一个带有多个选项卡的Android onCreateView更改不会更新,android,android-fragments,android-tabhost,android-view,Android,Android Fragments,Android Tabhost,Android View,我有一个带有多个选项卡的FragmentTabHost。每次用户选择选项卡时,我都想更改其中一个选项卡中某些按钮内的文本,但我无法使其正常工作。看起来Android正在缓存片段,甚至调用了ougonCreateView 内部onCreateViewI do(作为测试): 我已经加载了6个具有相同片段类的选项卡,每次我第一次选择一个选项卡时,按钮文本都会获得递增的I…但是如果我再次选择这些选项卡中的任何一个,文本将保持不变,就像我第一次选择它时一样。我知道I正在变化,因为我可以多次选择选项卡(假设
FragmentTabHost
。每次用户选择选项卡时,我都想更改其中一个选项卡中某些按钮内的文本,但我无法使其正常工作。看起来Android正在缓存片段
,甚至调用了ougonCreateView
内部onCreateView
I do(作为测试):
我已经加载了6个具有相同片段类的选项卡,每次我第一次选择一个选项卡时,按钮文本都会获得递增的I
…但是如果我再次选择这些选项卡中的任何一个,文本将保持不变,就像我第一次选择它时一样。我知道I
正在变化,因为我可以多次选择选项卡(假设每次我在调用CreateView并增加“I”时,都会在选项卡1和2之间切换),只需增加I
,然后第一次选择另一个选项卡(假设它是选项卡6),i
的值在该片段中的按钮内按其应显示,但在此之后,按钮的文本不再更新
这对我很重要,因为我有一些字符串被翻译成其他语言,我不能触发按钮语言文本中的任何更新
如何强制Android更新按钮文本
编辑:
我可以在恢复时将更新移动到onResume
,但我想了解一下onCreateView
中发生的事情,即事情不会得到更新
编辑2:
这是我的onCreateView
代码:
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
final Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_test_tab, container, false);
........ // configuring some buttons and grids callbacks and animations
i++;
RadioButton button1 = (RadioButton) rootView.findViewById(R.id.button1);
button1.setText(String.valueOf(i));
RadioButton button2 = (RadioButton) rootView.findViewById(R.id.button2);
button2.setText(String.valueOf(i));
RadioButton button3 = (RadioButton) rootView.findViewById(R.id.button3);
button3.setText(String.valueOf(i));
RadioButton button4 = (RadioButton) rootView.findViewById(R.id.button4);
button4.setText(String.valueOf(i));
return rootView;
}这是因为OncreateView()只执行一次,每次选择选项卡时执行的操作是onResume()
尝试将代码移动到onResume()方法
我不这么认为,正如我所说,I++
正在运行,这意味着onCreateView
正在运行,此外,我可以在其中放置一个断点,我可以看到onCreateView
正在运行。我想您可能正在尝试更改过去视图的信息。我必须看到更多的代码。请尝试使用Log.d,在onCreate()方法或onCreateView()@Akagami中创建片段时打印I值,哪个代码部分可以帮助您查看它?我的代码非常简单…@user3586222我向你保证我正在更改它只是没有显示出来
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
final Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_test_tab, container, false);
........ // configuring some buttons and grids callbacks and animations
i++;
RadioButton button1 = (RadioButton) rootView.findViewById(R.id.button1);
button1.setText(String.valueOf(i));
RadioButton button2 = (RadioButton) rootView.findViewById(R.id.button2);
button2.setText(String.valueOf(i));
RadioButton button3 = (RadioButton) rootView.findViewById(R.id.button3);
button3.setText(String.valueOf(i));
RadioButton button4 = (RadioButton) rootView.findViewById(R.id.button4);
button4.setText(String.valueOf(i));
return rootView;
public void onResume(){
i++;
RadioButton button = (RadioButton) rootView.findViewById(R.id.button);
button.setText(String.valueOf(i));
}