use of android.support.design.widget.TabLayout in project philm by chrisbanes.
the class BasePhilmTabFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_viewpager, container, false);
mAdapter = new TabPagerAdapter(getChildFragmentManager());
mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
mViewPager.setAdapter(mAdapter);
mViewPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.spacing_minor));
mTabLayout = (TabLayout) view.findViewById(R.id.viewpager_tabs);
mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
final Fragment fragment = mAdapter.getItem(tab.getPosition());
if (fragment instanceof ListFragment) {
((ListFragment) fragment).smoothScrollTo(0);
}
}
});
if (savedInstanceState != null) {
mCurrentItem = savedInstanceState.getInt(SAVE_SELECTED_TAB);
}
return view;
}
use of android.support.design.widget.TabLayout in project cheesesquare by chrisbanes.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
setupDrawerContent(navigationView);
}
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
if (viewPager != null) {
setupViewPager(viewPager);
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG).setAction("Action", null).show();
}
});
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
use of android.support.design.widget.TabLayout in project Reader by TheKeeperOfPie.
the class FragmentNewPost method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_new_post, container, false);
layoutCoordinator = (CoordinatorLayout) view.findViewById(R.id.layout_coordinator);
layoutAppBar = (AppBarLayout) view.findViewById(R.id.layout_app_bar);
scrollText = (NestedScrollView) view.findViewById(R.id.scroll_text);
textInfo = (TextView) view.findViewById(R.id.text_info);
textSubmit = (TextView) view.findViewById(R.id.text_submit);
editTextTitle = (EditText) view.findViewById(R.id.edit_title);
editTextBody = (EditText) view.findViewById(R.id.edit_body);
toolbar = (Toolbar) view.findViewById(R.id.toolbar);
toolbar.setTitle(getString(R.string.new_post));
toolbar.setTitleTextColor(themer.getColorFilterPrimary().getColor());
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
toolbar.setNavigationOnClickListener(v -> {
UtilsInput.hideKeyboard(editTextBody);
mListener.onNavigationBackClick();
});
toolbar.getNavigationIcon().mutate().setColorFilter(themer.getColorFilterPrimary());
setUpOptionsMenu();
textInfo.setText(getString(R.string.submitting_post, getArguments().getString(SUBREDDIT), getArguments().getString(USER)));
String submitTextHtml = getArguments().getString(SUBMIT_TEXT_HTML);
Log.d(TAG, "submitTextHtml: " + submitTextHtml);
if (TextUtils.isEmpty(submitTextHtml) || "null".equals(submitTextHtml)) {
textSubmit.setVisibility(View.GONE);
} else {
textSubmit.setText(UtilsReddit.getFormattedHtml(submitTextHtml));
}
textSubmit.setMovementMethod(LinkMovementMethod.getInstance());
if (Reddit.PostType.LINK == postType) {
editTextBody.setHint("URL");
} else {
editTextBody.setHint("Text");
}
View.OnFocusChangeListener onFocusChangeListener = (v, hasFocus) -> {
if (hasFocus) {
AppBarLayout.Behavior behaviorAppBar = (AppBarLayout.Behavior) ((CoordinatorLayout.LayoutParams) layoutAppBar.getLayoutParams()).getBehavior();
behaviorAppBar.onNestedFling(layoutCoordinator, layoutAppBar, null, 0, 1000, true);
}
};
editTextTitle.setOnFocusChangeListener(onFocusChangeListener);
editTextBody.setOnFocusChangeListener(onFocusChangeListener);
editMarginDefault = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
editMarginWithActions = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 56, getResources().getDisplayMetrics());
textPreview = (TextView) view.findViewById(R.id.text_preview);
viewDivider = view.findViewById(R.id.view_divider);
toolbarActions = (Toolbar) view.findViewById(R.id.toolbar_actions);
toolbarActions.inflateMenu(R.menu.menu_editor_actions);
toolbarActions.setOnMenuItemClickListener(this);
tabLayout = (TabLayout) view.findViewById(R.id.layout_tab);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabTextColors(themer.getColorFilterTextMuted().getColor(), themer.getColorFilterPrimary().getColor());
viewPager = (ViewPager) view.findViewById(R.id.view_pager);
viewPager.setAdapter(new PagerAdapter() {
@Override
public CharSequence getPageTitle(int position) {
switch(position) {
case PAGE_POST:
return getString(R.string.page_post);
case PAGE_PREVIEW:
return getString(R.string.page_preview);
}
return super.getPageTitle(position);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
return viewPager.getChildAt(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
@Override
public int getCount() {
if (Reddit.PostType.LINK == postType) {
tabLayout.setVisibility(View.GONE);
toolbarActions.setVisibility(View.GONE);
viewDivider.setVisibility(View.GONE);
itemHideActions.setVisible(false);
return 1;
}
return viewPager.getChildCount();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (position == PAGE_POST && toolbarActions.getVisibility() == View.VISIBLE) {
float translationY = positionOffset * (toolbarActions.getHeight() + viewDivider.getHeight());
viewDivider.setTranslationY(translationY);
toolbarActions.setTranslationY(translationY);
}
}
@Override
public void onPageSelected(int position) {
if (position == PAGE_PREVIEW) {
if (editTextBody.length() == 0) {
textPreview.setText(R.string.empty_reply_preview);
} else {
textPreview.setText(Html.fromHtml(Processor.process(editTextBody.getText().toString())));
}
}
if (Reddit.PostType.SELF == postType) {
itemHideActions.setVisible(position == PAGE_POST);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
layoutCaptcha = (RelativeLayout) view.findViewById(R.id.layout_captcha);
imageCaptcha = (ImageView) view.findViewById(R.id.image_captcha);
editCaptcha = (EditText) view.findViewById(R.id.edit_captcha);
buttonCaptchaRefresh = (ImageButton) view.findViewById(R.id.button_captcha_refresh);
buttonCaptchaRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadCaptcha();
}
});
if (getArguments().getBoolean(IS_EDIT, false)) {
loadEditValues();
} else {
reddit.needsCaptcha().subscribe(new Observer<String>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(String response) {
if ("true".equalsIgnoreCase(response)) {
layoutCaptcha.setVisibility(View.VISIBLE);
loadCaptcha();
}
}
});
}
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Menu menu = toolbarActions.getMenu();
int maxNum = (int) (view.getWidth() / TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics()));
int numShown = 0;
for (int index = 0; index < menu.size(); index++) {
MenuItem menuItem = menu.getItem(index);
menuItem.getIcon().setColorFilter(themer.getColorFilterIcon());
if (numShown++ < maxNum - 1) {
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
} else {
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
}
// Toggle visibility to fix weird bug causing tabs to not be added
tabLayout.setVisibility(View.GONE);
tabLayout.setVisibility(View.VISIBLE);
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
return view;
}
use of android.support.design.widget.TabLayout in project Reader by TheKeeperOfPie.
the class FragmentHistory method showDateRangeDialog.
private void showDateRangeDialog() {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_date_range, null, false);
final DatePicker datePickerStart = (DatePicker) view.findViewById(R.id.date_picker_start);
final DatePicker datePickerEnd = (DatePicker) view.findViewById(R.id.date_picker_end);
final ViewPager viewPager = (ViewPager) view.findViewById(R.id.view_pager_time);
viewPager.setAdapter(new PagerAdapter() {
@Override
public Object instantiateItem(ViewGroup container, int position) {
switch(position) {
case 0:
return datePickerStart;
case 1:
return datePickerEnd;
}
return super.instantiateItem(container, position);
}
@Override
public CharSequence getPageTitle(int position) {
switch(position) {
case 0:
return viewPager.getContext().getResources().getString(R.string.start);
case 1:
return viewPager.getContext().getResources().getString(R.string.end);
}
return super.getPageTitle(position);
}
@Override
public int getCount() {
return 2;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
});
final TabLayout layoutTab = (TabLayout) view.findViewById(R.id.layout_tab);
layoutTab.setupWithViewPager(viewPager);
final Calendar calendarStart = Calendar.getInstance();
calendarStart.setTimeZone(TimeZone.getTimeZone("UTC"));
datePickerStart.init(calendarStart.get(Calendar.YEAR), calendarStart.get(Calendar.MONTH), calendarStart.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
calendarStart.set(year, monthOfYear, dayOfMonth);
layoutTab.getTabAt(0).setText(DateUtils.formatDateTime(getActivity(), calendarStart.getTimeInMillis(), FORMAT_DATE));
}
});
long timeStart = controllerHistory.getTimeStart();
if (timeStart > 0) {
calendarStart.setTimeInMillis(timeStart);
datePickerStart.updateDate(calendarStart.get(Calendar.YEAR), calendarStart.get(Calendar.MONTH), calendarStart.get(Calendar.DAY_OF_MONTH));
}
final Calendar calendarEnd = Calendar.getInstance();
calendarEnd.setTimeZone(TimeZone.getTimeZone("UTC"));
datePickerEnd.init(calendarEnd.get(Calendar.YEAR), calendarEnd.get(Calendar.MONTH), calendarEnd.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
calendarEnd.set(year, monthOfYear, dayOfMonth);
layoutTab.getTabAt(1).setText(DateUtils.formatDateTime(getActivity(), calendarEnd.getTimeInMillis(), FORMAT_DATE));
}
});
datePickerEnd.setMaxDate(System.currentTimeMillis() + AlarmManager.INTERVAL_DAY);
long timeEnd = controllerHistory.getTimeEnd();
if (timeEnd < Long.MAX_VALUE) {
calendarEnd.setTimeInMillis(timeEnd);
calendarEnd.add(Calendar.DAY_OF_MONTH, -1);
datePickerEnd.updateDate(calendarEnd.get(Calendar.YEAR), calendarEnd.get(Calendar.MONTH), calendarEnd.get(Calendar.DAY_OF_MONTH));
}
AlertDialog dialog = new AlertDialog.Builder(getActivity()).setView(view).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Calendar calendarStart = Calendar.getInstance();
calendarStart.set(datePickerStart.getYear(), datePickerStart.getMonth(), datePickerStart.getDayOfMonth());
calendarStart.setTimeZone(TimeZone.getTimeZone("UTC"));
Calendar calendarEnd = Calendar.getInstance();
calendarEnd.set(datePickerEnd.getYear(), datePickerEnd.getMonth(), datePickerEnd.getDayOfMonth());
calendarEnd.setTimeZone(TimeZone.getTimeZone("UTC"));
calendarEnd.add(Calendar.DAY_OF_MONTH, 1);
controllerHistory.setTimeStart(calendarStart.getTimeInMillis());
controllerHistory.setTimeEnd(calendarEnd.getTimeInMillis());
controllerHistory.reload();
}
}).setNeutralButton(R.string.reset, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
controllerHistory.setTimeStart(0);
controllerHistory.setTimeEnd(Long.MAX_VALUE);
controllerHistory.reload();
}
}).setNegativeButton(R.string.cancel, null).create();
dialog.show();
}
use of android.support.design.widget.TabLayout in project Reader by TheKeeperOfPie.
the class FragmentReply method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_reply, container, false);
layoutCoordinator = (CoordinatorLayout) view.findViewById(R.id.layout_coordinator);
layoutAppBar = (AppBarLayout) view.findViewById(R.id.layout_app_bar);
toolbar = (Toolbar) view.findViewById(R.id.toolbar);
toolbar.setTitleTextColor(themer.getColorFilterPrimary().getColor());
setUpToolbar();
textAuthor = (TextView) view.findViewById(R.id.text_author);
textAuthor.setText(getString(R.string.replying_from, controllerUser.getUser().getName()));
scrollText = (NestedScrollView) view.findViewById(R.id.scroll_text);
textParent = (TextView) view.findViewById(R.id.text_parent);
textParent.setText(getArguments().getCharSequence(ARG_TEXT_PARENT, ""));
editMarginDefault = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
editMarginWithActions = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 56, getResources().getDisplayMetrics());
editReply = (EditText) view.findViewById(R.id.edit_reply);
editReply.setText(getArguments().getString(ARG_TEXT));
editReply.setSelection(editReply.length());
View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
AppBarLayout.Behavior behaviorAppBar = (AppBarLayout.Behavior) ((CoordinatorLayout.LayoutParams) layoutAppBar.getLayoutParams()).getBehavior();
behaviorAppBar.onNestedFling(layoutCoordinator, layoutAppBar, null, 0, 1000, true);
}
}
};
editReply.setOnFocusChangeListener(onFocusChangeListener);
textPreview = (TextView) view.findViewById(R.id.text_preview);
viewDivider = view.findViewById(R.id.view_divider);
toolbarActions = (Toolbar) view.findViewById(R.id.toolbar_actions);
toolbarActions.inflateMenu(R.menu.menu_editor_actions);
toolbarActions.setOnMenuItemClickListener(this);
viewPager = (ViewPager) view.findViewById(R.id.view_pager);
viewPager.setAdapter(new PagerAdapter() {
@Override
public CharSequence getPageTitle(int position) {
switch(position) {
case PAGE_PARENT:
return getString(R.string.page_parent);
case PAGE_REPLY:
return getString(R.string.page_reply);
case PAGE_PREVIEW:
return getString(R.string.page_preview);
}
return super.getPageTitle(position);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
return viewPager.getChildAt(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
@Override
public int getCount() {
return viewPager.getChildCount();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (toolbarActions.getVisibility() == View.VISIBLE) {
float translationY = toolbarActions.getTranslationY();
if (position == PAGE_REPLY) {
translationY = positionOffset * (toolbarActions.getHeight() + viewDivider.getHeight());
} else if (position == PAGE_PARENT) {
translationY = (1f - positionOffset) * (toolbarActions.getHeight() + viewDivider.getHeight());
}
viewDivider.setTranslationY(translationY);
toolbarActions.setTranslationY(translationY);
}
}
@Override
public void onPageSelected(int position) {
if (position == PAGE_PREVIEW) {
if (editReply.length() == 0) {
textPreview.setText(R.string.empty_reply_preview);
} else {
textPreview.setText(Html.fromHtml(Processor.process(editReply.getText().toString())));
}
}
menu.findItem(R.id.item_hide_actions).setVisible(position == PAGE_REPLY);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
viewPager.setOffscreenPageLimit(viewPager.getChildCount() - 1);
viewPager.setCurrentItem(PAGE_REPLY);
tabLayout = (TabLayout) view.findViewById(R.id.layout_tab);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabTextColors(themer.getColorFilterTextMuted().getColor(), themer.getColorFilterPrimary().getColor());
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Menu menu = toolbarActions.getMenu();
int maxNum = (int) (view.getWidth() / TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics()));
int numShown = 0;
for (int index = 0; index < menu.size(); index++) {
MenuItem menuItem = menu.getItem(index);
menuItem.getIcon().setColorFilter(themer.getColorFilterIcon());
if (numShown++ < maxNum - 1) {
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
} else {
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
}
// Toggle visibility to fix weird bug causing tabs to not be added
tabLayout.setVisibility(View.GONE);
tabLayout.setVisibility(View.VISIBLE);
editReply.requestFocus();
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
view.post(new Runnable() {
@Override
public void run() {
view.setTranslationY(view.getHeight());
view.setVisibility(View.VISIBLE);
ViewCompat.animate(view).translationY(0).withEndAction(new Runnable() {
@Override
public void run() {
Fragment fragment = getFragmentManager().findFragmentByTag(fragmentParentTag);
if (fragment != null) {
getFragmentManager().beginTransaction().hide(fragment).commit();
}
}
}).start();
}
});
return view;
}
Aggregations