【发布时间】:2013-11-18 13:35:03
【问题描述】:
当按下返回按钮时,如何刷新片段的视图?
我已经在片段的 onResume 方法中尝试过了,但它不起作用。
好的,代码如下
布局如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@color/mainscreen_bg">
<fragment
android:id="@+id/topbar"
android:name="de.app.applib.TopbarFragment"
android:layout_width="match_parent"
android:layout_height="@dimen/topbar_height"
android:layout_alignParentTop="true"
tools:layout="@layout/fragment_topbar" />
<fragment
android:id="@+id/position_list"
android:name="de.app.applib.CriteriaListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@id/topbar"
android:tag="CriteriaList"
tools:layout="@layout/fragment_criterialist" />
TopbarFragment 的布局如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/topbar_bg" >
<ImageView android:id="@+id/topbar_home"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:src="@drawable/ic_ab_back_holo_light"
android:visibility="invisible"
android:padding="0dp"
android:contentDescription="@string/none"
android:layout_margin="0dp"/>
<ImageView android:id="@+id/topbar_logo"
android:src="@drawable/actionbar_logo"
android:layout_height="30dp"
android:layout_width="120dp"
android:layout_centerVertical="true"
android:padding="0dp"
android:layout_margin="0dp"
android:contentDescription="@string/none"
android:layout_toRightOf="@id/topbar_home"/>
<ToggleButton
android:id="@+id/topbar_btn_push_abo"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/topbar_btn_info"
android:layout_toLeftOf="@id/topbar_btn_info"
android:background="@drawable/topbar_btn_push_bg"
android:paddingTop="@dimen/padding_topbar_btn_push"
android:textOff=""
android:textOn=""
android:textSize="12sp" />
在 Fragment 的 onCreateView() 中,我设置了 toggleButton 的 textOn 和 textOff 值。
此片段包含在所有活动中。
现在,我想在用户按下后退按钮时更新 toggleButton 的文本。 我在 Fragment 的 onResume() 方法中设置了新文本。此方法被调用,但旧文本出现在活动中,而不是新的更新文本。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG,"onCreateView called");
View v = inflater.inflate(ResourceUtils.getCustomizableLayoutID(getActivity(), "fragment_topbar"), container, false);
subscrButton = (ToggleButton) v.findViewById(R.id.topbar_btn_push_abo);
setCountOfNewAdds();
private void setCountOfNewAdds() {
int countOfNewAds = ((App)getActivity().getApplication()).getCountOfNewAds();
subscrButton.setTextOff(countOfNewAds>0?""+countOfNewAds:"");
subscrButton.setTextOn(countOfNewAds>0?""+countOfNewAds:"");
subscrButton.invalidate();
}
@Override
public void onResume() {
super.onResume();
setCountOfNewAdds();
}
【问题讨论】: