Load custom position of page when create viewpager fragment - android

I got a problem with viewpager fragment.
Example: i got an array: 1-2-3-4-5-6.
I want when i create viewpager fragment, it will jump to fragment position 3.
Now the way viewpager do is: load all the list seriated like (1 and 2 and 3 and..), after that this set viewpager.setCurrentItem(3,false).
But with the way like that, it take more time when user click to open viewpager, is any way to do like:
When create viewpager, it will load fragment with position 3 first, after finish build UI of fragment(3), it will load another position of fragment, so when user click to open viewpager, they will not have waiting for viewpager create.
So please give me the ideal for that case.

Related

Fragment initialized without onCreate being called

So I have a fragment inside a viewpager which is contained inside a fragment which is getting initialize'd as shown in the debugger but doesn't have it's onCreate,onCreateView or any such methods being called. The activity containing has a bottom navigation view and contains 4 such fragments and this issue is only happening in 1 such fragment.
All these fragments and viewpagers and fragments inside them are created on the oncreate of the activity. If I move the logic to create the fragment to when a bottom tab is clicked, this issue gets solved on.
How is this possible ?
Only the currently active fragment and the two directly adjecant fragments are created when the viewpager is first shown. so the fragment for the first page, the fragment for the page shown if you "scroll to the left" and the fragment for the page shown when you "scroll to the right". the 4th fragment will be created when it is put in the next adjecant position.
so if you have fragments a,b,c,d in a viewpager like -a-[b]-c-d- where b is the first visible page, only a,b and c will be created at startup. when you scroll to c -a-b-[c]-d- onCreate for fragment d will be called.
You can control number of pages/fragments preloaded when you launch your activity by setting it's off Screen Page Limit on your view pager adapter. like this
mViewPager.setOffScreenPageLimit(limit)
If you set limit to 0 than only first fragment would be load.
https://spotandroid.com/2016/11/23/android-tricks-viewpager-and-offscreen-tabs/
https://techcodegeek.wordpress.com/2015/06/23/android-viewpager-and-performance-improvements/
Need more detail to know your issue.Issue which you are facing is not understood with information you have provided.

How to swipe into a Fragment as big as the screen from a View not as big?

I am trying to create a main page in my android app in which there are multiple things a user can select to do. For this page, I am thinking of using a RecyclerView with a LinearLayoutManager. For each of the views in the RecyclerView (none of which are as big as the screen), I want a user to be able to swipe into a Fragment (which is as big as the screen).
I have already tried using multiple ViewPagers to solve this problem. When keeping the ViewPager as big as the screen (the default), the Fragment would be the correct size, except the view in the RecyclerView would be as big as the screen (I want it to only wrap_content). When shrinking the size of the ViewPager, the Fragment would not be as big as the screen, but the View in the RecyclerView would be the correct size.
I have heard that one can listen for a user's swipe on a view, except:
- I don't know if that is what I should do in my case, and
- I don't know how to do it for my situation
If listening for a user's swipe is the correct way to go, please give some direction and/or code on how to complete what I want to do. If it is not the way to go, please provide an alternate way.
Thanks in advance!
Update: Here is a picture describing what I mean:
// define your fragment with recycle view
class FragmentA extends Fragment {
// with recycler view
// in click on recycler item method as i posted
// 1) do check if pager fragment exist in pager adapter
// 2) if not create one
// 3) and move to it using pager method
}
// define second without
class FragmentB extends Fragment {
// without
}
// create pager
ViewPager vp = new ViewPager(Context);
// create list with fragments for pager adapter
// when you will swipe you will move from A to B
List<Fragment> // add FragmentA + FragmentB
// https://developer.android.com/reference/android/support/v4/view/PagerAdapter.html
// create pager adapter - implement own or user FragmentPagerAdapter
PagerAdapter ... = new MyPagerAdapter(List<Fragment>);
// set view pager with adapter
viewPager.setAdapter(PagerAdapter);
// add view pager to activity view - or some content view
I already tried this on my own; the viewpager would not allow for the
correct sizes. Either the full-screen fragment size was shrunk down to
the size of the View in the recyclerview, or the view in the
recyclerview was full-screen. – Flare Cat
its working:
Oh wait, read the code wrong. But still, I need to base FragmentB off
of what was swiped in FragmentA – Flare Cat
solution:
when you click on recycler item dynamic create new fragmentB and call move to fragmentB on pager (before create new one do a check if fragment not exist already!)
or if you don't use any click on item method but just constant recycler item content create such fragments corresponding to each entry in recycler
or create some sort of map which will map each entry in recycler to to its corresponding fragment content
As I understand it, I would need to create an onClickListener for each
view in the RecyclerView. But, would that onClickListener be called
when a user swipes the corresponding view? – Flare Cat
Actually, this might work with an onTouchListener. I'll have to try it out. – Flare Cat
once again:
it depends what u want to achieve - if recycler is a way of brief for each fragments and you know the content and want to present each entry for recycler item counting from top to bottom then you need add those fragments one by one in same order from left to right
#FlareCat but why using pager here ? not better to open new activity or add fragment with transition on recycler item click ?

Viewpager , How to switch the current page to another page which is not included in the ViewPager , with keeping the Tablayout at the bottom

First , Sorry for my vague title ,
I really don't know how to describe the problem I met.
I will try to draw a picture to explain.
In the following picture , there is a ViewPager with a TabLayout pointing to the first page.
so it works like this :
And the problem is , How to switch the current page to another page which is not included in the ViewPager , with keeping the Tablayout at the bottom and pointing to the current tab. Like this:
In the other words ,
Is it possible to group several pages(fragments) to a tab?
I think you should create custom FragmentPageAdapter for your ViewPager. That allows you replacing Tab1's base fragment (Current Page on your last picture) with another fragment (Another Page on your last picture).
You can do it e.g. after click on button on base fragment.
If you wont to go back to base fragment after click on Tab 1 you have implement OnTabSelectedListener having regard to setup with view pager.
I don't attach any code because I don't know how your code looks like.

The fragment data of the corresponding tab of viewpager need to be loaded only when the tab is clicked

I am having a viewpager and having 3 tabs in it. All the three tabs are having their fragments. What I noticed that each fragment data is getting loaded at the first time when I am setting adapter to viewpager.
What I need is to load the fragment data of the corresponding tab only when the particular tab is getting clicked.
I have noticed one more issue in it. Like when the activity started in which I am having viewPager, then the data of the first tab's fragment get loaded at the same time the data of the second tab's fragment also get loaded and that is shown when I click on that tab.
Same way when I click on Second Tab, third Tab's Fragment date get loaded.
So what I need is that the data of the fragment should be loaded only when I click on the particular tab.
Thank you so much in advance..
You should use the ViewPager.setOffscreenPageLimit(0) to only load the currently displayed content.
Its the View-pager behaviour that it loads one page left/right to current page even if you have specified setOfflinePageLimit(0).
To load each fragment on click on tab, you should use LocalBroadcastReciever.

How to Start Any Fragment at the bigining by using ViewPager

Hey i have 5 Fragments like Fragment1,Fragment2,Fragment3,..Which i am Displaying in a ViewPager But I want Fragment2 should display first instead of Fragment1when i am starting my app.
After setting the adapter for ViewPager, you can simply use setCurrentItem(position) to set the page selected at required position.
mViewPager.setCurrentItem(position);

Categories

Resources