First tab does not show the fragment.
I am using the following method to create new tabs.
public static void addNewTab(String TimelineName, String id) {
TabLayout.Tab tab = tabs.newTab();
tab.setText(TimelineName)
tabs.addTab(tab);
adapter.addTab(TimelineName);
}
adapter.add(Timeline) is a method in the following class:
public class TabsPagerAdapter extends PagerAdapter {
private final ArrayList<CharSequence> myTabList = new ArrayList<>();
int TabPosition = 0;
public static List<Bean_TimelineData> timelineData;
public static TimeLine_Adapter timeLineAdapter;
public static RecyclerView timelineRecyclerView;
// CREATE NEW TAB
public void addTab(String title) {
myTabList.add(title);
notifyDataSetChanged();
}
public void removeTab() {
if (!myTabList.isEmpty()) {
myTabList.remove(myTabList.size() - 1);
notifyDataSetChanged();
}
}
@Override
public int getCount() {
return myTabList.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
LinearLayout linearLayout = new LinearLayout(container.getContext());
timelineRecyclerView = new RecyclerView(container.getContext());
SwipeRefreshLayout swipeContainer;
timelineData = new ArrayList<Bean_TimelineData>();
timeLineAdapter = new TimeLine_Adapter(container.getContext(), timelineData, TabId);
timelineRecyclerView.setLayoutManager(new LinearLayoutManager(container.getContext()));
timelineRecyclerView.setAdapter(timeLineAdapter);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setBackgroundColor(Color.parseColor("#f2f2f2"));
container.addView(linearLayout, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(0, 20, 0, 0);
timelineRecyclerView.setLayoutParams(params);
linearLayout.addView(timelineRecyclerView);
return linearLayout;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public CharSequence getPageTitle(int position) {
return myTabList.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
But the the result i get is this:
Rest all tabs are working correctly:
If i swipe tab from 1 to 3 and then go back to 1 the fragment appears back correctly.
I have checked a few post regarding this such as:This SO post
Nothing Worked.
EDIT
I want to dynamically add tabs. On the first tab the tab pageradapter is not being called.That is why the first tab is showing empty, rest all works correctly.
use FragmentPagerAdapter
.
For adding Fragment
to FragmentPagerAdapter
, Follow it -
public class WeatherFragment extends Fragment {
private Toolbar toolbar;
private ViewPager viewPager;
private ViewPagerAdapter viewPagerAdapter;
private TabLayout tabLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View weatherFragment = inflater.inflate(R.layout.layout_weather_fragment, container, false);
//setup viewpager
viewPager = (ViewPager) weatherFragment.findViewById(R.id.viewpager);
tabLayout = (TabLayout) weatherFragment.findViewById(R.id.tabs);
viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
return weatherFragment;
}
And
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
if(position == 0) return new TodayTabFragment();
if(position == 1) return new TomorrowTabFragment();
if(position == 2) return new TenDaysTabFragment();
throw new IllegalStateException("Unexpected position " + position);
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
if(position == 0) return "Tab 1";
if(position == 1) return "Tab 2";
if(position == 2) return "Tab 3";
throw new IllegalStateException("Unexpected position " + position);
}
}
To get a reference to a Fragment created by a ViewPager, use the following findFragmentByTag
scheme:
Fragment fragment = supportFragmentManager.findFragmentByTag("android:switcher:" + viewPager.getId() + ":" + fragmentPosition)
Layout file for Fragment
.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
android:background="@color/colorlightBlue"
app:tabGravity="fill"
app:tabTextColor="@color/colorWhite"
app:tabSelectedTextColor="@color/colorWhite"
app:tabIndicatorColor="@color/colorWhite"
app:tabIndicatorHeight="2dp"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarAlwaysDrawHorizontalTrack="true"/>
</LinearLayout>