2015-11-20 102 views
2

我在mainActivity中有一個對話框提示。如果用戶關閉了對話框,我需要在fourthFragment內的一個ListView項目中顯示紅點(根據下圖中的fourthFragment中的紅框)。Android:點擊按鈕上的另一個活動刷新/更新片段

問題是隻有在關閉應用程序並重新打開它後才更新紅點,因爲fourthFragment已在用戶關閉對話框之前完成創建。如何在Dialog關閉後刷新/更新fourthFragment,以便立即顯示紅點?

簡短描述:

  • mainActivity:上對話框關閉>商店showRedDot = 「1」 到本地分貝
  • fourthFragment:的onCreate>從本地分貝讀showRedDot,如果 「1」,顯示的紅點。 (此問題,當的onCreate,showRedDot仍然是「0」,所以我需要更新fourthFragment佈局對話框關閉後。)

enter image description here

+0

你可以在對話框單擊後嘗試刷新主活動的第四個片段嗎? – Nivedh

+0

一種方法是使用本地廣播接收器,在對話框關閉時廣播消息並在片段類中偵聽事件。不確定的表現。 –

+0

使用Handler()在一段時間內刷新你的列表視圖並通知它,所以你將克服這個問題 –

回答

1

嘗試接口或廣播接收器如上。如果其他刷新片段爲

// Reload current fragment 
Fragment frg = null; 
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG"); 
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
ft.detach(frg); 
ft.attach(frg); 
ft.commit(); 
+0

謝謝,我現在能夠更新我的紅點。在第四個片段onViewCreated():'getActivity()。getSupportFragmentManager()。beginTransaction()。add(this,「general_setting_fragment_tag」);';因此在關閉對話框後的mainActivity中:'Fragment frg = getSupportFragmentManager()。findFragmentByTag(「general_setting_fragment_tag」); final FragmentTransaction ft = getSupportFragmentManager()。beginTransaction(); ft.detach(frg); ft.attach(frg); ft.commit();' –

1

你調用接口來推動事件解僱(點擊按鈕)的對話。 像這樣:

public interface MyDialogListener { 
    void OnCloseDialog(); 
} 

public class fourthFragment extend Fragment implements MyDialogListener { 
    public void SomeMethod() { 
     MyDialog myDialog = new MyDialog(this, this); 
     myDialog.show(); 
    } 

    public void OnCloseDialog() { 
     // Do update your listview in here(maybe call method initialize data for listview) 
    } 

} 

public class MyDialog extends Dialog { 
    MyDialogListener mListener; 

    public MyDialog (Context context, MyDialogListener listener) { 
     super(context, R.style.Dialog); 
     mListener = listener; 
    } 

    public void onClick(View view) { 
     switch (view.getId()) { 
      case R.id.CloseButton: 
       // Push event when Dialog close(or anything) 
       mListener.OnCloseDialog(); 
       dismiss() 
       break; 
      default: 
       //... 
     } 
    } 
} 
+0

嗨,感謝您的回覆。不知道我是否誤解了你的代碼,我的對話框在我的'mainActivity'裏面,但是我想要更新的是我的'fourthFragment',我怎樣才能實現你的方法?或者讓我們說,我如何更新'mainActivity'的'fourthFragment'列表視圖項? –

+0

你可以聽在mainActivity對話的情況下,使mainActivity有像[公共靜態MyDialogListener dialogListener]屬性 在fourthFragment使[mainActivity.dialogListener =新MyDialogListener(){ 公共無效OnCloseDialog(){// 做更新列表視圖中這裏 } } ] – hoangdv

+0

它爲我工作我正在使用片段\ –