2013-10-24 99 views
0

我正在與ViewPager和我有三個片段Fragment A, Fragment B and Fragment C其中Fragment AFragment B要溝通Fragment C。我已經實現了通信邏輯,但這裏是這樣的:當數據從Fragment B傳遞時,我無法刷新/更新Fragment C的視圖。一切正常,當Fragment AFragment C要溝通:視圖根據傳遞的數據進行更新。更新/刷新/溝通兩個片段

Fragment C這是一個MediaPlayer ...它播放從Fragment B溝通的媒體網址,但佈局有變化。有人可以告訴我這裏發生了什麼。下面是我做了什麼至今:

接口

public interface MediaInterface { 
    public void onPodCastClick(int position, 
      ArrayList<HashMap<String, String>> toPass); 
} 

在這兩個片段A和B

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    // TODO Auto-generated method stub 
    String title = data.get(position).get("Title").toString(); 
    SM.setCurrentPlayedID(title); 
    popPass.onPodCastClick(position, data); 
} 

@Override 
public void onAttach(Activity activity) { 
    // TODO Auto-generated method stub 
    super.onAttach(activity); 
    try{ 
     popPass = (MediaInterface)getActivity(); 
    } catch(ClassCastException e){ 
     Log.i(tag, "Activity " + getActivity().getClass().getSimpleName() 
       + " does not implement the MediaInterface"); 
     e.printStackTrace(); 
    } 
} 

其中popPassMediaInterface的一個實例。

在MainActivity(其中ViewPager被實現)

@Override 
public void onPodCastClick(int position, 
     ArrayList<HashMap<String, String>> toPass) { 
    // TODO Auto-generated method stub 
    Bundle element = new Bundle(); 

    element.putSerializable("toPass", toPass); 
    element.putInt("position", position); 

    Fragment toGo = new FragmentC(); 
    toGo.setArguments(element); 
    FragmentTransaction transaction = getSupportFragmentManager() 
      .beginTransaction(); 
    transaction.add(toGo, "FragmentC").commit(); 
    pager.setCurrentItem(FRAGMENT_C); 
} 

在C片段

Bundle element = getActivity().getSupportFragmentManager() 
        .findFragmentByTag("FragmentC").getArguments(); 

那裏有根據在包中的元素在視圖中所做的更改。

請你幫我弄清楚發生了什麼,我如何刷新此片段。

我還沒有看到這從android developers documentation ...但他們沒有提到的方式來更新UI。

回答

1

ViewPagers創建右和(如果有的話)左片段的實例自動。在你的情況下, C沒有被B更新,因爲它已經被添加,並且C的onCreate方法不會被調用。
如果您從片段A添加它,因爲您只有A片段和B片段,C將會被創建。
解決方案如果不存在,請不要添加您的C片段,取得C片段並更新它(使用find fragmentByTag)。

+0

謝謝你的解釋。弄清了很多事情。但我該如何更新C? –

+0

您可以在您的活動中聲明一個屬性,例如mediaPosition。用方法點擊時,從片段A和B更新它。然後在您的活動中編寫您的邏輯:如果找到片段C,只需將其媒體位置設置爲mediaPosition,然後調用尋呼機setCurrentItem。在你的片段的簡歷C播放你的媒體。如果C沒有被發現傳遞媒體位置片段C並添加C.在恢復它可以播放你的媒體。 – Devrim

+0

或者當你在pager上的currentItem變成A或B時刪除你的C片段。並且不要改變你當前的邏輯。 – Devrim

1

如果我的理解正確,問題是當片段B可見時,片段C已由ViewPager創建,以便在頁面之間平滑滾動。這意味着即使您更新了C的onResume中的界面,也會在創建片段B時調用該方法。

爲了解決這個問題,你可以覆蓋setUserVisibleHint方法來知道你的碎片居然變得活躍:

@Override 
public void setUserVisibleHint(boolean isVisibleToUser) { 
    super.setUserVisibleHint(isVisibleToUser); 

    if (isVisibleToUser == true) { 
     /* This means your fragment just became the active one. 
      You should call a GUI update function here. */ 
    } 
} 

然後,你需要有一個檢查是否有新的數據並相應地更新界面的功能。

+0

這裏的問題是,我在FragmentC的onCreateView中實例化了兩個單獨的視圖...我是否根本不調用onCreateView並將所有GUI更新代碼放在這裏? –

+0

'onCreateView'將被系統調用,你不應該自己調用它。但是你可以做的是將創建UI的代碼放到一個單獨的方法中,然後你可以從'onCreateView'和'setUserVisibleHint'調用它來確保你的GUI始終存在。如果界面非常沉重(如果可能創建兩次並不是一個好主意),那麼可以在'setUserVisibleHint'中添加一些邏輯來檢查GUI是否已經創建,並且只需更改需要更改的任何需要。 –

+0

使用setUserVisibleHint給我在FragmentB上的AsynTask錯誤...我有數據在FragmentB的onCreateView中加載。 –