use of android.support.v4.view.ViewPager.OnPageChangeListener in project Android-Developers-Samples by johnjohndoe.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set content view (which contains a CheeseListFragment)
setContentView(R.layout.sample_main);
// Retrieve the ViewPager from the content view
ViewPager vp = (ViewPager) findViewById(R.id.viewpager);
// Set an OnPageChangeListener so we are notified when a new item is selected
vp.setOnPageChangeListener(mOnPageChangeListener);
// Finally set the adapter so the ViewPager can display items
vp.setAdapter(mPagerAdapter);
}
use of android.support.v4.view.ViewPager.OnPageChangeListener in project material-components-android by material-components.
the class TabLayout method setupWithViewPager.
private void setupWithViewPager(@Nullable final ViewPager viewPager, boolean autoRefresh, boolean implicitSetup) {
if (mViewPager != null) {
// If we've already been setup with a ViewPager, remove us from it
if (mPageChangeListener != null) {
mViewPager.removeOnPageChangeListener(mPageChangeListener);
}
if (mAdapterChangeListener != null) {
mViewPager.removeOnAdapterChangeListener(mAdapterChangeListener);
}
}
if (mCurrentVpSelectedListener != null) {
// If we already have a tab selected listener for the ViewPager, remove it
removeOnTabSelectedListener(mCurrentVpSelectedListener);
mCurrentVpSelectedListener = null;
}
if (viewPager != null) {
mViewPager = viewPager;
// Add our custom OnPageChangeListener to the ViewPager
if (mPageChangeListener == null) {
mPageChangeListener = new TabLayoutOnPageChangeListener(this);
}
mPageChangeListener.reset();
viewPager.addOnPageChangeListener(mPageChangeListener);
// Now we'll add a tab selected listener to set ViewPager's current item
mCurrentVpSelectedListener = new ViewPagerOnTabSelectedListener(viewPager);
addOnTabSelectedListener(mCurrentVpSelectedListener);
final PagerAdapter adapter = viewPager.getAdapter();
if (adapter != null) {
// Now we'll populate ourselves from the pager adapter, adding an observer if
// autoRefresh is enabled
setPagerAdapter(adapter, autoRefresh);
}
// Add a listener so that we're notified of any adapter changes
if (mAdapterChangeListener == null) {
mAdapterChangeListener = new AdapterChangeListener();
}
mAdapterChangeListener.setAutoRefresh(autoRefresh);
viewPager.addOnAdapterChangeListener(mAdapterChangeListener);
// Now update the scroll position to match the ViewPager's current item
setScrollPosition(viewPager.getCurrentItem(), 0f, true);
} else {
// We've been given a null ViewPager so we need to clear out the internal state,
// listeners and observers
mViewPager = null;
setPagerAdapter(null, false);
}
mSetupViewPagerImplicitly = implicitSetup;
}
use of android.support.v4.view.ViewPager.OnPageChangeListener in project frostwire by frostwire.
the class TransferDetailActivity method initComponents.
@Override
protected void initComponents(Bundle savedInstanceState) {
super.initComponents(savedInstanceState);
if (!initUIBittorrentDownload()) {
UIUtils.showShortMessage(this, R.string.could_not_open_transfer_detail_invalid_infohash);
finish();
return;
}
transferDetailFragment = findFragment(R.id.fragment_transfer_detail);
if (transferDetailFragment != null) {
transferDetailFragment.setUiBittorrentDownload(uiBittorrentDownload);
}
initTabTitles();
initFragments();
if (detailFragments == null || detailFragments.length <= 0) {
throw new RuntimeException("check your logic: can't initialize components without initialized fragments");
}
if (savedInstanceState != null) {
lastSelectedTabIndex = savedInstanceState.getInt("lastSelectedTabIndex", -1);
} else {
lastSelectedTabIndex = ConfigurationManager.instance().getInt(Constants.PREF_KEY_TORRENT_TRANSFER_DETAIL_LAST_SELECTED_TAB_INDEX);
}
OnPageChangeListener onPageChangeListener = new OnPageChangeListener(this);
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager(), detailFragments);
ViewPager viewPager = findViewById(R.id.transfer_detail_viewpager);
if (viewPager != null) {
viewPager.clearOnPageChangeListeners();
viewPager.setAdapter(mSectionsPagerAdapter);
viewPager.setCurrentItem(lastSelectedTabIndex == -1 ? 0 : lastSelectedTabIndex);
viewPager.addOnPageChangeListener(onPageChangeListener);
TabLayout tabLayout = findViewById(R.id.transfer_detail_tab_layout);
tabLayout.setupWithViewPager(viewPager);
} else {
throw new RuntimeException("initComponents() Could not get viewPager");
}
}
use of android.support.v4.view.ViewPager.OnPageChangeListener in project Osmand by osmandapp.
the class SearchActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
((OsmandApplication) getApplication()).applyTheme(this);
super.onCreate(savedInstanceState);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
long t = System.currentTimeMillis();
setContentView(R.layout.tab_content);
settings = ((OsmandApplication) getApplication()).getSettings();
accessibilityAssistant = new AccessibilityAssistant(this);
navigationInfo = new NavigationInfo((OsmandApplication) getApplication());
showOnlyOneTab = getIntent() != null && getIntent().getBooleanExtra(SHOW_ONLY_ONE_TAB, false);
getSupportActionBar().setTitle("");
getSupportActionBar().setElevation(0);
Integer tab = settings.SEARCH_TAB.get();
if (!showOnlyOneTab) {
ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
PagerSlidingTabStrip mSlidingTabLayout = (PagerSlidingTabStrip) findViewById(R.id.sliding_tabs);
List<TabItem> mTabs = new ArrayList<TabItem>();
mTabs.add(getTabIndicator(R.string.poi, getFragment(POI_TAB_INDEX)));
mTabs.add(getTabIndicator(R.string.address, getFragment(ADDRESS_TAB_INDEX)));
mTabs.add(getTabIndicator(R.string.shared_string_location, getFragment(LOCATION_TAB_INDEX)));
mTabs.add(getTabIndicator(R.string.favorite, getFragment(FAVORITES_TAB_INDEX)));
mTabs.add(getTabIndicator(R.string.shared_string_history, getFragment(HISTORY_TAB_INDEX)));
setViewPagerAdapter(mViewPager, mTabs);
mSlidingTabLayout.setViewPager(mViewPager);
mViewPager.setCurrentItem(Math.min(tab, HISTORY_TAB_INDEX));
mSlidingTabLayout.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
settings.SEARCH_TAB.set(arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
} else {
setContentView(R.layout.search_activity_single);
Class<?> cl = getFragment(tab);
try {
getSupportFragmentManager().beginTransaction().replace(R.id.layout, (Fragment) cl.newInstance()).commit();
} catch (InstantiationException e) {
throw new IllegalStateException(e);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
setTopSpinner();
Log.i("net.osmand", "Start on create " + (System.currentTimeMillis() - t));
Intent intent = getIntent();
OsmandSettings settings = ((OsmandApplication) getApplication()).getSettings();
LatLon last = settings.getLastKnownMapLocation();
if (intent != null) {
double lat = intent.getDoubleExtra(SEARCH_LAT, 0);
double lon = intent.getDoubleExtra(SEARCH_LON, 0);
if (lat != 0 || lon != 0) {
LatLon l = new LatLon(lat, lon);
if (!Algorithms.objectEquals(reqSearchPoint, l)) {
reqSearchPoint = l;
if ((Math.abs(lat - last.getLatitude()) < 0.00001) && (Math.abs(lon - last.getLongitude()) < 0.00001)) {
updateSearchPoint(reqSearchPoint, getString(R.string.select_search_position) + " " + getString(R.string.search_position_map_view), false);
} else {
updateSearchPoint(reqSearchPoint, getString(R.string.select_search_position) + " ", true);
}
}
}
}
if (searchPoint == null) {
if (!Algorithms.objectEquals(reqSearchPoint, last)) {
reqSearchPoint = last;
updateSearchPoint(last, getString(R.string.select_search_position) + " " + getString(R.string.search_position_map_view), false);
}
}
}
use of android.support.v4.view.ViewPager.OnPageChangeListener in project quran_android by quran.
the class PagerActivity method getViewPagerObservable.
public Observable<Integer> getViewPagerObservable() {
return Observable.create(e -> {
final OnPageChangeListener pageChangedListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
e.onNext(quranInfo.getPageFromPos(position, isDualPages));
}
};
viewPager.addOnPageChangeListener(pageChangedListener);
e.onNext(getCurrentPage());
e.setCancellable(() -> viewPager.removeOnPageChangeListener(pageChangedListener));
});
}
Aggregations