简体   繁体   English

Android:从PagerAdapter获取片段

[英]Android: Get fragment from PagerAdapter

So I'm trying to implement a ViewPager with a custom FragmentStatePagerAdapter. 因此,我尝试使用自定义FragmentStatePagerAdapter实现ViewPager。 In my MainActivity, I'm trying to access the fragment being displayed so I can modify the children inside of that fragment's view. 在我的MainActivity中,我试图访问显示的片段,以便可以修改该片段视图内的子代。

Here's the custom fragment: 这是自定义片段:

public class CustomFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_custom, container, false);
        return rootView;
    }
}

Here's the FragmentStatePagerAdapter: 这是FragmentStatePagerAdapter:

public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

    private int NUM_PAGES;

    public ScreenSlidePagerAdapter(FragmentManager fm, int NUM_PAGES) {
       super(fm);
       this.NUM_PAGES = NUM_PAGES;
    }

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

    @Override
    public Fragment getItem(int position) {
        return new CustomFragment();
    }

}

Here's the important part in the MainActivity. 这是MainActivity中的重要部分。 This is where I try and get the fragment currently being displayed by the pager adapter. 在这里,我尝试获取寻呼机适配器当前正在显示的片段。 mPagerAdapter.instantiateItem returns a fragment, but when I call primaryFragment.getView(), I get a null pointer. mPagerAdapter.instantiateItem返回一个片段,但是当我调用primaryFragment.getView()时,我得到了一个空指针。

    moveViewPagerToIndex(); // Move the view pager to NUM_PAGES / 2 to start
    primaryFragment = (CustomFragment) mPagerAdapter.instantiateItem(mViewPager, mViewPager.getCurrentItem());
    updateFragment();

The updateFragment(): updateFragment():

    ArrayList<Button> arrayList = Utility.getCellsFromCalendarFragment(primaryFragment.getView());
    for (int i = 0; i < arrayList.size(); i++) {
         // do stuff with all the cells
    }

The Utility.getCellsFromCalendarFragment(View rootView) Utility.getCellsFromCalendarFragment(查看rootView)

public static ArrayList<Button> getCellsFromCalendarFragment(View rootView) {
    ArrayList<Button> cellList = new ArrayList<>();
    cellList.add((Button) rootView.findViewById(R.id.cell00));
    cellList.add((Button) rootView.findViewById(R.id.cell01));
    cellList.add((Button) rootView.findViewById(R.id.cell02));
    // There are a lot more than 3 cells... There's roughly 50 cells.
    return cellList
}

I ran through the debugger in Android Studio and watched the variable 'primaryFragment'. 我在Android Studio中运行了调试器,并观看了变量'primaryFragment'。 When I call mViewPager.instantiateItem(...), I get an actual CustomFragment back. 当我调用mViewPager.instantiateItem(...)时,我得到了一个实际的CustomFragment。 The problem is that 'primaryFragment' now has a null view so when I try to access the view to modify the cells, I get a NullPointerException. 问题在于'primaryFragment'现在具有空视图,因此当我尝试访问该视图以修改单元格时,会收到NullPointerException。

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.view.View android.view.View.findViewById(int)'

How do I get the view from this fragment? 如何从该片段中获取视图? Is this even the right way to get the fragment from the view pager? 这是从视图分页器中获取片段的正确方法吗? I've been ripping my hair out over this. 我一直在为此扯头发。 I just want to get the fragment being displayed and modify its view and the children. 我只想获取正在显示的片段并修改其视图和子级。

Thanks! 谢谢!

FragmentStatePagerAdapter.instantiateItem() creates new Fragment by calling getItem() . FragmentStatePagerAdapter.instantiateItem()通过调用getItem()创建新的Fragment Your getItem() creates a new instance of CustomFragment every time it is called. 每次调用getItem()都会创建一个CustomFragment的新实例。 So when you call isntantiateItem() you are getting a new instance of CustomFragment and not the one that is being used by the adapter. 所以,当你调用isntantiateItem()你得到的新实例CustomFragment而不是正在使用的适配器一个。

You may need to store the instances of CustomFragment in you adapter and write a method to get them by index. 您可能需要将CustomFragment实例存储在适配器中,并编写一种方法来按索引获取它们。

I am not sure about your full use case so I can not advice on how you could go on to modify your adapter. 我不确定您的完整用例,因此无法建议您如何继续修改适配器。 I mean I do not know which fragment you want to access, if it is the one that is currently shown or some distant one that is far gone out of screen. 我的意思是我不知道您要访问哪个片段,是当前显示的片段还是远距离显示的片段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM