如何在 Android 中以编程方式创建 TabLayout
Posted
技术标签:
【中文标题】如何在 Android 中以编程方式创建 TabLayout【英文标题】:How to create TabLayout with programmatically in Android 【发布时间】:2017-12-28 08:43:12 【问题描述】:在我的应用程序中,我想以编程方式在TabLayout
中创建选项卡,以便在ViewPager
中设置Fragments
。
我想要:如果 celebrityCount > 0 show celebritiesFragments
,如果 celebrityCount not show 这个片段还有更多...
为此,我编写了以下代码:
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】:您需要使用 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);
【讨论】:
以上是关于如何在 Android 中以编程方式创建 TabLayout的主要内容,如果未能解决你的问题,请参考以下文章
是否可以在 android 中以编程方式创建 string.xml 文件?