2017-06-17 61 views
0

我想刷新列表視圖fragmentA當回壓如何onbackpressed時刷新片段列表視圖()

例如:

我在FragmentA列表視圖中單擊onitem到FragmentB

,當我點擊FragmentB中的(在Activity中),我想刷新 listview FragmentA。

對不起我的英語不好。

如何在FragmentA中設置onViewCreated或onResume ???

和我的活動

@Override 
    public void onBackPressed() { 
     super.onBackPressed(); 

MyFragmentA

@Override 
    public void setItems(ArrayList<NotificationListModel> items,String last_page) { 
     Log.d("NotificationFragment", "setItems "); 
     this.notificationListModels=items; 
     this.last_page=last_page; 

     for (NotificationListModel notificationListModel: notificationListModels){ 
      notificationListModelArrayList.add(notificationListModel); 
     } 
     getData(); 
     if (getActivity()!=null) { 
      int currentPosition = getListView().getFirstVisiblePosition(); 
      adapNotificationList = new AdapNotificationList(getActivity(), notificationListModelArrayList); 
      setListAdapter(adapNotificationList); 
      adapNotificationList.notifyDataSetChanged(); 
      getListView().setSelection(currentShowList); 
      getListView().setSelectionFromTop(currentPosition + 1, 0); 
     } 
    } 

回答

0

首先檢查fragmentB在片段A可見或不可見時則

@Override 
    public void onBackPressed() { 
     if(fragmentb.isVisible()) 
     { 
      //update listview of fragment a 
      adapter.notifyDataSetChanged() 
     } 
     else 
      super.onBackPressed(); 
    } 
+0

我的代碼我設置了adapter.notifyDataSetChanged();在setItems()中是錯誤的?請檢查我的代碼,我編輯了一篇文章 –

0

覆蓋的onResume和調用adapter.notifyDataSetChanged( )或處理你的listview數據刷新的函數。

@Override 
public void onResume() { 
    super.onResume(); 
    adapter.notifyDataSetChanged(); 
    //OR 
    callDataRefreshFunction(); 

} 

您不需要在Activity中處理onBackPressed。

+0

嗯我的代碼我設置adapter.notifyDataSetChanged();在setItems()中是錯誤的?請檢查我的代碼,我編輯了一篇文章 –

+0

adapter.notifyDataSetChanged()在調用ListView時重繪視圖。所以在設置適配器後調用它是不必要的,可以刪除。 – nitinkumarp

+0

請給我例子T.T –

0

我想你是從服務器加載數據到列表視圖。如果是這樣,你想要刷新列表視圖,那就不要重寫壓縮後的方法。只需從片段B中刪除此代碼。

@Override 
    public void onBackPressed() { 
     super.onBackPressed(); 
    }