I am using ViewPager with 4 pages. I need a code to navigate to the first page on specific events. Any idea how? The PagerAdapter has the methods setPrimaryItem, but I am uncertain how to use it.
Another relevant question is how to trigger swiping the pager through code, I could use that to swipe all the way to the needed view, and I imagine it will also look better.
Just use mViewPager.setCurrentItem(1), this method will navigate to that page with the swipe animation.
Related
I am A beginner at android. I have created an app which loads news from JSON to recycler view. I want to open an activity when the user swipes from left to right. I tried different methods but it doesn't work for me. Please help.
You have to create an activity first and put a view pager in it and switch between fragments : ( its better to use fragments at this kind of situations, u can swipe between ur fragments by using android view pager )
Here is a great link that gonna help you :
https://medium.com/#kyroschow/how-to-use-viewpager-for-navigating-between-fragments-with-tablayout-a28b4cf92c42
Have a good day
The better option is to use one Activity & other Fragments. And then use Viewpager for swipe between fragments. Follow below link:
https://developer.android.com/training/animation/screen-slide
I have a viewpager, it has 5 fragments page. I can switch between each fragment by tapping a navigation button.
Now, what I'm trying to achieve is to refresh 1 of the page when entering it. (e.g. I'm in Page 1, when I tap button for Page 2, Page 2 will display and refresh.
However, I find out that the onResume method in the fragment is not called on this behavior. or any of the other lifecycle method.
I can't seem to find a way to do this. Is there any callbacks I can use for such scenario?
Since you don't need any kind of caching, it's probably a good idea for you to use viewPager.setOffscreenPageLimit(0) so pages won't be loaded in background, and onResume will be called only when page is actually displayed.
UPD: Thanks, Yarh, for pointing out that setOffscreenPageLimit(0) doesn't work. Sorry for that.
If this doesn't work for you, use viewPager.addOnPageChangeListener(listener) - onPageSelected(position) method should help.
use onTabSelected and onTabReSelected methods of navigation tabs to do your stuff
I use a View Pager in my app to get animations between fragments, but I've got a problem because I use a button to pass fragments, I have disabled swipe option, and when a fragment pass to other, it is really quickly and you can not see anything. I want to pass more slowly so, Can you help me please?
I use this method to pass fragments:
myViewPager.setCurrentItem(int fragment_position);
If you need more information just say it.
I want to make something like transitions between ViewControllers in iOS 7 but in Android, is it possible?
Explanation: please look at image below, I have main Activity which has ViewPager inside, so we can swipe between Fragments. I want to call another Activity (or Fragment) from each Fragment (blue on image) swiping to bottom or to top but this action should be done smoothly like in ViewPager.
You can intercept swipe up and down gestures and set items in viewpager accordingly. Please read this guide on how to intercept swipes in various directions.
The ViewPager object is implemented such that the next screen is already created in memory before the user swipes to it. ViewPager has a method setOffscreenPageLimit(n) in which you can set the number of ViewPager fragments that are created in advance, but n=0 is not allowed. The reason behind that is to garantee a 'smooth user experience'. In my case however then content of page n+1 is determined by what the user has done on page n. For instance if the user has clicked on a checkbox on page n, it can happen that some widget should not be shown on page n+1. My question is: how can I ensure that page n+1 is recreated? If that goes at the cost of a 'smooth user experience' so be it. I am able to intercept the swipe event in:
pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
In that method I can call a refresh() method of the current fragment. The problem is: what do I do in that refresh() method or is this the wrong approach? The user interface I want to recreate is in the onCreate method, a callback method. Or can't this work and do I need to replace the Fragment (n+1) in memory with a new one and if so, how do I do that?
Any help would be appreciated.
Trying to replace the fragment associated with a page in a ViewPager is going to be the responsibility of your fragment-based PagerAdapter. I am not aware of an easy way to accomplish that with FragmentPagerAdapter or FragmentStatePagerAdapter. My ArrayPagerAdapter can handle it better, though I don't offer a direct replace() operation.
In your case, the better solution is to adjust the fragment that you have, rather than to replace the fragment outright. In theory, this should be possible for just about any degree of change. In practice, there is probably a level of complexity after which trying to replace the fragment would be simpler than trying to have a mashup of all possible fragments.
Note that nested fragments might be another option, where the N+1th page was a placeholder and ran a FragmentTransaction to populate itself when shown. Nested fragments are tricky and quirky.
With regards to the "holes", View.INVISIBLE indicates that you want the widget to continue taking up space (e.g., so stuff after it in a LinearLayout does not move), but do not draw the pixels. View.GONE means that the View is totally ignored for layout purposes, though it is still in the hierarchy and so can be easily toggled back to View.VISIBLE if needed. Another possibility would be to remove the View entirely from the parent container, though this should have no visible change when compared with View.GONE.