【问题标题】:fragment does not refresh when back button is pressed按下后退按钮时片段不刷新
【发布时间】: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();
}

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    如果你的fragment调用replace(),而不是add()方法切换到另一个fragment(SecondFragment),当SecondFragment中的返回按钮被按下时,你的fragment会再次调用onCreateView()方法

    【讨论】:

    • 我不调用 add 或 repalce 来添加片段我使用 getSupportFragmentManager().findFragmentById() 方法在活动中获取片段
    【解决方案2】:

    您可以捕获用户在 Fragment 所在的活动中的返回点击。

    然后,您可以在 Fragment 中调用 refresh 方法来处理刷新视图。

    在你的活动中:

    @Override
    public void onBackPressed() {
        Fragment yourFragment = getSupportFragmentManager().findFragmentById("ID");
        (TopbarFragment) yourFragment.setCountOfNewAdds();
    }
    

    【讨论】:

      【解决方案3】:

      问题的解决方案是在布局中使用 Framelayout 来包含片段,例如

      <FrameLayout
      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" />
      

      然后使用FragmentManager在onStart()方法中插入Fragment

      @Override
      protected void onStart() {
          super.onStart();
      
          TopbarFragment topbarFragment = (TopbarFragment) getSupportFragmentManager().findFragmentById(R.id.topbar);
          if(topbarFragment == null) {
              topbarFragment = new TopbarFragment();
              getSupportFragmentManager().beginTransaction().replace(R.id.topbar, topbarFragment).commit();
              getSupportFragmentManager().executePendingTransactions();
          }
      }
      

      那么 onResume() 方法中的更新就起作用了

      【讨论】:

        【解决方案4】:

        就我而言,我使用LocationManager 接收用户位置。首先,我打开另一个片段(片段 B),然后使用返回按钮返回到当前片段(片段 A)。然后将view 变量(view == getView()) 传递给启动locationManager.requestSingleUpdate 的方法。

        在收到onLocationChanged 事件后,view 变量奇怪地变成了另一个值,而 getView() 是对的。因此,当我后来使用view.textview.text = "aaa" 访问任何控件时,它没有设置新值,片段也没有刷新。另外getView() 并不总是null,它可以是null,如果你从onCreateView 获得它,但不是从onViewCreated 获得。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-30
          相关资源
          最近更新 更多