ViewPager using WindowManager - android

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/

Related

MvvmCross ViewPager not show in Activity

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.

Why does this portion of google's android tutorial use "FragmentActivity"?

It is taking me forever to learn the Android SDK. I am currently trying to implement swipe views with tabs by following this tutorial.
I understand that we are supposed to use fragments as "mini activities", and that one fragment makes up of the many tabs attached to the ViewPager.
There is this code segment in the tutorial that I fail to grasp, however:
public class CollectionDemoActivity extends FragmentActivity {
// When requested, this adapter returns a DemoObjectFragment,
// representing an object in the collection.
DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
ViewPager mViewPager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collection_demo);
// ViewPager and its adapters use support library
// fragments, so use getSupportFragmentManager.
mDemoCollectionPagerAdapter =
new DemoCollectionPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mDemoCollectionPagerAdapter);
}
}
Does the FragmentActivity class come into play when attaching a Fragment to the ViewPager? In separate documentations of fragments and viewpagers, I cannot find mention of FragmentActivity.
I would really appreciate it if you could shed some light on this matter.
Thank you in advance.
The Adapter takes this as an argument: getSupportFragmentManager()
That is only available to a FragmentActivity. (specifically, of the Support library variety).

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.

Changing Activity classes to Fragment (using FragmentPagerAdapter)

I have two activities which I want to combine with a FragmentPagerAdapter.
I followed a tutorial and everything works great with the test classes. However, as mentioned I want to use two previously made Activities in that horizontal slider.
At the moment it looks like this:
public class CheatPagerFragmentActivity extends FragmentActivity {
...
/**
* Initialise the fragments to be paged
*/
private void initialisePaging() {
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, CheatView.class.getName()));
fragments.add(Fragment.instantiate(this, CheatMetaView.class.getName()));
this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager);
pager.setAdapter(this.mPagerAdapter);
}
}
CheatView and CheatMetaView used to be Activities but now are Fragments ("extends Fragment"). I don't need to know how I can still pass information to those (new) Fragments as I can not use "intent" anymore how I used to pass information between Activities.
Also creating new elements in the code of these classes is being marked as an error now. Like this line of code:
TableRow trTh = new TableRow(this);
How can I pass information to those classes and how do I have to change the code to create a new TableRow in the code of the former Activity class?
Thanks for any help.
Re your first question: Instead of using Fragment.instantiate, create a newInstance method for each fragment that can take the information you wish to pass as a parameter. See the page below for an example (search for newInstance):
http://developer.android.com/reference/android/app/Fragment.html

Categories

Resources