How to create TabLayout with programmatically in Android

时间秒杀一切 提交于 2019-12-23 05:07:52

问题


In my application i want to programmatically create Tabs in TabLayout for setting Fragments in ViewPager.
I want : If celebrityCount > 0 show celebritiesFragments, if celebrityCount < 0 not show this fragments ond more ...
For this, i write below code :

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);
            }
        });

But show me this error :

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)

How can I fix it? please help me


回答1:


You need to add tabs to TabLayout using addTab() method

Here is sample code :

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);

     }


来源:https://stackoverflow.com/questions/45252193/how-to-create-tablayout-with-programmatically-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!