【发布时间】:2014-07-24 18:54:55
【问题描述】:
我已经四处搜索,但无法找到解决此问题的方法。我有一个正在显示的片段,当您按下一个按钮时,它会在其上打开一个新的活动。然后,当该 Activity 完成时,它会将一些意图数据返回给 Fragment/Parent Activity。该数据有时会更改 TextView 或将按钮添加到 Fragment。但是,如果不将 Fragment 重新添加到后台堆栈(popBackStack 然后替换或添加),我无法更新 Fragment 的视图。
我尝试过的几件事是在 Fragment 的基本布局上调用 invalidate() 和 requestLayout(),但刷新视图的唯一方法似乎是通过删除并重新添加它再次调用 onCreateView() .有没有办法在不重新加载的情况下指定 Fragment 的“内容视图”?
父活动代码:
// Used to retrieve the data from the first Activity
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
// Try to update the Fragment's view here
mMyFragment.refreshView(data.getString(NEW_BUTTON_TEXT));
}
}
片段代码(MyFragment):
private View mBaseLayout;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mBaseLayout = inflater.inflate(R.layout.my_layout, container, false);
...
return mBaseLayout;
}
public void refreshView(String newButtonText) {
Button button = mBaseLayout.findViewById(R.id.new_button);
button.setText(newButtonText);
button.setVisibility(View.VISIBLE);
// TODO Reset the Fragment's view with the update mBaseLayout view
// ? ? ?
// Tried mBaseLayout.invalidate() and mBaseLayout.requestLayout() here
}
对此的任何想法都会非常有帮助,谢谢!
【问题讨论】:
标签: java android android-layout android-fragments