以编程方式更改TabLayout的选项卡但不更新ViewPager的片段

时间:2016-09-06 06:29:47

标签: android android-fragments android-viewpager android-tablayout

我正在开发一款Android应用。在我的应用程序中,我使用TabLayout和ViewPager。当选择底部导航栏中的项目时,我需要以编程方式更新TabLayout及其片段的选项卡。我可以更新TabLayout的选项卡。但寻呼机的片段没有更新。

这就是问题所在:

enter image description here

正如您在上面所看到的,标签已更改,但其片段未更改。列表片段始终显示。所有片段与选择的第一个选项卡完全相同。我的意思是片段根本没有变化。请参阅下面的代码。

这是我的选项卡和视图分页器的XML文件:

 <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">


        <android.support.design.widget.TabLayout
            android:id="@+id/ma_tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabMode="fixed" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:id="@+id/ma_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

这是我的整个活动:

public class MainActivity extends AppCompatActivity {

    private BottomBar bottomBar;
    private TabLayout topTabLayout;
    private ViewPager mainViewPager;
    private MainPagerAdapter pagerAdapter;
    private ArrayList<Fragment> pagerFragments;
    private ArrayList<String> pagerTitleList;
    protected TfrApplication app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        app = (TfrApplication)getApplication();
        setContentView(R.layout.activity_main);
        initialize();
        initViews();
        setUpViews();
    }

    private void initialize()
    {
        pagerFragments = new ArrayList<Fragment>();
        pagerTitleList = new ArrayList<String>();
    }

    private void initViews()
    {
        bottomBar = (BottomBar)findViewById(R.id.ma_bottom_action_bar);
        mainViewPager = (ViewPager)findViewById(R.id.ma_viewpager);
        topTabLayout = (TabLayout)findViewById(R.id.ma_tab_layout);
    }

    private void setUpViews()
    {
        pagerAdapter = new MainPagerAdapter(getSupportFragmentManager(),pagerFragments,pagerTitleList);
        mainViewPager.setAdapter(pagerAdapter);
        topTabLayout.setupWithViewPager(mainViewPager);
        setUpBottomBar();
    }

    private void clearPagerFragments()
    {
        if(pagerAdapter!=null && pagerFragments!=null && pagerFragments.size()>0)
        {
            pagerFragments.removeAll(pagerFragments);
            pagerTitleList.removeAll(pagerTitleList);
            pagerAdapter.notifyDataSetChanged();
        }
    }

    private void setUpNewsPagerFragments()
    {
        NewsFragment latestNewsFragment = new NewsFragment();
        Bundle latestNewsBundle = new Bundle();
        latestNewsBundle.putInt(NewsFragment.FIELD_CATEGORY_ID,0);
        latestNewsBundle.putInt(NewsFragment.FIELD_LEAGUE_ID,0);
        latestNewsBundle.putInt(NewsFragment.FIELD_COUNTRY_ID,0);
        latestNewsBundle.putInt(NewsFragment.FIELD_TEAM_ID,0);
        latestNewsFragment.setArguments(latestNewsBundle);
        pagerTitleList.add("LATEST");
        pagerFragments.add(latestNewsFragment);

        PrioritizedTeamsFragment teamsFragment = new PrioritizedTeamsFragment();
        pagerTitleList.add("TEAMS");
        pagerFragments.add(teamsFragment);

        NewsFragment articlesFragment = new NewsFragment();
        Bundle articlesBundle = new Bundle();
        articlesBundle.putInt(NewsFragment.FIELD_CATEGORY_ID, 0);
        articlesBundle.putInt(NewsFragment.FIELD_LEAGUE_ID, 0);
        articlesBundle.putInt(NewsFragment.FIELD_COUNTRY_ID, 0);
        articlesBundle.putInt(NewsFragment.FIELD_TEAM_ID, 0);
        articlesFragment.setArguments(articlesBundle);
        pagerTitleList.add("ARTICLES");
        pagerFragments.add(articlesFragment);

        TopFragment topFragment = new TopFragment();
        pagerTitleList.add("TOP 10");
        pagerFragments.add(topFragment);
    }

    private void setUpMatchesPagerFragments()
    {
        MatchesFragment matchesFragment = new MatchesFragment();
        pagerTitleList.add("MATCHES");
        pagerFragments.add(matchesFragment);

        StatisticsFragment statisticsFragment = new StatisticsFragment();
        pagerTitleList.add("STATISTICS");
        pagerFragments.add(statisticsFragment);
    }

    private void setUpBottomBar()
    {
        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(int tabId) {
                switch (tabId){
                    case R.id.bottom_tab_news:
                        clearPagerFragments();
                        setUpNewsPagerFragments();
                        pagerAdapter.notifyDataSetChanged();
                        break;
                    case R.id.bottom_tab_matches:
                        clearPagerFragments();
                        setUpMatchesPagerFragments();
                        pagerAdapter.notifyDataSetChanged();
                        topTabLayout.getTabAt(0).select();
                        break;

                    case R.id.bottom_tab_meme:
                        Toast.makeText(getBaseContext(),"MEME",Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.bottom_tab_settings:
                        Toast.makeText(getBaseContext(),"Settings",Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }
}

我展示了整个活动,因为代码处理该问题的整个活动。此外,整个活动仅包含用于创建选项卡,查看寻呼机和底栏的代码。所有都是连接的。

这是我的查看寻呼机适配器:

public class MainPagerAdapter extends FragmentPagerAdapter {
    private ArrayList<Fragment> fragmentList;
    private ArrayList<String> titleList;

    public MainPagerAdapter(FragmentManager fragmentManager,ArrayList<Fragment> fragmentsParam,ArrayList<String> titlesParam)
    {
        super(fragmentManager);
        this.fragmentList = fragmentsParam;
        this.titleList = titlesParam;
    }

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

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

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

为什么更改标签时不会更改视图寻呼机的内容或片段?我的代码出了什么问题?

正如你在底部项目选择的监听器上看到的那样,我试图像这样设置所选的片段:

topTabLayout.getTabAt(0).select();

我也尝试了这个:

mainViewPager.setCurrentItem(0);

两者都不起作用。

3 个答案:

答案 0 :(得分:0)

尝试添加一个方法来交换片段列表并在适配器中调用notifyDataSetChanged(),如下所示:

public void notifyDataSetChanged(List<Fragment> list) {
    fragmentList.removeAll();
    fragmentList.addAll(list);
    notifyDataSetChanged();
}

更改活动中的数据并调用adapter.notifyDataSetChanged()可能无法更新您的视图。您可以登录getItem()以检查适配器是否知道数据集已更改。

答案 1 :(得分:0)

只需在MatchesFragment类中检查您要膨胀的布局。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.matches_fragment, container, false);
}

答案 2 :(得分:0)

如果您的标签页设置正确,请在设置标签页的代码下再次运行:

mainViewPager.setAdapter(pagerAdapter);

这是我的代码:

selectedTab = 0; tabLayout.getTabAt(selectedTab).select(); viewPager.setAdapter(tabsAdapter);