如何在 ViewPager 中重新创建所有片段:

How to recreate all fragments in ViewPager:(如何在 ViewPager 中重新创建所有片段:)
本文介绍了如何在 ViewPager 中重新创建所有片段:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题,我有一个带有 ViewPager 和一个 ViewPagerAdapterFragmentActivity.

I have this problem, I have a FragmentActivity with a ViewPager and a ViewPagerAdapter.

在这个 ViewPager 中,我有 3 个 Fragments,每个片段都包含一个 TableLayout,它使用 2dArrayList 以编程方式填充.

In this ViewPager I have 3 Fragments that each of them contains a TableLayout that gets populated programmaticly using a 2dArrayList.

单击 FragmentActivity 布局中的按钮,我过滤 2dArrayList 中的数据,基本上需要重新创建所有 3 个 Fragments使用新数据集(在所有数据集中运行 onCreateView 方法).

on a click of a button in the FragmentActivity layout, I filter the data in the 2dArrayList and basically need to recreate all 3 Fragments with the new dataset (run the onCreateView method in all of them).

所以我为这个按钮创建了一个 onClickListener:

So I create an onClickListener for this button:

 private class SlicerSelectedOnClickListener implements OnClickListener {

    private ODSharedSlicer slicer;

    public SlicerSelectedOnClickListener(ODSharedSlicer slicer) {
        super();
        this.slicer = slicer;
    }

    public void onClick(View v) {
        String slicerValue = ((Button) v).getText().toString();
        Log.d(TAG,"Slicer value: "+ slicerValue +" chosen");
        application.currentReport.getODSharedSlicers().get(0).getSharedSlicerSelectedMemebers().add(slicerValue);

        ViewGroup parent = (ViewGroup) ((Button) v).getParent();

        for (int i = 0; i < parent.getChildCount(); i++) 
        {
            if ((parent.getChildAt(i) != v) && (parent.getChildAt(i) instanceof ToggleButton))
            {
                ((ToggleButton) parent.getChildAt(i)).setChecked(false);
            }
        }

        mPagerAdapter.notifyDataSetChanged();
    }
}

如您所见,我有一个 mPagerAdapter.notifyDataSetChanged();如此处建议:

As you can see I have a mPagerAdapter.notifyDataSetChanged(); As suggested here:

如何故意用viewpager重新创建片段?

在此方法结束时,因此我希望适配器重新创建所有 Fragment,但任何片段中的 onCreateView 都不会运行.

at the end of this method, So I expect the adapter to recreate all the Fragments, but the onCreateView from any of the fragments never runs.

任何人都可以建议我做错了什么.任何帮助将不胜感激.

Can any one suggest what am I doing wrong. any assistance would be appreciated.

推荐答案

正如@Luksprog 在这里所建议的,将我的适配器类更改为:

As been suggested here by @Luksprog, changing my adapter class to this:

public class ViewPagerAdapter extends FragmentPagerAdapter 
{
private List<Fragment> fragments;

/**
 * @param fm
 * @param fragments
 */
public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    this.fragments = fragments;
}

/* (non-Javadoc)
 * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
 */

@Override
public Fragment getItem(int position) {
    return this.fragments.get(position);
}

/* (non-Javadoc)
 * @see android.support.v4.view.PagerAdapter#getCount()
 */

@Override
public int getCount() {
    return this.fragments.size();
}

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}  
}

解决了我的问题.

我所做的是添加这个方法:

what I did was to add this method:

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}  

这篇关于如何在 ViewPager 中重新创建所有片段:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)