MvvmCross ViewPager not show in Activity - android

I want to show a few tabs sharing the same viewmodel with viewpager in an activity. I am using MvvmCross 5.6.2.
When I do that in a fragment, the viewpager is shown properly. Using the same method, I add tab fragments to the viewpager in an activity with code like this:
var viewPagerFragmentList = new List<MvxViewPagerFragmentInfo>
{
new MvxViewPagerFragmentInfo("Personal", typeof (SettingPersonalFragment), ViewModel),
new MvxViewPagerFragmentInfo("Preference", typeof (SettingPreferenceFragment), ViewModel)
};
viewPager.Adapter = new MvxCachingFragmentStatePagerAdapter(this, SupportFragmentManager, viewPagerFragmentList);
var tabLayout = FindViewById<TabLayout>(Resource.Id.tabs_fragment_setting_viewpager);
tabLayout.SetupWithViewPager(viewPager);
The tab fragments are not shown on screen, though the tab fragment's OnCreate and OnCreateView are executed.
I found the latest sample Playground is showing tabs in activity too. Following the sample, I register the fragment:
[MvxTabLayoutPresentation(TabLayoutResourceId = Resource.Id.tabs_fragment_setting_viewpager, ViewPagerResourceId = Resource.Id.viewpager_fragment_setting_viewpager, Title = "Personal", ActivityHostViewModelType = typeof(SettingViewModel))]
No luck with that. The last thing I found in Playground to show a tab is to navigate to the fragment viewmodel:
_navigationService.Navigate<Tab2ViewModel>();
_navigationService.Navigate<Tab3ViewModel>();
However, in my case, I do not have a viewmodel for each fragment. The workaround on my hand is to move the viewpager to a fragment in the activity.
How could I show the viewpager fragments without their own viewmodels in an activity?
Thanks,
Nick

The above code that works in fragment also work in activity. There is an issue in the xml causing the viewpager not showing. Sorry.
Thanks everyone who helped.

Related

How to make shared transition animation from ViewPager to another Fragment?

I have the next situation. Activity[FragmentWithPager[PageWithGrid]] and I want to make trandition animation from list item to DetailFragment but I don't know wnat I do wrong. I have added the same transitionName to views in list item and fragment detail layouts and try to make transition animatiot with the code below.
val fragmentTransaction = supportFragmentManager.beginTransaction()
transitionItems.forEach { view ->
fragmentTransaction.addSharedElement(view, view.transitionName)
}
val fragment = DetailFragment()
val transitionSet = TransitionSet().apply {
addTransition(ChangeTransform())
addTransition(ChangeClipBounds())
addTransition(ChangeBounds())
}
fragment.sharedElementEnterTransition = transitionSet
fragment.sharedElementReturnTransition = transitionSet
fragmentTransaction.replace(R.id.root, fragment)
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()
I found what I did wrong. Transition name must be unique for each item in list and page if you switch fragment with pager. Animation began to work when I have added page and list index to each transition name.
example project
Shared Element Transition API is a bit tricky and has some undefined behaviors, Even if you could manage to implement this, The transition wouldn't be smooth, Here's a work around that I've implemented a few month ago:
Instead of directly hosting the GridFragment inside the ViewPagerFragment add another Fragment as a container:
Activity[ViewPagerFragment[ContainerFragment[GridFragment]]]
Now you have a simple Fragment to Fragment Transition that works smoothly.
Here is the sample code in my github repo
Screen Shot
The steps to implement a shared transition is to
make your transition animation.
Transition name in your xml view
Add SharedElement with the fragmentTransition Object along with the target view and the transition that to be performed.
you can have look for reference 1, 2, 3
i have found this sample by google it is very helpful and it handles the fragments in a very pretty way
google transition sample
hope it helps u

How to set a specific tab when opening a Sliding Tab Layout activity?

I tried this tutorial and it works great, but now I would like to know if it is possible, when opening the application, to go directly to a specific tab and not the first one (at left) by default ?
Ex : I would like the application to open directly on "events" tab :
I searched into the code, but didn't find anything relevant.
Thanks folks
EDIT :
Here is the way :
Just add pager.setCurrentItem(1); in your MainActivity (the tabs container).
ViewPager is most often used in conjunction with Fragment, which is a
convenient way to supply and manage the lifecycle of each page.
You can use setCurrentItem // Set the currently selected page.
Set the currently selected page. If the ViewPager has already been
through its first layout with its current adapter there will be a
smooth animated transition between the current item and the specified
item.
Finally,
Your_View_Pager_Obj.setCurrentItem(1); // call into onCreate(Bundle savedInstanceState)
Use the following code along with the viewPager and tablayout
pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
you can use setCurrentTab(index) in onResume() of MainActivity where index is the index of tab you want to go to.
Add to the MainActivity.java
private static final int EVENTS = 1;
private ViewPager mViewPager;
mViewPager = (ViewPager) findViewById(R.id.pager);
#Override
public void onStart() {
super.onStart();
mViewPager.setCurrentItem(EVENTS);
}

