Skip to content

Instantly share code, notes, and snippets.

@vishyrich
Last active August 22, 2016 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vishyrich/014cad13b37cb7896d805d0e1ed9e6b8 to your computer and use it in GitHub Desktop.
Save vishyrich/014cad13b37cb7896d805d0e1ed9e6b8 to your computer and use it in GitHub Desktop.
NPE in FrameLayout onMeasure()
public enum HomeViewPagerIdentifier {
RECRUITMENT_LIST(RecruitmentListHomeFragment.class, R.string.recruitment_list, R.drawable.tab_icon_recruitment_active, R.drawable.tab_icon_recruitment, true, 0, 0),
FAVORITES_LIST(FavoriteListFragment.class, R.string.favorite_list, R.drawable.tab_icon_favorite_active, R.drawable.tab_icon_favorite, false, 1, -1),
QUICK_RESERVATION(QuickReservationSettingsFragment.class, R.string.quick_reservation, R.drawable.tab_icon_quick_reservation_active, R.drawable.tab_icon_quick_reservation, false, 1, -1),
RESERVATION_LIST(MessageBoxHomeFragment.class, R.string.reservation_list, R.drawable.tab_icon_reservation_active, R.drawable.tab_icon_reservation, false, 2, -1),
PHOTOS(PublicPhotosFragment.class, R.string.photos, R.drawable.tab_icon_snapphoto_active, R.drawable.tab_icon_snapphoto, false, 3, 1),
MY_PAGE(MyPageFragment.class, R.string.my_page, R.drawable.tab_icon_mypage_active, R.drawable.tab_icon_mypage, false, 4, -1);
private final Class<? extends Fragment> mPageFragment;
private final int mTitleResourceId;
private final int mSelectedTabIconResourceId;
private final int mUnSelectedTabIconResourceId;
private final boolean mIsSearchVisible;
private int mLoggedInTabPosition;
private int mLoggedOutTabPosition;
HomeViewPagerIdentifier(Class<? extends Fragment> pageFragment,
int titleResourceId, int selectedTabIconResourceId, int unSelectedTabIconResourceId,
boolean isSearchVisible, int loggedInTabPosition, int loggedOutTabPosition) {
mPageFragment = pageFragment;
mTitleResourceId = titleResourceId;
mSelectedTabIconResourceId = selectedTabIconResourceId;
mUnSelectedTabIconResourceId = unSelectedTabIconResourceId;
mIsSearchVisible = isSearchVisible;
mLoggedInTabPosition = loggedInTabPosition;
mLoggedOutTabPosition = loggedOutTabPosition;
}
public int getLoggedInTabPosition() {
return mLoggedInTabPosition;
}
public int getLoggedOutTabPosition() {
return mLoggedOutTabPosition;
}
public Class<? extends Fragment> getFragmentClass() {
return mPageFragment;
}
public int getTitleResourceId() {
return mTitleResourceId;
}
public int getSelectedTabIconResourceId() {
return mSelectedTabIconResourceId;
}
public int getUnSelectedTabIconResourceId() {
return mUnSelectedTabIconResourceId;
}
public boolean getIsSearchVisible() {
return mIsSearchVisible;
}
}
private void initHomeViewPager() {
mTabPagerAdapter = new HomeTabPagerAdapter(getSupportFragmentManager(), mHomeTabViewManager.loadViewPagerList());
mHomeViewPager = (SwipeLockedViewPager) findViewById(R.id.view_pager);
if (mHomeViewPager != null) {
mHomeViewPager.setAdapter(mTabPagerAdapter);
mHomeViewPager.setOffscreenPageLimit(HomeTabViewManager.OFF_SCREEN_PAGE_LIMIT);
mHomeViewPager.setEnableSwipe(false);
mHomeViewPager.setVisibility(View.VISIBLE);
mTabPagerAdapter.setCurrentItem(mHomeViewPager.getCurrentItem());
}
setupTabLayout();
}
private void setupTabLayout() {
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
if (tabLayout != null) {
tabLayout.removeAllTabs();
final HomeViewPagerIdentifier currentIdentifier = mTabPagerAdapter.getType(mHomeViewPager.getCurrentItem());
for (HomeViewPagerIdentifier identifier : mHomeTabViewManager.loadViewPagerList()) {
TabLayout.Tab tab = tabLayout.newTab();
TabConfigurationContainer container = TabConfigurationContainer.fromIdentifier(identifier, this);
tab.setTag(container);
boolean isSelected = identifier.equals(currentIdentifier);
tab.setCustomView(mHomeTabViewManager.getTabItemView(this, container, isSelected));
tabLayout.addTab(tab);
}
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(final TabLayout.Tab tab) {
// click listener for quick reservation edit discard dialog
QuickReservationSettingsFragment.OnDiscardDialogSelectedListener onDialogSelectListener =
new QuickReservationSettingsFragment.OnDiscardDialogSelectedListener() {
@Override
public void onPositiveSelect() {
// clear the reservation slots modified by user
Object previousFragment = mHomeViewPager.getAdapter().instantiateItem(mHomeViewPager, mHomeViewPager.getCurrentItem());
if (previousFragment instanceof QuickReservationSettingsFragment) {
((QuickReservationSettingsFragment) previousFragment).discardScheduleChanges();
}
// move to the tab selected by user
handleTabSelected(tab);
}
@Override
public void onNegativeSelect() {
int tabPosition = HomeViewPagerIdentifier.QUICK_RESERVATION.getLoggedInTabPosition();
TabLayout.Tab quickReservationTab = tabLayout.getTabAt(tabPosition);
if (quickReservationTab != null) {
// reselect quick reservation tab
quickReservationTab.select();
// un-select tab pressed by user
if (tab.getCustomView() != null) {
tab.getCustomView().setSelected(false);
}
}
}
};
Object previousFragment = mHomeViewPager.getAdapter().instantiateItem(mHomeViewPager, mHomeViewPager.getCurrentItem());
if (previousFragment instanceof QuickReservationSettingsFragment
&& tab.getPosition() != HomeViewPagerIdentifier.QUICK_RESERVATION.getLoggedInTabPosition()) {
if (showDiscardDialogIfEditing(previousFragment, onDialogSelectListener))
return;
}
handleTabSelected(tab);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
TabConfigurationContainer container = (TabConfigurationContainer) tab.getTag();
View customView = tab.getCustomView();
if (container == null || container.getIdentifier() == null || customView == null) {
return;
}
updateTabViews(customView, container, false);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
// do nothing
}
});
// select tab for deep link
selectDeepLinkTab(tabLayout);
if (mShouldShowLoginPrompt && !MDoor.isLoggedIn()) {
new Handler().post(new Runnable() {
@Override
public void run() {
AccountMenuDialogFragment.newInstance().show(getSupportFragmentManager());
}
});
mShouldShowLoginPrompt = false;
}
}
}
public class SwipeLockedViewPager extends ViewPager {
private boolean mEnableSwipe = true;
public SwipeLockedViewPager(Context context) {
super(context);
}
public SwipeLockedViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (mEnableSwipe) {
return super.onInterceptTouchEvent(ev);
} else {
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (mEnableSwipe) {
return super.onTouchEvent(ev);
} else {
return false;
}
}
public void setEnableSwipe(boolean enableSwipe) {
mEnableSwipe = enableSwipe;
}
}
public class TabConfigurationContainer {
public HomeViewPagerIdentifier mIdentifier;
public int mSelectedTextColor;
public String mTabText;
public static TabConfigurationContainer fromIdentifier(HomeViewPagerIdentifier identifier, Context context) {
TabConfigurationContainer container = new TabConfigurationContainer();
container.setIdentifier(identifier);
container.setTabText(context.getString(identifier.getTitleResourceId()));
container.setSelectedTextColor(context.getResources().getColor(R.color.header_bg));
return container;
}
public HomeViewPagerIdentifier getIdentifier() {
return mIdentifier;
}
public int getSelectedTextColor() {
return mSelectedTextColor;
}
public String getTabText() {
return mTabText;
}
public void setIdentifier(HomeViewPagerIdentifier identifier) {
mIdentifier = identifier;
}
public void setSelectedTextColor(int selectedTextColor) {
mSelectedTextColor = selectedTextColor;
}
public void setTabText(String tabText) {
mTabText = tabText;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment