MvvmCross: How to programmatically construct an MvxListView with custom adapter? - android

I am trying to implement a grouped listview on Android similar to iOS. Therefore, I am trying to write my own custom MvxAdapter that supports grouped section headers. The default MvxListView constructed from axml will create a default MvxAdapter. Since I need to supply my own custom MvxAdapter, I need to create the MvxListview programmatically so I can pass in my own adapter. The problem I am having is at the time of OnCreate of my android view where I try to construct my custom MvxAdapter, the Android binding context is null as retrieved from
MvxAndroidBindingContextHelpers.Current()
Is there an example of constructing an MvxListView programmatically with a custom MvxAdapter with v3 API?

There's no examples of creating an MvxListView programatically - almost all Android UI controls are created in axml in the current samples.
For creating custom adapters, there are a few examples around, inclduing:
an example in the polymorphic list in the collection at: https://github.com/slodge/MvvmCross-Tutorials/tree/master/Working%20With%20Collections
an advanced example in the https://github.com/slodge/MvvmCross-Tutorials/blob/master/Sample%20-%20CirriousConference/Cirrious.Conference.UI.Droid/Views/SessionsLists/BaseSessionListView.cs
Alternatively, you can, of course, inherit a CustomListView from MvxListView and can then pass in your custom adapter as part of the constructor.
For more on creating and using custom views, see http://slodge.blogspot.co.uk/2013/05/n18-android-custom-controls-n1-days-of.html
In the event that you ever do want to push a context onto the stack you can do this using:
using (new MvxBindingContextStackRegistration<IMvxAndroidBindingContext>(**TheContext**))
{
// create your controls here
}
This is exactly what happens during xaml inflation - see: https://github.com/slodge/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Binding.Droid/BindingContext/MvxAndroidBindingContext.cs#L47

Related

How can I do for-loops with Android databinding?

Most popular MVVM-frameworks allow some kind of loops for list type properties. For example Knockout has the foreach-binding. It allows you to loop through the elements of the list property and for each element the markup contained in the foreach-loop is duplicated. Inside the copy, the current element is used as the context for data-binding.
I was looking for something similar in Android, but I only saw the possibility to bind a list to a specific ui-element. But this is bad with respect to declarativeness because I need to create a UI element in code.
Is there a way to simulate something similar using the data binding features from Android? If not, is there a workaround? Or a way to extend the binding syntax?
As #tynn answered, this kind of api is not available.
To achieve the same result, you can setup an adapter for RecyclerView in XML itself. I have implemented this feature in my MVVM library. See Setup RecyclerView from XML itself.
Your XML will look as follows:
<android.support.v7.widget.RecyclerView
bind:items="#{vm.itemVms}"
bind:layout_vertical="#{true}"
bind:view_provider="#{#layout/row_item}" />
<!-- Same arguments for ViewPager-->
<android.support.v4.widget.ViewPager
bind:items="#{vm.itemVms}"
bind:view_provider="#{#layout/row_item}" />
Additionally, there is support for displaying different kinds of child views based on your ViewModel. See Using different types of child views
It's not possible as is. The android databinding library uses static XML layout and binds values to attributes. Creating static layout dynamically is not supported and I assume won't ever be. If you want to create a view hierarchy dynamically you still should use views with adapters. That's something you can do with databindings. Just create your own databinding adapter for binding a list to a RecyclerVier. You could bind to a pair of binding:iterator and binding:layout attributes for example and set the RecyclerVier.Adapter inside the databinding adapter.

Xamarin Android non-static ListView example

I'm having a difficult time creating a ListView using a custom class and a custom layout to visualise data. Creating a custom Adapter extending BaseAdapter doesn't seem to be capable of being updated with new data, and I haven't found any examples of using a custom ArrayAdapter that isn't based around simple data types. Any suggestions what should I do?
As long as you call notifyDataSetChanged() on BaseAdapter, there should be no problem updating your custom views with new data. I have used ListViews and RecyclerViews with custom views no problem using custom adapters.

Creating a custom android view within another custom android view

So, I have the basic structure of creating a custom android view component locked down (i.e. create the view's layout xml, create a class for that view that references the layout xml, and then use the class name in an layout xml that uses the custom view) and that's all fine.
However, if I have a set of custom android UI components, can I combine them to create an even bigger custom android UI component? And if so, how would i reference the "inner" custom android UI components in the class that defines the "outer" android UI component?
The real issue here is that the TypedArray Class used to extract values for a custom UI component has only methods for extracting individual attributes (e.g. there is a "getString()" but no "[] getString()" that would return an array of strings that would then be used to update a set of custom UI components at once).
Is it possible in android to create a composite custom android UI component made up of custom android UI components?
(updated: Here is a link. http://postimg.org/image/nbnfq4nlp/2916ab5b/. Here a set of one QRCode Image, time and location TextViews form one Custom View (QRCodeScanItemView) while a group of QRCodeScanItemViews and the prominent date form another View)

How to create custom Spinner?

I am creating a custom Spinner in Xamarin.Android with text and image. I've created the Spinner in Xamarin but don't know how to customise it.
For example how do I create rounded corners and with customised text and image?
Can anyone tell me how to achieve this? I was not able to upload the screen shot of what i have done so far since my reputation is low.
It is done exactly the same way as in Native Android development. You need to implement a custom Adapter to fill the Spinner with Views that live up to your requirement. Such an Adapter handles which View is shown when and at which position. It handles inflating these Views and populating them with the correct data.
As for styling the Spinner, it is done through the Styling system that Android provides. Which is not specific to how Xamarin.Android (or MonoDroid) does it.
You need to look into the documentation about Custom Adapters, read Creating Custom Row Layouts (in this case they are for ListView, but can easily be adapted to Spinner by overriding and implementing the GetDropDownView) and Spinner

Broken inheritance hierarchy when using Pixate within Xamarin.Android app

I'm trying to add Pixate to our cross-platform app built with Xamarin.
In Android app I'm using custom ListView and custom adapter which implements an interface. But when the Adapter property of the list view is being set to my custom adapter its base class is Java.Lang.Object instead of BaseAdapter and does not implement the interface anymore (cast will cause an exception).
Anyone had a similar issue with Pixate ? How do I fix it ?
I've answered to this one on the github repo, so here is a copy-paste of that.
Pixate work on AdapterViews by proxying them in order to tag the recycled views with their new position (see https://github.com/Pixate/pixate-freestyle-android/blob/master/pixate-freestyle/src/com/pixate/freestyle/PXHierarchyListener.java#L232 ). This allows us to style more complex CSS expressions, like "nth-child" on a list.
Currently, that java.lang.reflect.Proxy is being created with a few known adapter interfaces, so the casting will fail if you would like to get your original BaseAdapter.
The good news is that we have a way to get to that original Adapter via PixateFreestyle.getAdapter(AdapterView view). Then you can cast to your BaseAdapter. That should not cause any problem.

Categories

Resources