you suppose i have two fragment on viewpager which i can switch between them by swipe, for example Fragment1 and Fragment2,
what i want to that when i swipe? i want to show toast between switch on them, for example when i swipe to switch on Fragment1, application show toast, or when i swipe to switch on Fragment2 application show toast, and again, show toast each showing and switching between fragments,
how can i do that? i can't find any documentation about this solution
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
public void onPageSelected(int position) {
// Show your Toast Here like
Toast.makeText(app.getBaseContext(),"Some string",
Toast.LENGTH_SHORT).show();
}
});
and if you want some method's inside your Fragments use this in every fragment whereever you want to show toast
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
}
try this
viewpagerEvent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//toast message here
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
set OnPageChangeListener on ViewPager to get callback when page changed/scrolled or selected.
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
// Show fragment change/switch toast
Toast.makeText(this,"Fragment1",
Toast.LENGTH_LONG).show();
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
You have to make condition for Both Fragment while on page changed. try below code on your Activity Class.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(position == 0)
// Toast For Fragment 1
if(position == 1)
// Toast For Fragment 2
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Related
I am working on an app and when I slide to the next tab I would like to change the title of the screen to match the Tab!
You can use registerOnPageChangeCallback in ViewPager2 to detect page swipes. In ViewPager2s host(for example host Activity) implement this event and when onPageSelected is called change the title of host Activity. In onPageSelected method you can get current position of ViewPager2 and based on position set your title.
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
// Set title of Activity based on the position of Fragment
}
#Override
public void onPageScrollStateChanged(int state) {
super.onPageScrollStateChanged(state);
}
});
I am using viewPager for my custom Tablayout. I have two fragments, when viewPager activity was created , it should get the data from one of the fragment and have to display it on the tabs as unread count.
but I am getting data at scrolling. how can I over come this issue ?. please help me.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
PendingPlusPendingForUploadFiles frage = new PendingPlusPendingForUploadFiles();
System.out.println("====c value===" + frage.count);
}
#Override
public void onPageSelected(int position) {
// previousTabPosition = position;
viewPager.setCurrentItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
you can onResume(); method in the Fragment get it!
I want to interpolate between two colors while scrolling or "swiping" to the next tab in a TabLayout. To do this, I need some way to know when the user is swiping between tabs. I have tried using TabLayout.OnScrollChangeListener but the method is never called. Is it possible to know when the user is swiping to a new tab and, if so, how?
Edit:
libraryTabs.setOnScrollChangeListener(new TabLayout.OnScrollChangeListener() {
#Override
public void onScrollChange(View view, int i, int i1, int i2, int i3) {
}
});
is never called.
i think to achieve what you want then you will need a ViewPage
something like this
ViewPager mViewPager;
TabLayout mSlidingTabs;
mSlidingTabs = (TabLayout) findViewById(R.id.tabs);
mSlidingTabs.setupWithViewPager(mViewPager);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Log.w(TAG, "onPageScrolled");
// do what you want
}
#Override
public void onPageSelected(int position) {
Log.w(TAG, "onPageSelected");
// do what you want
}
#Override
public void onPageScrollStateChanged(int state) {
Log.w(TAG, "onPageScrollStateChanged");
// do what you want
}
});
Try this.
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());
//Do whatever you want here.
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
I have a SlidingTabLayout with this pageListener, when I scroll to another tab, the FAB hides and then shows again, this is correct, the problem is that it doesn't work when I click tab, it just hides and doesn't reappear until I scrollPage again. Any ideas? (I'm using support design library of course)
private ViewPager.OnPageChangeListener pageListener = new
ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
Log.i("SCROLL", "onPageSelected");
fab.show()
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
Log.i("SCROLL", "onPageScrolled");
if(fab.isShown()) {
fab.hide();
}
}
#Override
public void onPageScrollStateChanged(int arg0) {
Log.i("SCROLL", "onStateChanged: "+arg0);
fab.show();
}
};
Currently i am trying to deveolop an app which implements swiping between different views.
The swiping works fine, but i want to set the title of the view to the actionbar, that the users knows where he is.
So i hope you can give me some ideas where i had to put the code for the title change.
Thanks
You'll need to set up an OnPageChangeListener for your ViewPager. When you swipe, the OnPageChangeListener will be notified, and you can change the ActionBar title from the callback.
Here's some pseudo code for you:
ViewPager vp;
vp.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
getActionBar().setTitle(titles[position]); // Set the correct title based on the position
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {} // not used
#Override
public void onPageScrollStateChanged(int state) {} // not used
});
public void setTitle (CharSequence title)
Change the title associated with this activity. If this is a top-level activity, the title for its window will change. If it is an embedded activity, the parent can do whatever it wants with it.
http://developer.android.com/reference/android/app/Activity.html#setTitle(int)
mPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int pos) {
// Chage the title
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});