如何从另一个片段更新一个片段的ListView?
在另一个Fragment
内的Fragment
事件发生后,如何传达ListFragment
的listview
?
How can I communicate a listview
of a ListFragment
after an event of a Fragment
inside another Fragment
?
在ListFragment
(片段A)中,我有一种刷新ListView
的方法.而且,在单击Fragment
内的Button
之后,我需要刷新它,这是另一个Fragment
的子级(片段b)
In the ListFragment
(fragment A) I have a method to refresh the ListView
. But also, I need to refresh it after a click of a Button
inside a Fragment
, wich is child of another Fragment
(fragment b)
就像Fragment
A(listFragment)| Fragment
B(详细视图)
(片段C-B的子片段)
Its like Fragment
A (listFragment) | Fragment
B (detailview)
(fragment C - child fragment of B)
我该怎么办?
您可以通过其标签访问另一个Fragment
:
You can access another Fragment
by its tag:
// find your fragment
YourFragment f = (YourFragment) getSupportFragmentManager().findFragmentByTag("yourFragTag");
// update the list view
f.updateListView();
Fragment
的标签是在附加到容器布局时设置的:
The tag of your Fragment
is set when it is attached to a container layout:
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.frameBuy, YourFragment.newInstance(), "yourFragTag").commit();
因此,当您单击Button
时,请通过其标签找到要刷新的Fragment
,然后调用刷新方法.
So when you click your Button
, find the Fragment
you want to refresh by its tag, and then call your refresh method.
如果您使用的是ViewPager
,这是获取Fragments
标签的方法:
IF you are using a ViewPager
, this is how to get the Fragments
tag:
/**
* 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;
}