FragmentStatePagerAdapter在当前选项卡之前添加选项卡而不刷新

时间:2018-10-30 08:43:21

标签: android android-viewpager android-tablayout

我知道在SO中有一些问题,但是没有人帮助我。

这是我的情况:

  • 我有一个带有N个动力学标签的布局。
  • 其中一个标签(称为“活动”)具有包含某些真假项目的列表视图。
  • 更改某些项目的值会在当前标签页之前(称为“详细信息”)添加或删除标签页。

解释这种情况:

开始情况

标签(当前选中的是粗体)

首页-详细信息-客户-工厂-活动-简而言之

  • 在“活动”标签中,我更改了此复选框的选中值,这是我要成为的新情况:

首页-客户-工厂-活动-其他

这就是我得到的:

我只有两个要点:


1:选择了 SOMETHINGELSE 标签页眉,并显示了 ACTIVITIES 内容

2:选择了 ACTIVITIES 标签页眉,并显示了 SOMETHINGELSE 内容

我想要添加的标题是删除并删除显示 ACTIVITIES 选项卡并显示 ACTIVITIES 内容的标题。

在这很长的介绍之后,这是我的代码:

FragmentStatePagerAdapter

private class PagerAdapter extends FragmentStatePagerAdapter {
    private int mNumOfTabs;
    private SparseArray<MyFragmentsInterface> activeFragments;
    private ArrayList<MyHeaderEnum> titles;

    MyFragmentsInterface getNewMeterFragmentIfAvailable(int position) {
        try {
            return activeFragments.get(position);
        } catch (Exception ex) {
            return null;
        }
    }

    PagerAdapter(FragmentManager fm, int NumOfTabs, ArrayList<MyHeaderEnum> titles) {
        super(fm);
        this.activeFragments = new SparseArray<>();
        this.mNumOfTabs = NumOfTabs;
        this.titles = titles;
    }


    @Override
    public int getItemPosition(@NonNull Object object) {
        //I played a lot with this part to manage the addiction-removal of tabs before current, but without succeed
        return super.getItemPosition(object);
    }

    void removetitle(int position) {
        if (position != -1 && position < titles.size()) {
            titles.remove(position);
            mNumOfTabs--;
            notifyDataSetChanged();
        }
    }

    void addTitle(int position, MyHeaderEnum title) {
        titles.add(position, title);
        mNumOfTabs++;
        notifyDataSetChanged();
    }

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

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (openedIntervention.isInPlace()) {
            activeFragments.get(position).save(false);
        }
        activeFragments.remove(position);

        super.destroyItem(container, position, object);
    }

    @Override
    public Fragment getItem(int position) {
        MyHeaderEnum currentTitle = titles.get(position);

        Bundle bundle = new Bundle();
        bundle.putString(_intervention_id_key, interventionID);

        currentFragment = newFragment;

        //this is to retrieve the correct content for the header
        switch (currentTitle) {
            case DETTAGLIO:
                InterventoDettaglioFragment interventoDettaglioFragment = new InterventoDettaglioFragment();
                interventoDettaglioFragment.setArguments(bundle);
                newFragment = interventoDettaglioFragment;
                return interventoDettaglioFragment;
                //... and so on
        }
        return null;
    }

    @NonNull
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        MyFragmentsInterface fragment = (MyFragmentsInterface) super.instantiateItem(container, position);

        activeFragments.put(position, fragment);

        return fragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles.get(position).getValue();
    }
}

添加/删除标签的方法(活动中):

public void manageTabBefore() {
    //pre-code to decide if it has to be added or not

    if (hasToBeAdded) {
        isTabAdded = true;
        adapter.addTitle(podTabIndex, MyHeaderEnum.POD);
    } else if (hasToBeRemoved) {
        isTabAdded = false;
        adapter.removetitle(podTabIndex);
    }
}

已更改选项卡上的Tab布局

    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() 
    {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            //I also played a looooot with this
            int position = tab.getPosition();
            viewPager.setCurrentItem(position);
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });

我快要放弃了,该怎么做才能在不刷新当前标签不更改所选标签之前添加一个标签?

0 个答案:

没有答案