I want to show page progress in top of a viewpager in android. The progress bar will become blue while you scrolling to next screen.
Any help??
You can use the OnPageChangeListener() method to set a ViewPager.OnPageChangeListener which can listen for when you scroll the viewpager to get to another page or when you simply scroll the viewpager.
Example:
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//Write code to change progressbar when page is scrolled here.
}
#Override
public void onPageSelected(int position) {
//Write code to change progressbar when page is changed here
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
See these references for more information.
Reference: http://developer.android.com/reference/android/support/v4/view/ViewPager.html
http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html
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 have 3 fragments in my viewpager and I want to show a spinner item on my toolbar's menu on the last page. So if I am going from 2 -> 3 the spinner should become visible but if I go from 3 -> 2, it should become invisible. I know I have to add an onPageChangeListener to the viewpager but I am not sure how to determine which was the last page. How can I do it?
In MainActivity where you have used ViewPager use below method
//visible spinner when 3rd fragment is visible
viewPagerFloor.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(position==2){
spinner.setVisibility(View.VISIBLE);
}else{
spinner.setVisibility(View.GONE);
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
I've set up a pager with 3 fragments, I want to execute an specific block of code when fragment #2 is shown but problem is that onCreate/onResume methods from this fragment execute when fragment #1 is shown. I understand that the logic behind this is doing work ahead for when fragment#2 is actually needed, but how can I do if I need something to be done specifically when fragment#2 is shown?
You can implement an OnPageChangeListener, override OnPageSelected and set it on the ViewPager this way:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (position == 1) { // Meaning the position of the 2nd Fragment
doSomeCode();
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
I have a problem. I have an Activity with ViewPager which have some Fragments.
In my first Fragment I have some elements. Lets say an ImageView, TextView. How to make them visible by timer when you completely open the page of ViewPager?!
I used this code below in my MainActivity.java document to make that ImageView visible by timer but it works not as I expected. When i want to go to next page by scrolling and touch the screen ImageView dissapear. Why its happened?!
final ImageView mImageView = (ImageView) findViewById(R.id.earth);
mImageView.setVisibility(View.INVISIBLE);
mImageView.postDelayed(new Runnable() {
public void run() {
mImageView.setVisibility(View.VISIBLE);
}}, 5000);
Any help is appreciated. Thanks!
My new edits:
// Initialize the ViewPager and set an adapter
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
// Bind the tabs to the ViewPager
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(pager);
//open concrete page of ViewPager. setCurrentItem(index of page)
pager.setCurrentItem(1);
tabs.setOnPageChangeListener(new OnPageChangeListener() {
//This method will be invoked when the current page is scrolled, either as part of a program initiated smooth scroll or a user initiated touch scroll.
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
//This method will be invoked when a new page becomes selected.
#Override
public void onPageSelected(int position) {
final ImageView mImageView = (ImageView) findViewById(R.id.earth);
mImageView.setVisibility(View.INVISIBLE);
if (position == 0) {
mImageView.postDelayed(new Runnable() {
public void run() {
mImageView.setVisibility(View.VISIBLE);
}
}, 5000);
}
}
//Called when the scroll state changes.
#Override
public void onPageScrollStateChanged(int state) {
}
});
My Problem is conflict with TABS and ViewPager. When I use tabs.setOnPageChangeListener(new OnPageChangeListener() i have conflict with pager.setCurrentItem(1); which shows concrete page when Activity first opened. In my case its shows 2-d page when Activity first opened but Tab shows that opened 1-st page. WHY?! How to solve this problem?!
I would not use a timer. It would either fire too early, or too late during which your UI looks bad. Use the pager's events to know when to update your UI. For example, you can probably use OnPageChangedListener:
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
// Now displaying page at position
}
#Override
public void onPageScrollStateChanged(int state) {
}
};
I have a viewpager that dislapys text views. I have header layout above it. What i want is to hide or display this header when the user click on the screen. but not when the scrolling is occur. I tried the ViewPager.onPageChangeListener. But with no success. What i need is how to know when the swapping between pages finishes. The onPageScrollStateChange didnt help i create an instance variable to hold the state and keep updating it when this method call so i can use it in my onclick listener to now if the page is scrolling or not but with no success
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
if(ViewPager.SCROLL_STATE_IDLE == state){
//Scrolling finished. Do something.
}
}
});