How to display different layouts with the ViewPager? - android

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..:)

Related

How to combine two activities together to use it as a single feature?

I am stuck with a problem where I have designed a tab layout that I have used as a library in the current project which has a Recycler View. I want to show the tab layout on top and the recycler view under the tab layout.
For reference in Whatsapp, there is tab layout under which there is a recycler view that shows all the messages.
Problem is that I am confused about how to combine both activities together so the user can use the tab layout.
If anyone knows any solution please help!!
Use single-activity pattern. This pattern means that you need to use Fragments in a single activity (MainActivity, etc). Additional tip, you can use ViewPager2 with TabLayout for your Fragments.
Activities can't be composed into one, cause it designed so.

How is it possible to use a single fragment in place of multiple fragments to load same format contents inside of ViewPager

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.

What is the convention for using Fragments and views in a ViewPager?

I have a ViewPager and I want to use implement fragments for it, but I don't know what is the best way to do it, should I have multiple instances of a fragment with different views or should I have multiple fragments with their own views?
Edit:
What should I do when I have different layouts, exactly?
Check out the code on my git.
This includes infinite scrolling along with header that moves along scrolling and clickable pagerview pager with fragments with listview
You just have to replace the fragmented layout to however you want.
Im using this for my own sns and its very speedy. Make sure you have asynctask for any heavy loading

How to implement a horizontal scroll / swipe between Fragments?

I would like to achieve a navigation in my app like Pinterest or Trello, that is, kind of three tabs to navigation + horizontal scrolling.
I have made a custom tabbar for this (since I couldn't guess how to change tabs width in my actionbar with navigation TAB mode). So I have three buttons to navigate from one fragment to another. Now I would like to implement the horizontal scrolling like these two examples, to also navigate among my fragments.
I have read about View Pager but I don't know if it fits to my case, since I don't have only views but fragments. Does anybody have an example or an idea of how to do it? Could I apply it among different activities?
Thanks
Please take a look at the duplicate question that I just answered:
How to implement a swipe-gesture between Fragments?
I suppose the Android ViewPager is what you are looking for:
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
Here is a nice tutorial on how to implement it:
http://developer.android.com/training/animation/screen-slide.html
The basic idea is that you have multiple Fragments, each representing
a different Screen. The ViewPager enables the user to swipe between
the different Fragments and display different content.
You can use ViewPager. More information about viewpager this.

Fragments inside a Gridview in Android

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

Categories

Resources