如何在Android中以编程方式创建TabLayout

时间:2017-07-22 08:03:16

标签: android android-fragments android-tablayout android-tabs

在我的应用程序中,我想在TabLayout中以编程方式创建标签,以便在Fragments中设置ViewPager
我想:如果 celebrityCount> 0 显示 celebritiesFragments,如果 celebrityCount< 0 不显示此片段更多...
为此,我写下代码:

FullSearchSendData sendData = new FullSearchSendData();
        sendData.setKey(fullSearchText);
        sendData.setLoadImages(true);
        sendData.setSearchInCelebrities(true);
        sendData.setSearchInMovies(true);
        sendData.setSearchInSeries(true);
        sendData.setSearchInEpisodes(false);
        sendData.setSearchInUsers(true);
        sendData.setPageIndex(1);
        sendData.setPageSize(10);
        sendData.setMaxDistance(1);

        fullSearch_LoadLay.setVisibility(View.VISIBLE);

        InterfaceApi api = ApiClient.getClient().create(InterfaceApi.class);
        Call<FullSearchResponse> call = api.getFullSearch(sendData);

        call.enqueue(new Callback<FullSearchResponse>() {
            @Override
            public void onResponse(Call<FullSearchResponse> call, Response<FullSearchResponse> response) {
                FullSearchResponse searchResponse = response.body();
                if (searchResponse.getData().getCelebritiesCount() > 0) {
                    celebritiesCount = searchResponse.getData().getCelebritiesCount();
                    celebrityList.clear();
                    celebrityList.addAll(searchResponse.getData().getCelebrities());
                } else {
                }
                if (searchResponse.getData().getMoviesCount() > 0) {
                    movieCount = searchResponse.getData().getMoviesCount();
                    movieList.clear();
                    movieList.addAll(searchResponse.getData().getMovies());

                } else {
                }
                if (searchResponse.getData().getSeriesCount() > 0) {
                    serieCount = searchResponse.getData().getSeriesCount();
                    seriesList.clear();
                    seriesList.addAll(searchResponse.getData().getSeries());

                } else {
                }
                if (searchResponse.getData().getUsersCount() > 0) {
                    usersCount = searchResponse.getData().getUsersCount();
                    userList.clear();
                    userList.addAll(searchResponse.getData().getUsers());

                } else {
                }

                ///////////
                adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

                    @Override
                    public Fragment getItem(int position) {
                        Fragment fragment = null;
                        switch (position) {
                            case 0:
                                if (celebritiesCount > 0)
                                    fragment = FullSearchCelebritiesFragment.getInstance(celebrityList, celebritiesCount, fullSearchText);
                                break;
                            case 1:
                                if (movieCount > 0)
                                    fragment = FullSearchMovieFragment.getInstance(movieList, movieCount, fullSearchText);
                                break;
                            case 2:
                                if (serieCount > 0)
                                    fragment = FullSearchSeriesFragment.getInstance(seriesList, serieCount, fullSearchText);
                                break;
                            case 3:
                                if (usersCount > 0)
                                    fragment = FullSearchUsersFragment.getInstance(userList, usersCount, fullSearchText);
                                break;
                        }
                        return fragment;
                    }

                    @Override
                    public CharSequence getPageTitle(int position) {
                        String title = null;
                        switch (position) {
                            case 0:
                                if (celebritiesCount > 0)
                                    title = "Celebrities";
                                break;
                            case 1:
                                if (movieCount > 0)
                                    title = "Movies";
                                break;
                            case 2:
                                if (serieCount > 0)
                                    title = "Series";
                                break;
                            case 3:
                                if (serieCount > 0)
                                    title = "Users";
                                break;
                        }
                        return title;
                    }

                    @Override
                    public int getCount() {
                        int count = 0;
                        if (celebritiesCount > 0) {
                            count = 1;
                        }
                        if (movieCount > 0) {
                            count = 2;
                        }
                        if (serieCount > 0) {
                            count = 3;
                        }
                        if (usersCount > 0) {
                            count = 4;
                        }
                        return count;
                    }
                };
                ///////////

                fullSearch_ViewPager.setAdapter(adapter);
                fullSearch_tabLayout.setupWithViewPager(fullSearch_ViewPager);
                fullSearch_tabLayout.setTabMode(TabLayout.MODE_FIXED);

                fullSearch_LoadLay.setVisibility(View.GONE);
            }

            @Override
            public void onFailure(Call<FullSearchResponse> call, Throwable t) {
                fullSearch_LoadLay.setVisibility(View.GONE);
                fullSearch_ReloadLay.setVisibility(View.VISIBLE);
            }
        });

但是告诉我这个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
                                                                               at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:380)
                                                                               at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:375)
                                                                               at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:103)
                                                                               at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1034)
                                                                               at android.support.v4.view.ViewPager.populate(ViewPager.java:1182)
                                                                               at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
                                                                               at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:540)
                                                                               at in.app.test.Activities.FullSearch$1.onResponse(FullSearch.java:208)
                                                                               at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:135)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5349)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

我该如何解决?请帮帮我

1 个答案:

答案 0 :(得分:0)

您需要使用 addTab()方法 TabLayout 添加标签

以下是示例代码:

private void setUpTab() {
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), this);
         for (int i = 0; i < viewPagerAdapter.getCount(); i++) {
             tabLayout.addTab(
                     tabLayout.newTab()
                             .setText(viewPagerAdapter.getPageTitle(i)));
         }
         tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        viewPager.setAdapter(viewPagerAdapter);

     }