I want to use a view page to read an indeterminate number of files, i have an activity that creates a person's profile and saves the data, and now i need one that uses ViewPager to access the files with only a swipe, i have the layout created and i can load a single file to it and i have a list of all the files in that directory the only thing i need is to implement that in a viewpager in order to switch between my Profiles, i don't know how to use ViewPager can someone please help me out in this? providing an example code, or a useful link that goes according to my problem?
Example of using ViewPager with fragments
Example of using ViewPager without fragments
You should implement FragmentStatePagerAdapter
which is best for paging across a collection of objects for which the number of pages is undetermined. It destroys fragments as the user navigates to other pages, minimizing memory usage.
Check this tutorial:
http://developer.android.com/training/implementing-navigation/lateral.html
Related
I have an application with a single page. But I want to create another page for an information from the file that I fill up using background service. I want it look like as swipable tabs. What is the best option to do it ?
I have only 1 Activity and 1 service. Service is just for getting information from web and saving it to file. First page is for controls, second, which I want to add, is for ListView that will read from that file service is filling up (swipable tabs will be the best). I tried to use Fragments, but there is too much to rewrite in order to transfer logic to Fragment.
Your Best option is to use ViewPager and Fragments as the UI codes will be clean and will easily be obtained from internet.
The other option if you are not willing to rewrite the code is to use a Tabbed View and clicking on the tab will hide and show elements in the mainlayout( Will not have animations though)
I am facing a situation as of few days now. I intend to create an activity where there is a TabLayout and a ViewPager.
The tabs in TabLayout corresponds with the fragments/slides in the ViewPager.
Now as of now, each fragment contains the same format,i.e., two TextViews, one under the other, populated with the strings softcoded in string.xml
But this resulted in the creation of too much xml files for the fragments used inside ViewPager.
So I was thinking if it's possible to use only one fragment inside the ViewPager and then programmatically set the strings for those two TextViews in the fragment, that changes each time w.r.t. click on another tab or sliding on the ViewPager area.
This will lessen the no. of fragments to only one, in turn cutting the time to create it and increase the overall performance of the app in which it will be present.
So any insightful help, suggestion, or walkthrough on how to implement this concept - any help on how to do this will be highly appreciated.
You can certainly do that. If you're using FragmentStatePagerAdapter, just initiate the same fragment with different arguments supplied according to it's position and then in fragment check for those arguments and make changes accordingly.
I am new to developing on android, finding myself somewhat confused regarding fragments and activities, and when to use the former specifically.
I want to achieve the following:
Have an activity with buttons for displaying different graphs. The selected graph should appear on screen in a panel overlaying the screen, or in fullscreen, and it should have functionality/buttons e.g. for selecting a graph timeframe.
Would creating each graph-page as fragments, routing events to the main activity be a good idea here, or should I just make a new activity for each? Or are there better options?
Cheers
I wouldn't recommend to use separate activities for this task.
The fragments are a great option for your case. You can save the state of each fragment and thus avoid recreating the graph views every time (which saves lots of CPU time if amount of data is big).
Read info about FragmentTransaciton and of course learn about working with Fragments in general. Maybe you should also try using ViewPager if you want to avoid switching fragments by yourself.
In case of using ViewPager you should use FragmentPagerAdapter (this one saves fragments for you) and you will just switch between them from your MainActivity. In each of the fragments you will implement your own graph with its own (or shared) layout file.
This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs. The fragment of each page the user visits will be kept in memory, though its view hierarchy may be destroyed when not visible.
I want to make a leitner application . the data is coming from database .
For instance , I've about 500 rows in my database and I want to make a viewpager for showing them , each row comes on one page so I'll have 500 pages in the viewPager .
The question is , What is the best way to implement something like this ? is it going to have memory problems or something ?
What are the better ways to do so ? if no other way ,What type of viewPager is better to use for database? A fragment viewAdapter for example
For this you should use a normal ViewPager with FragmentStatePagerAdapter.
As described in 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.
In my app I have the dropdown enabled in the ActionBar. The user has two elements to choose from. Depending on the choice I want a ViewPager to show different contents from different FragmentPagerAdapters. The user has to have the ability to switch all the time.
I've tried to set two different PagerAdapters in the listener with no luck. The Pager would just reload the previous fragments every time. Similar problems are described here and here. However in my approach I don't want to change the content of one adapter. I really just want to switch between two separate adapters.
This whole thing seems really confusing to me. Are there any known workaround for this or is there an alternative solution to my problem?
If it is possible for your application, I would suggest using FragmentStatePagerAdapter instead of FragmentPagerAdapter. Each adapter should be able to store a separate list of fragments, and should keep the saved state of your fragments when you switch to the other adapter.
I've tried to set two different PagerAdapters in the listener with no luck. The Pager would just reload the previous fragments every time.
I am assuming that you are using FragmentPagerAdapter or FragmentStatePagerAdapter. Both of those store their fragments by tag name in the FragmentManager, and therefore this switching approach will not work.
Are there any known workaround for this or is there an alternative solution to my problem?
One approach would be to fork FragmentPagerAdapter or FragmentStatePagerAdapter, replacing the makeFragmentName() method to use an alternative tag syntax, and use the revised adapter class for one of your two.