How to swipe a frgaments inside viewpager using robolectric in android

I want to swipe the fragments left and right which is added inside a viewpager .But i am not able to get a method in robolectric .Is there any way to achieve this in robolectric
You could do it like this..
// ... initialise your activity using Robolectric.buildActivity().create().start().visible() etc.
ViewPager viewPager = (ViewPager) myActivity.getWindow().findViewById(R.id.view_pager_tabs);
viewPager.setCurrentItem(idOfFragmentInterestedIn); // or use getCurrentItem()+1 to go right
When I tried this, I had some problems with the Fragments not getting loaded in the ViewPager when testing, but while it definitely occurred only with Robolectric, it may have had more to do with our own implementation rather than the framework, so I can't make any good suggestions out of that.
The first thing you need to do is to find a reference to your viewPager and set the page (Fragment) you want to display (As Alex Florescu said before):
ViewPager viewPager = (ViewPager) activity.getWindow().findViewById(R.id.viewPager);
viewPager.setCurrentItem(desired_tab_position);
Then you need to make sure the fragment is added to the fragment manager by doing it manually:
Fragment desiredFragment =
activity.getSupportFragmentManager().findFragmentById( R.id.fragmentId );
FragmentManager fragmentManager = activity.getSupportFragmentManager();
fragmentManager.beginTransaction().add( desiredFragment, null ).commit();
activity.getSupportFragmentManager().executePendingTransactions();
This will ensure that your fragment is actually created and you can access any view that belongs to your fragment.

ViewPager using WindowManager

I am using WindowMananger to show a view on screen pretty much like what Facebook Messenger does with ChatHeads. It works fine for simple views but I have a trouble adding something like a viewpager. Can anyone help me how to add a viewpager like UI using WindowManager.
Here is a sample of code :
ViewPager viewPager = (ViewPager) mDialogView.findViewById(R.id.view_pager);
//returns a list of fragments
List<Fragment> fragments = getFragments();
mMyAdapter = new MyAdapter(*--GET FRAGMENT MANAGER--*, fragments);
viewPager.setAdapter(mMyAdapter);
The problem is how to get the fragment manager because the WindowManager is used from a Service and not an Activity
I followed the following tutorial : http://www.piwai.info/chatheads-basics/

How to switch tabs programmatically in Android from fragment?

I have implemented a TabActivity which extends FragmentActivity. It has 5 tabs each tab is a Fragment. What i am looking for is to switch between the tabs programmatically.
For eg: if i am in tab4. On button click I want to move from tab4 to tab1. Tried a lot but couldn't find the solution for this.
Tried with the following but it doesn't help.
From SecondTab
public void switchTabInActivity(String value){
FirstTab parent;
parent = (FirstTab) getActivity().getParent();
parent.switchTab(value);
}
TabActivity
/** To Change Tab*/
public void switchTab(String tabno){
this.onTabChanged(tabno);
}
for Material support you switch the tablayout from a fragment in the following ways:
1) send a broadcast that is received by the parent activity which then modifies the tab.
context.sendBroadcast(yourintent);
2.) A modification of vino's answer,
TabLayout tabhost = (TabLayout) getActivity().findViewById(R.id.tabLayout);
tabhost.getTabAt(2).select();
tablayout is the id of the tablayout as defined in your main xml.
Finally i can switch between the tabs programatically from Fragments using the following line of code
TabHost host = (TabHost) getActivity().findViewById(android.R.id.tabhost);
host.setCurrentTab(2);
Hope it will help some one.
I have tabs (using TabLayout not TabHost(depreciated))(with Fragments) in my Main Activity in which in my first tab(fragment) with a click listener in the fragment which is for changing the current tab in my MainActivity.
I successfully change the current tab via the below in the onCreateView() method within the fragment.
TabLayout tabs = (TabLayout)((MainActivity)getActivity()).findViewById(R.id.tabs);
tabs.getTabAt(1).select();
Take a look at this answer: https://stackoverflow.com/a/5460651/198996
((TabActivity) getParent()).getTabHost().setCurrentTab(2)
If you're using TabLayout instead of TabHost , I suggest a modification to BENN1TH's answer that worked for me:
TabLayout tabs = getActivity().findViewById(R.id.tab_layout);
tabs.getTabAt(tabNumber).select();
(The difference is R.id.tab_layout)
for kotlin , Please use this form fragment
activity!!.<tab_id_in_xml>.getTabAt(tab_number)!!.select()
Here's another approach in 'newer' kotlin:
requireActivity().findViewById<TabLayout>(R.id.tab_layout)?.getTabAt(destinationTabNumber)?.select()
using ?. instead of !! to avoid asserting.

Categories

Resources