Skip to content

Instantly share code, notes, and snippets.

@enginebai
Last active December 9, 2015 22:44
Show Gist options
  • Save enginebai/605b0e4f8274d089b4ab to your computer and use it in GitHub Desktop.
Save enginebai/605b0e4f8274d089b4ab to your computer and use it in GitHub Desktop.
public class MoviePageFragment extends Fragment implements MaterialTabListener, View.OnClickListener {
public static final int[] TABS = {R.string.tab_movie_thisweek,
R.string.tab_movie_intheater, R.string.tab_movie_comingsoon};
public enum SortBy {
LASTEST,
OLDEST,
BOMBER
}
@InjectView(R.id.tab_movie_list)
MaterialTabHost mTabMoviewList;
@InjectView(R.id.pager_movie)
ViewPager mPagerMovie;
@InjectView(R.id.fab_sort_lastest)
FloatingActionButton mFabLastest;
@InjectView(R.id.fab_sort_oldest)
FloatingActionButton mFabOldest;
@InjectView(R.id.fab_sort_bomber)
FloatingActionButton mFabBomber;
@InjectView(R.id.fab_menu_sort)
FloatingActionsMenu mFabMenuSort;
@InjectView(R.id.fab_overlay)
View mFabOverlay;
private MoviePagerAdapter mAdapter;
private int mCurrentTab = 0;
public static MoviePageFragment newInstance() {
MoviePageFragment fragment = new MoviePageFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movie_page, container, false);
this.initView(rootView);
return rootView;
}
private void initView(View rootView) {
ButterKnife.inject(this, rootView);
mAdapter = new MoviePagerAdapter(getFragmentManager(), getActivity());
this.mPagerMovie.setAdapter(mAdapter);
this.mPagerMovie.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mTabMoviewList.setSelectedNavigationItem(position);
// MovieListFragment fragment = (MovieListFragment) mAdapter.instantiateItem(mPagerMovie, position);
// fragment.loadMovieList(true);
mCurrentTab = position;
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
for (int i = 0; i < mAdapter.getCount(); i++) {
this.mTabMoviewList.addTab(this.mTabMoviewList.newTab()
.setTabListener(this)
.setText(getActivity().getResources().getString(TABS[i])));
}
this.mFabMenuSort.setOnFloatingActionsMenuUpdateListener(new FloatingActionsMenu.OnFloatingActionsMenuUpdateListener() {
@Override
public void onMenuExpanded() {
mFabOverlay.setVisibility(View.VISIBLE);
}
@Override
public void onMenuCollapsed() {
mFabOverlay.setVisibility(View.GONE);
}
});
this.mFabLastest.setOnClickListener(this);
this.mFabOldest.setOnClickListener(this);
this.mFabBomber.setOnClickListener(this);
this.mFabOverlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mFabMenuSort.collapse();
mFabOverlay.setVisibility(View.GONE);
}
});
}
@Override
public void onClick(View v) {
MovieListFragment fragment = (MovieListFragment)mAdapter.instantiateItem(mPagerMovie,
this.mCurrentTab);
switch (v.getId()) {
case R.id.fab_sort_lastest:
fragment.setSortBy(SortBy.LASTEST);
break;
case R.id.fab_sort_oldest:
fragment.setSortBy(SortBy.OLDEST);
break;
case R.id.fab_sort_bomber:
fragment.setSortBy(SortBy.BOMBER);
break;
}
mFabMenuSort.collapse();
fragment.loadMovieList(true);
}
@Override
public void onTabSelected(MaterialTab materialTab) {
int position = materialTab.getPosition();
this.mPagerMovie.setCurrentItem(position);
this.mCurrentTab = position;
}
@Override
public void onTabReselected(MaterialTab materialTab) {
}
@Override
public void onTabUnselected(MaterialTab materialTab) {
}
}
public class MoviePagerAdapter extends FragmentStatePagerAdapter {
private Context mContext;
public MoviePagerAdapter(FragmentManager fm, Context c) {
super(fm);
this.mContext = c;
}
@Override
public Fragment getItem(int position) {
return MovieListFragment.newInstance(position);
}
@Override
public int getCount() {
return MoviePageFragment.TABS.length;
}
@Override
public CharSequence getPageTitle(int position) {
return this.mContext.getResources().getString(MoviePageFragment.TABS[position]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment