从另一个Activity(不是父Activity)更新片段中的ExpandableListView

时间:2019-01-11 10:01:29

标签: android android-fragments odoo-11 android-expandable-list-view

在我的情况下,我有两个活动,其中包含一个带有相对片段的ViewPager。 所以情况是这样的:

Activity1(ViewPager):片段1,片段2,片段3,片段n ... Activity2(ViewPager):FragmentA,FragmentB,FragmentC,Fragment m ...

Fragment2包含项目的ExpandibleListView,由OnClickListener调用带有startActivity(intent)的Activity2,以显示Details和其他内容。 Activity2中的FragmentB可能会将一个信息(即选定元素的名称)更改为ExpandibleListView,当我更改项目的名称时,我会使用以下代码刷新当前的Fragment B(通过分离并重新附加当前的Fragment):

Fragment frg_feedDetails = adapter.getItem(viewPager.getCurrentItem());
final FragmentTransaction frg_feedDetailsTransaction = getSupportFragmentManager().beginTransaction();
frg_feedDetailsTransaction.detach(frg_feedDetails);                                     
frg_feedDetailsTransaction.attach(frg_feedDetails);                                      
frg_feedDetailsTransaction.commitAllowingStateLoss();
// Update Activity title               
Objects.requireNonNull(getSupportActionBar()).setTitle(projectName + ":" + projectFeed.browse(currentObject).get("name"));

问题出现在用户按下向后或单击顶部向后的箭头并从FragmentB返回到包含ExpandibleListView的Activity1时。 如果用户这样做时带有项目列表的ExpandibleListView没有收到新名称,则在此之前,我必须在Activity1内部切换一些片段。

为避免这种情况,我将此代码添加到Activity1中:

public void onResume() {
    super.onResume();
    /* FIXME maybe is not the best way to do that but without this override if user change name
     * of feed when press back button (or back arrow) the expandible listview don't refresh items name
     * by adding and removing fragment we get the results
     */
    if (fragmentProjectFeeds != null && fragmentProjectFeeds.isVisible()) {
        Fragment frg_projectFeeds = adapter.getItem(viewPager.getCurrentItem());
        final FragmentTransaction ft_projectFeeds = getSupportFragmentManager().beginTransaction();
        ft_projectFeeds.detach(frg_projectFeeds);
        ft_projectFeeds.attach(frg_projectFeeds);
        ft_projectFeeds.commit();
    }
}

我希望有一些最好的方法来实现此目标,因为onResume,如果用户按回去并且不更改名称,它将分离并重新附加Fragment。

我会要求社区提供一些更好的方法来处理这种情况。

0 个答案:

没有答案