I need to show one thousands question with 4 options to fill using one activity. I got an idea to use viewpager, but cant able to show 1000 fragment.
It is possible to use ViewPager with 1000 fragments. Just make sure that your adapter extends FragmentStatePagerAdapter.
From the documentation:
This version of the pager is more useful when there are a large number
of pages, working more like a list view. When pages are not visible to
the user, their entire fragment may be destroyed, only keeping the
saved state of that fragment. This allows the pager to hold on to much
less memory associated with each visited page as compared to
FragmentPagerAdapter at the cost of potentially more overhead when
switching between pages.
Related
I'm relatively new to programming and I'm facing the challenge of having 2 tabs in my activity. In each tab there should be a RecyclerView which is identical with the other.
My guess would be to update the datasource when the user switches the tab. So one activity with one RecyclerView cares about two tabs.
But as I read more and more about it people usually use fragments for each tab.
Why? Which approach is better?
If you use fragments, you have two lists so you can properly implement dragging between pages, and returning to either tab will properly keep current scroll. You can also keep data for each tab in separate fragment, which breaks down huge source files into something more manageable.
Tip for using multiple RecyclerViews with same item types is creating single RecyclerView.RecycleViewPool and keeping reference in activity to reduce number of ViewHolders you need to create.
I have a Android app that shows lots of real-time data jammed onto one large scrolling activity.
Now i want to split it up into two simpler screens using fragments, where only one fragment may be on the screen at any one time.
I read up a whole lot on fragments and watched several videos, but before i start ripping up my code to convert it to fragments i wanted to know the following.
If i create two fragments A and B, then while showing fragment B, data comes in for fragment A. Can the controlling activity still communicate with fragment A giving it data even though its off screen? OR do i have to save the data somewhere and then when the user switches to fragment A then I give fragment A the data to be shown, while saving incoming data for fragment B which will now be off screen?
The problem is that right now im not saving any data because everything is on one screen, so as data come in i just displayed it, but if i switch to using fragments i dont know if i can do the same thing by passing the data to the fragments even if they are not on screen at the same time.
Thanks.
If you retrieve your data with multiple asynchronous requests in your Activity, you may create a fragment for each of them and move related retrieval operation into that fragment (probably to oncreateView() method). Then, you can use ViewPager (probably with TabLayout) in the parent Activity to be able to use all those fragments. Therefore, your Activity only deals with setting the ViewPager and leave the rest to fragments.
ViewPager shows one page at a time but it can initialize other fragments as well, even before they are shown. You can use ViewPager's setOffscreenPageLimit() method to increase that range.
In case you need a communication channel between fragments and the activity, you may create callback mechanisms, as described here.
I'm developing an app that uses an internal sqlite database. The MainActivity contains a Fragment.This fragment has a ViewPager which contains other 3 fragments, each of these 3 fragments has a ListView in it. when you click an item of the list, a second Activity is opened containing another ViewPager that has fragments showing the complete details(a bitmap and Strings) of each list item.
My question is, what would be better for performance ?
To make constant calls to the database each time the user scrolls through the detail fragments(ViewPager).
Make only three calls to the database (one for each listview in the mainActivity), to create 3 ArrayList of objects and keep them in memory. The list could have lots of items.
Is there something else I'm not considering ?
3. Is there something else I'm not considering ?
Yes, use ViewPager.setOffscreenPageLimit instead.
I'm trying to create a basic UI for a simple Temperature Conversion app (new to android developing), and I can't figure out how to create a SINGLE page with two SEPARATE scrollable view pagers, one occupying the top half and the other occupying the bottom one.
I am using Eclipse. Feel free to ask for any other information required for your answer.
This is a rough drawing of what the layout should look like.
I'm trying to create a basic UI for a simple Temperature Conversion app
I do not know why a temperature conversion app would need one ViewPager, let alone two in the same activity at the same time.
I can't figure out how to create a SINGLE page with two SEPARATE scrollable view pagers, one occupying the top half and the other occupying the bottom one
In terms of the ViewPagers themselves, use a LinearLayout or something to vertically stack them.
Your bigger headache will come with the PagerAdapter implementations. At least one of these will have to be something other than FragmentPagerAdapter or FragmentStatePagerAdapter. Those implementations assume that they are the only such adapter for your activity, and I would expect that having two will cause collisions.
I have a ViewPager, that i'm populating with fragments which has a listview inside. The point is that i have to show a really big amount of data, and i need to make pages, so i only request littel amounts of data.
My problem is that i need to call an asynctask to retrieve the data when the page is changed, and fill the listview of this page, how can i do this? How can i change that listview in the onPostExecute of the task?
PS: i have used an eclipse template for tabs + swipe activity, so im not posting my code.
I'm not sure I got your problem right, but the first thing that comes to my mind, is that I would use an AsyncTaskLoader instead of a simple AsyncTask. From my (limited) experience, loaders solve a lot of problems when it comes to Fragment/Activity lifecycle/configuration changes.
Loaders guide (Android Developers)
No matter what method you are using to get the data, changing the content of the list view in page B after loading the data in page A shouldn't be much of a problem: you have plenty of options, from simply saving the data for page B in the Activity (then changing the page with setCurrentItem and intercepting the page change event with setOnPageChangeListener),
to a more elegant approach employing a SQLite database (which btw would allow you to do some caching on the results, if possible).
Oh, and let's not forget that, if you are using an implementation of the abstract class FragmentPagerAdapter you can probably pass the data directly from one page to the other, as this PagerAdapter implementation
represents each page as a Fragment that is persistently kept in the fragment manager as long as the user can return to the page.
(just use getItem(int) to get a reference to the page you need. I never tried this myself, though).
Hope this helps
EDIT I just found out that getting a reference to the current Fragment shown in the ViewPager is tricky if you are not using a FragmentPagerAdapter: if this was the root of your problem, information about how it can be done can be found here:
Update data in ListFragment as part of ViewPager
Retrieve a Fragment from a ViewPager
Communication between ViewPager and current Fragment