i have been following this tutorial for the pagerAdapter.
http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/
My problem is that i dont want only to have a different layout for every page but i would like to create a new activity in order to have some java code connected with every layout.Is it possible?
You can't assign different Activities to the pages of a ViewPager. You can, however, assign different Fragments, which should be enough. Check out the docs about the FragmentPagerAdapter class.
Related
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.
In Xamarin, I currently have two different activities and I would like to combine these together and use a ViewPager.
One activity uses the following content view: SetContentView(Resource.Layout.MapWithOverlayLayout);
The second activity uses the following content view: SetContentView(Resource.Layout.HomeScreen);
Can I please have some help to code a simple ViewPager that can add the two above content views into a ViewPager and then show each of these content views via code?
Thanks in advance
For a ViewPager, you will need Fragments instead of Activities.
Here is how to implement a ViewPager with Fragments and different layouts: How to implement a ViewPager with different Fragments / Layouts
The basic idea is that you create a Fragment for every layout you have, and then inside your FragmentPagerAdapter, load the correct Fragment depending on which page you are currently on.
This is how I am doing it in my other post and sorry about not posting the same code here.
Take a look at those links. It will help for sure.
Android:How to create different view in ViewPager?
Its a pretty simple implementation. You can define your own layouts and then use it in the view pager.
Here's my other post where you can get what you want to achieve:
How to use swipe gesture in a view pager's page with android?
Hope this helps..:)
I'm designing a similar interface to MathStep pictured below but my main activity already extends
public class MainActivity extends Activity implements TextWatcher
and Java doesn't have multiple inheritance.
In this program, you can ViewPager between Basic, functions and extra tabs. My program is not using tabs but I am swiping between fragments of buttons in a RelativeView.
How do I have part of my screen as a ViewPager, if I'm already extending Activity? Do I need to have a separate java file? How do I link the fragments that will contain the button sets back to the original activity?
I have been working on this with a friend for, I'm not kidding, 8 hours before we resorted to asking a question here. I have looked at this and this post and they were very helpful in understanding how ViewPager and Fragments work, but in all the examples they reference full paged ViewPagers, every time we attempted to create this scenario, something either wouldn't compile, or we weren't linking our XML correctly and code wasn't running... I've searched extensively for this answer and I hope I'm not the only one who has struggled with this so others can learn.
I'll admit, part of the confusion has been the learning curve but that's why I'm doing this, to learn.
How do I have part of my screen as a ViewPager, if I'm already extending Activity? Do I need to have a separate java file? How do I link the fragments that will contain the button sets back to the original activity?
All Fragments have a reference to the Activity via the getActivity() method, but you should only use it if you really need a handle to the context.
Inheritance is not required in any way whatsoever (technicality: other than for your activity, fragment, and FragmentPagerAdapter which must inherit from their respective parent classes...). The ViewPager itself can be included in the view heirarchy by referencing it from XML. The different fragments are displayed in the ViewPager by a FragmentPagerAdapter that you will have to implement yourself, this should be a separate class. If you want, it can be an inner static class, but, do not use an inner class. Keeping the scope organized by forcing dependencies to be passed through constructors will keep your code clean.
You should start by reading the ViewPager/Fragment related documentation on d.android.com. There is example code for these things and once you understand them individually everything will come together.
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.
Is it possible to accommodate a Fragments view inside a Gridview. I m unable to find any support with regards to this in the internet.
My basic requirement is i m unaware how many fragments i would be in need i to display wherein the number of fragments would be decided dynamically. Each fragment would contain a separate webviews inside it.
Any sample code would be of great help
Fragment doesn't inherited from View. So its impossible to populate grid with fragments. GridView needs in BaseAdapter which should implement View getView method.
You should write your custom views instead of fragments.
Fragment is independent from view even also from activity .it has life-cycle .I mean you cant make fragment inside GridView. Either you have to make your own custom view by extending gridview .or Use only fragment. Use add and replace to do more ...if you want to play with fragment.
You cannot use GridVew but you have a GridLayout, where fragments can be put