2014-03-13 247 views
3

如何在Fragment事件發生後在另一Fragment內傳遞ListFragmentlistview如何從另一個片段更新片段的Lis​​tView?

ListFragment(片段A)我有一個方法來刷新ListView。但同時,我需要一個FragmentButton的點擊後刷新它,至極是另一個Fragment(片段B)孩子

它像Fragment A(listFragment)| Fragment B(detailview) (片段C - B的子片段)

我該怎麼辦?

+1

,我保存到列表片段的引用在一個全局變量中,並在任何地方使用它,只要確保在重新創建列表時確保使用對listView的新引用更新變量。 – harshal

+0

這樣做的最好方法是使用包含的Activity來處理通信。首先在你的Fragment中創建一個接口,在Activity中實現它,然後從你的Activity找到你想要共享信息的Fragment。然後使用片段的引用調用該方法來更新列表。 –

回答

3

您可以通過它標籤訪問另一個Fragment

// find your fragment 
YourFragment f = (YourFragment) getSupportFragmentManager().findFragmentByTag("yourFragTag"); 

// update the list view 
f.updateListView(); 

Fragment的標籤設置,當它是連接到容器佈局:

FragmentManager fm = getSupportFragmentManager(); 
fm.beginTransaction().replace(R.id.frameBuy, YourFragment.newInstance(), "yourFragTag").commit(); 

所以,當你點擊你的Button,找到想要刷新它的標記的Fragment,然後調用你的刷新方法。

如果您正在使用一個ViewPager,這是如何得到Fragments標籤:

/** 
    * Gets the fragment tag of a fragment at a specific position in the viewpager. 
    * 
    * @param pos the pos 
    * @return the fragment tag 
    */ 
    public String getFragmentTag(int pos){ 
     return "android:switcher:"+R.id.yourViewPagerId+":"+pos; 
    } 
+0

它很棒!謝謝! –

0

您可以用幾個簡單的步驟做:

  1. 創建組件的監聽器接口收聽來自FragmentC的按鈕點擊事件。例如:

    public interface FragmentCButtonListener {void 0} public void onButtonClicked(); }

  2. 在FragmentC中添加一個方法來允許監聽器註冊。 FragmentC將跟蹤監聽器,並在按鈕被點擊後立即調用監聽器回調。例如:

    public class FragmentC extends Fragment {FragmentCButtonListener myListener; public void registerListener(FragmentCButtonListener listener){ myListener = listener; }}

  3. FragmentA應該實現FragmentCButtonListener接口,自身註冊爲一個監聽器FragmentC,當它從FragmentC接收回調刷新列表視圖。例如:

    public class FragmentC extends Fragment implements FragementCButtonListener { FragmentC fragmentC; public void onCreate(){ fragment = new FragmentC(); 片段。registerListener(this); }

    public void onButtonClicked() { 
        //refresh the list view 
    } 
    

    }

請注意,我假設FragmentA有樣品中FragmentC參考。如果沒有,只要確保所有片段的容器類自己註冊爲FragmentC的偵聽器。一旦容器類從FragmentC接收回調,容器類可以像FragmentA一樣更新listview。

0

請按照下列步驟 我們有兩個片段稱爲AddFragmentListFragment,並且在對第一個片段添加一個項目你想要的更新列表清單上顯示的片段(什麼樣的巫術是這個!)。

步驟1創建AddFragment的類級別與要由另一個人(ListFragment)來實現的方法中的監聽器接口和創建接口類型變量

public class AddFragment extends Fragment{ 
//listener varriable 

//listener 
    public interface OnCategoryAddedListener{ 
    public void refreshList(); 
} 
private static OnCategoryAddedListener meAddListener; 
} 

步驟2創建類套準調整方法相同AddFragment類的水平並且在任何情況下是反芻按鈕點擊或在烏爾應用叫喊設置listenter可變

public class AddFragment extends Fragment{ 


public void registerListener(OnCategoryAddedListener listener) 
{ 
    meAddListener = listener; 
} 
} 

步驟3(被認爲是在機器人:-)粗魯事件)檢查偵聽器對象meAddListener變量和調用接口, 在莎士比亞的概括地說這意味着「for thou who implement ye interface and brought the method within ur class shelter, I shall give u my utmost privilege and blessing to …. 」

步驟4在ListFragment實現AddFragment的界面,沒有回頭路可走只是去實現它的方法。在這種方法你剛剛叫abracadabra重新填充UR列表或任何類型的可更新的Android視圖對象的...和UR做

public class ListFragment extends Fragment implements AddFragment.OnCattegoryAddedListener{ 
//refer to AddFragment 
AddFragment addFragment; 

//once the fragment is within the memory scope u instantiate AddFragment 
//and register listener with AddFragment context which inherently implement OnCategoryAddedListener 

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     categoryAddFragment = new CategoryAddFragment(); 
     categoryAddFragment.registerListener(this); 

    } 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    fillList(); 

} 

public void fillList() { 

    ArrayAdapter<String> catArrayAdapter = new ArrayAdapter<>(context,android.R.layout.simple_list_item_1,getItems()); 
    setListAdapter(catArrayAdapter); 

} 
//le interface method u can count on to refresh ur list content 
public void refreshList(){ 
fillList(); 
} 

check this out for a little more 享受我的情況下,在Java魔術

相關問題