FragmentPagerAdapter与ViewPager和两个片段。转到第一个与第二和第一更新的文字 [英] FragmentPagerAdapter with ViewPager and two Fragments. Go to the first from the second and update first's text

查看:181
本文介绍了FragmentPagerAdapter与ViewPager和两个片段。转到第一个与第二和第一更新的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉的 FragmentPagerAdapter ,所以这将是那些问题之一,我们(你)阅读说明批判。

I'm not familiar with FragmentPagerAdapter, so this is going to be one of those questions that we (you) read the description critically.

结构:我有一个 FragmentPagerAdapter ($ C $低于C),将举行两个片段的时间。第一个显示预定摘编,和第二个书名列表。

Structure: I have a FragmentPagerAdapter (code below), that will hold two fragments at a time. The first displays book excerpts, and the second a list of book titles.

目标:我要达到什么样的标题描述:用户可以导航到寻呼机的第二个片段,点击标题,然后我想用户回迁第一个片段,告诉第一个片段来更新文本。第一个片段有一个 triggerRefresh 方法的。

Goal: I want to achieve what is described in the title: the user can navigate to the second fragment in the pager, click on a title, and then I want to move the user back to the first fragment and tell the first fragment to update the text. The first fragment has a triggerRefresh method for that.

code:我相信我的问题发生,因为这样 FragmentPagerAdapter 重用/创建片段(我不明白)。这是我的类:

Code: I believe my problem happens because of the way FragmentPagerAdapter reuses/creates the Fragments (which I don't understand). This is my class:

static class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    @Override
    public Fragment getItem(int position) {
        switch(position) {
        case 0:
            return new ExcerptsFragment();
        case 1:
            return new BookListFragment();
        default:
            throw new IllegalArgumentException("not this many fragments: " + position);
        }
    }
}

这是怎么产生的相关成员:

This is how I created the relevant members:

ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
MyFragmentPagerAdapter mFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mFragmentPagerAdapter);

这是我在我的活动在其他地方尝试过,当我收到回调从书名片段与选定的标题:

And this is what I've tried elsewhere in my Activity, when I receive the callback from the book titles Fragment with the title selected:

mViewPager.setCurrentItem(0); // back to excerpts screen page. It's OK.
// Here's the problem! How to identify the fragment 0 
// to ExcerptsFragment and call its triggerRefresh()?!?

一系列的问题:

调用适配器的 getView()将无法工作,因为它会返回 ExcerptsFragment 的新实例,这当前未连接(如预期,引发异常)的人。

Calling the adapter's getView() won't work because it will return a new instance of ExcerptsFragment, which is not the one currently attached (as expected, throws the exception).

我在这里看到很多人(例如)在 getView只是存储碎片( )。是对的吗?因为通过看官方的例子,似乎是一个反模式,我(击败握住物品的自动参考)。而这也是这里的意见这里(并期待我的权利)。

I've seen many people here (example) just storing fragments in the getView(). Is that right? Because by looking at the official examples, seems like an anti-pattern to me (defeat the automatic reference by holding the items). And that is also the opinion here and here (and looks right to me).

有什么建议?我也不会感到惊讶,如果我没有理解这一切一位的......

Any suggestions? I wouldn't be surprised if I'm not understanding all of this one bit...

推荐答案

抱歉这个问题,我认为这是一小时。

Sorry about that question, I think it was the hour.

要解决这个问题,我实现了这个解决方案原样。似乎工作得很好。所以,我相信这是只是找出它的ID是如何命名找到(当前连接)片段实例的问题。上面的链接解释它是如何做。

To solve that problem, I implemented this solution as is. Seems to work just fine. So, I believe it was just a matter of finding the (currently attached) fragment instance by figuring out how its Id is named. The link above explains how it's made.

我选择回答删除它自己的问题,而不是因为我相信像我这样的新手对这些寻呼机会从一个真正的情况中受益。大多数我见过谈最为牵挂的理论,这是正确的方式BTW的答案......但没有一个真实的例子来工作,有时我这样的人迷失方向。

I opted to answer my own question instead of deleting it because I believe novices like me on these pagers will benefit from a "real case scenario". Most of the answers I've seen talk most about the theory, which is the right way BTW... but without a real example to work on sometimes people like me get lost.

总之,这里是我所需要的最后一块code(上面的注释部分):

Anyway, here is the last piece of code that I needed (the commented part above):

int n = 0;
mViewPager.setCurrentItem(n); // in the question I had stopped here.

ExcerptsFragment f = (ExcerptsFragment) ContainerActivity.this
        .getSupportFragmentManager().findFragmentByTag(getFragmentTag(n));
f.triggerRefresh();

// ... below the helper method: used the solution from the link.

private String getFragmentTag(int pos){
    return "android:switcher:"+R.id.pager+":"+pos;
}

所以,我有一种感觉,这是一个强大的解决方案,因为我没有抱着任何引用片段(从而冒着被引用过时)。我一直在我自己的code在最低限度,从而最大限度地减少了我的机会做一些愚蠢的。

So, I'm having a feeling that this is a robust solution, because I'm not holding any references to fragments (thus risking the references being outdated). I kept my own code at a minimum, therefore minimizing the chances of me doing something stupid.

当然,如果你有什么要补充,向我们展示了,告诉什么是错在做或者什么可以改进,我会很高兴听到你的声音。

这篇关于FragmentPagerAdapter与ViewPager和两个片段。转到第一个与第二和第一更新的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