首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导航抽屉项一次又一次地更新子片段

导航抽屉项一次又一次地更新子片段
EN

Stack Overflow用户
提问于 2014-05-01 05:37:34
回答 1查看 319关注 0票数 0

在导航抽屉中,我有一个带有列表视图的子片段。通过第一次单击抽屉项,将显示列表视图,但当通过单击同一抽屉项关闭并重新打开抽屉时,更新子片段中的列表视图,即在子片段的列表视图中添加新的列表项以及列表视图..please帮助的先前值。我卡住了。

代码语言:javascript
复制
int select=0;
select=position-2;
        String sendto =list.get(position-2).getIMEI();
        String sendtoname=list.get(position-2).getName();
        System.out.println(deviceIMEI+"--"+sendto+sendtoname);
    Fragment fragment = new ChatFrag();
    Bundle args1 = new Bundle();
    args1.putString(ChatFrag.MYIEMI, deviceIMEI);
    args1.putString(ChatFrag.SENDERIEMI, sendto);
    args1.putString(ChatFrag.SENDERNAME,sendtoname);
    args1.putInt(ChatFrag.SELECT, select);
    fragment.setArguments(args1);
    fm.beginTransaction()
    .replace(R.id.content_frame, fragment)
    .commit();

这是如何记住索引的code..please帮助。

EN

回答 1

Stack Overflow用户

发布于 2014-05-01 07:31:59

原因是每当单击列表中的某一项时,都会调用Drawer ListView onItemClick。这就是ListView的行为。

解决这个问题的一种方法是记住last selected index,避免在下一次if the selected index is same as last selected中再次添加Fragment

另一种方法是如果已经添加了Fragment,则检查FragmentManager

代码语言:javascript
复制
    List<Fragment> fragmentList = getSupportFragmentManager().getFragments();

    if(fragmentList != null) {
        for (Fragment fragment : fragmentList) {
            if (fragment != null && fragment instanceof CustomFragment) {
                // don't add the fragment again
            }else{
            // add the fragment here
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23398466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档