且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从另一个片段更新一个片段的Lis​​tView?

更新时间:2022-11-09 16:26:41

您可以通过其标签访问另一个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;
    }