I have a single listview that is populated with different information depending on the selected tab. What I expect is that historical tab will getPastBookings(), scheduled tab will getFutureBookings() and favorites tab will get getFavoriteBookings().
Unfortunately, historical and scheduled tabs calls getFavoriteBookings OR getFutureBookings, while favorite tab calls nothing.
What could be the problem in the code?
THANK YOU IN ADVANCE. :)
TabsAdapter.java
public class TabsAdapter extends FragmentPagerAdapter implements
TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final TabHost mTabHost;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
static final class TabInfo {
private final String tag;
private final Class<?> clss;
private final Bundle args;
TabInfo(String _tag, Class<?> _class, Bundle _args) {
tag = _tag;
clss = _class;
args = _args;
}
}
static class DummyTabFactory implements TabHost.TabContentFactory {
private final Context mContext;
public DummyTabFactory(Context context) {
mContext = context;
}
@Override
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
public TabsAdapter(FragmentActivity activity, TabHost tabHost,
ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mTabHost = tabHost;
mViewPager = pager;
mTabHost.setOnTabChangedListener(this);
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
tabSpec.setContent(new DummyTabFactory(mContext));
String tag = tabSpec.getTag();
TabInfo info = new TabInfo(tag, clss, args);
mTabs.add(info);
mTabHost.addTab(tabSpec);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
@Override
public void onTabChanged(String tabId) {
int position = mTabHost.getCurrentTab();
mViewPager.setCurrentItem(position);
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
TabWidget widget = mTabHost.getTabWidget();
int oldFocusability = widget.getDescendantFocusability();
widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
mTabHost.setCurrentTab(position);
widget.setDescendantFocusability(oldFocusability);
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
TripsListTabsFragment.java
public class TripsListTabsFragment extends BaseSupportFragment {
private static final String LOG_TAG = "TripsListTabsFragment";
@InjectView(R.id.pager) ViewPager mViewPager;
@InjectView(android.R.id.tabhost) TabHost mFragTabHost;
TabsAdapter mTripsPagerAdapter;
public static Fragment newInstance() {
Bundle bundle = new Bundle();
TripsListTabsFragment fragment = new TripsListTabsFragment();
fragment.setArguments(bundle);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return createTabhost(inflater, container);
}
private View createTabhost(LayoutInflater inflater, ViewGroup container) {
View rootView = inflater.inflate(
R.layout.fragment_tab_contents, container,
false);
ButterKnife.inject(this, rootView);
mFragTabHost.setup();
mTripsPagerAdapter = new TabsAdapter(getActivity(), mFragTabHost, mViewPager);
TabSpec scheduledTab = mFragTabHost.newTabSpec("scheduled")
.setIndicator(getResources().getString(R.string.scheduled));
Bundle scheduledTabBundle = new Bundle();
scheduledTabBundle.putSerializable(TripsListFragment.EXTRA_LIST_TYPE, ListType.SCHEDULED);
mTripsPagerAdapter.addTab(scheduledTab, TripsListFragment.class, scheduledTabBundle);
TabSpec historicalTab = mFragTabHost.newTabSpec("historical")
.setIndicator(getResources().getString(R.string.history));
Bundle historicalTabBundle = new Bundle();
historicalTabBundle.putSerializable(TripsListFragment.EXTRA_LIST_TYPE, ListType.HISTORICAL);
mTripsPagerAdapter.addTab(historicalTab, TripsListFragment.class, historicalTabBundle);
TabSpec favoritesTab = mFragTabHost.newTabSpec("favorites")
.setIndicator(getResources().getString(R.string.button_favorites));
Bundle favoritesTabBundle = new Bundle();
favoritesTabBundle.putSerializable(TripsListFragment.EXTRA_LIST_TYPE, ListType.FAVORITES);
mTripsPagerAdapter.addTab(favoritesTab, TripsListFragment.class, favoritesTabBundle);
setTabColor(mFragTabHost);
return rootView;
}
private void setTabColor(TabHost tabhost) {
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) {
tabhost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.beck_tab_indicator_ora nge);
}
}
}
TripsListFragment.java
public class TripsListFragment extends BaseSupportListFragment {
public static final String EXTRA_LIST_TYPE = "com.becktaxi.customer.TRIP_LIST_TYPE";
// @formatter:off
@InjectView (android.R.id.list) ListView mListView;
@InjectView(android.R.id.empty) TextView mEmptyTextView;
// @formatter:on
List<Booking> mBookings;
BookingAdapter mArrayAdapter;
ListType mListType;
public static Fragment newInstance(ListType listType) {
Bundle bundle = new Bundle();
bundle.putSerializable(EXTRA_LIST_TYPE, listType);
TripsListFragment fragment = new TripsListFragment();
fragment.setArguments(bundle);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_trips_list_layout,
container, false);
ButterKnife.inject(this, rootView);
mBookings = new ArrayList<Booking>();
ListType lt = (ListType) getArguments().getSerializable(EXTRA_LIST_TYPE);
showBookingsInList();
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
showBookingsInList();
}
public void showBookingsInList() {
if (mArrayAdapter != null) {
mArrayAdapter = null;
}
mListType = (ListType) getArguments().getSerializable(EXTRA_LIST_TYPE);
String listType = mListType.name();
switch (mListType) {
case SCHEDULED: {
mBookings = getFutureBookings();
if(mBookings.size() < 1) {
mEmptyTextView.setText(R.string.empty_scheduled_bookings);
}
break;
}
case HISTORICAL: {
mBookings = getPastBookings();
if(mBookings.size() < 1) {
mEmptyTextView.setText(R.string.empty_past_bookings);
}
break;
}
case FAVORITES: {
mBookings = getFavoriteBookings();
if(mBookings.size() < 1) {
mEmptyTextView.setText(R.string.empty_favourite_bookings);
}
break;
}
default:
break;
}
mArrayAdapter = new BookingAdapter(getActivity(),
R.layout.view_booking_single_item_layout, R.id.cbv_booking,
mBookings, mCallbacks.getHelper().getBookingDao());
setListAdapter(mArrayAdapter);
}
private List<Booking> getPastBookings() {
try {
return DbService.getAllPastBookings(mCallbacks.getHelper()
.getBookingDao());
} catch (SQLException e) {
e.printStackTrace();
}
return new ArrayList<Booking>();
}
private List<Booking> getFutureBookings() {
try {
return DbService.getAllFutureBookings(mCallbacks.getHelper()
.getBookingDao());
} catch (SQLException e) {
e.printStackTrace();
}
return new ArrayList<Booking>();
}
private List<Booking> getFavoriteBookings() {
try {
return DbService.getAllFavoriteBookings(mCallbacks.getHelper()
.getBookingDao());
} catch (SQLException e) {
e.printStackTrace();
}
return new ArrayList<Booking>();
}
public enum ListType {
SCHEDULED, HISTORICAL, FAVORITES
}
}
I was able to fix it by:
FragmentPagerAdapter
to FragmentStatePagerAdapter
getItemPosition()
in the FragmentStatePagerAdapter
which will return Position.NONE
notifySetDataChanged()
in onTabChanged()