I have a Xamarin Android porject with MvvmCross.
For testing, I added a CustomImageView with a HslBackground property
public class CustomImageView : ImageView
{
private HslColor _hslBackground;
public CustomImageView (Context context)
: base(context) { }
public CustomImageView (Context context, IAttributeSet attrs)
: base(context, attrs) { }
public HslColor HslBackground
{
get { return _hslBackground; }
set
{
if (_hslBackground != value)
{
_hslBackground = value;
byte r, g, b;
value.ToRGB(out r, out g, out b);
this.Background = new ColorDrawable(new Color(r, g, b));
}
}
}
}
Here is my layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Example.MVVM.Droid.Controls.CustomImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/colorView"
android:adjustViewBounds="true"
local:MvxBind="HslBackground HslColor" />
</LinearLayout>
I added breakpoints to the HslBackground property, but either getter nor setter is called. Any idea what to do? I am using the latest MvvmCross from Nuget.
Update 1:
I see in the log
MvxBind:Error: 0.99 View type not found - md56c24830106cce038b53f4325e503c583.CustomImageView
MvxBind:Error: 0.99 View type not found - md56c24830106cce038b53f4325e503c583.CustomImageView
[art] JNI RegisterNativeMethods: attempt to register 0 native methods for md56c24830106cce038b53f4325e503c583.CustomImageView
Update 2
Binding the property to TextView works
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
local:MvxBind="Text HslColor" />
Related
I have a TabLayout with Fragments. In one of the Fragments, I want to add a RecyclerView. I have implemented the code for it and deployed it, but when I start the app I get the following error at tabLayout.SetupWithViewPager(viewPager):
Android.Views.InflateException: Binary XML file line #15: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.Button
and sometimes just
Android.Views.InflateException: Timeout exceeded getting exception details
I tried to copy the same code for the RecyclerView in a new Project but there just in the MainActivity and it worked. So I'm doing something else wrong...
My Code:
Main.axml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="#+id/tablayout_navigation"
android:layout_width="match_parent"
app:tabTextColor="#ffffff"
app:tabSelectedTextColor="#08aeab"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="4dp" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_margin="0dp" />
</LinearLayout>
Overview.axml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:minWidth="25px"
android:minHeight="25px">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
AppointmentListItem.axml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:text="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtName"
android:textColor="#fff"/>
</LinearLayout>
RecyclerAdapter.cs:
class RecyclerAdapter : RecyclerView.Adapter
{
private List<Appointment> mAppointments;
public RecyclerAdapter(List<Appointment> appointments)
{
mAppointments = appointments;
}
public class MyView : RecyclerView.ViewHolder
{
public View mMainView { get; set; }
public TextView mName { get; set; }
public MyView(View view) : base(view)
{
mMainView = view;
}
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.AppointmentListItem, parent, false);
TextView txtName = row.FindViewById<TextView>(Resource.Id.txtName);
MyView view = new MyView(row) { mName = txtName };
return view;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
MyView myHolder = holder as MyView;
myHolder.mName.Text = mAppointments[position].Name;
}
public override int ItemCount
{
get { return mAppointments.Count; }
}
}
OverviewFragment.cs
class OverviewFragment : Android.Support.V4.App.Fragment
{
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private RecyclerView.Adapter mAdapter;
private List<Appointment> mAppointments;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
View view = inflater.Inflate(Resource.Layout.Overview, container, false);
mAppointments = new List<Appointment>();
mAppointments.Add(new Appointment("Test1"));
mAppointments.Add(new Appointment("Test2"));
mAppointments.Add(new Appointment("Test3"));
mAppointments.Add(new Appointment("Test4"));
mRecyclerView = view.FindViewById<RecyclerView>(Resource.Id.recyclerView);
mLayoutManager = new LinearLayoutManager(view.Context);
mRecyclerView.SetLayoutManager(mLayoutManager);
mAdapter = new RecyclerAdapter(mAppointments);
mRecyclerView.SetAdapter(mAdapter);
return view;
}
}
MainActivity.cs:
public class MainActivity : AppCompatActivity
{
private TabLayout tabLayout;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
tabLayout = FindViewById<TabLayout>(Resource.Id.tablayout_navigation);
ViewPager viewPager = FindViewById<ViewPager>(Resource.Id.pager);
SetupViewPager(viewPager);
tabLayout.SetupWithViewPager(viewPager);
}
private void SetupViewPager(ViewPager viewPager)
{
viewPager.OffscreenPageLimit = 3;
PageAdapter adapter = new PageAdapter(SupportFragmentManager);
adapter.AddFragment(new OverviewFragment(), Resources.GetString(Resource.String.overview));
adapter.AddFragment(new CalendarFragment(), Resources.GetString(Resource.String.calendar));
adapter.AddFragment(new SharesFragment(), Resources.GetString(Resource.String.share));
viewPager.Adapter = adapter;
}
}
I have to display the following table:
So I want to know what is the best widget to use for the table. I want the table to be dynamic I mean if I choose some other group from the dropdown the number of items in the table to increase or to decrease and the total price to change depending on what I have in column price. What widget is the best widget to achieve that?
RecyclerView is the best UI control to achieve it. Because RecyclerView is Adapter pattern, so you can use the adapter to achieve dynamic what you have said.
I want the table to be dynamic I mean if I choose some other group from the dropdown the number of items in the table to increase or to decrease and the total price to change depending on what I have in column price
I have made a demo for you, you can test it and do some changes for your project:
Three .axml files:
Main.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="#+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="group1"/>
<Button
android:id="#+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="group2"/>
<Button
android:id="#+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="group3"
/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
item_layout, every item's layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/item_tv"
android:layout_width="match_parent"
android:textSize="30dp"
android:layout_height="wrap_content" />
</LinearLayout>
item_layout, total price's layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/foot_tv"
android:gravity="end"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity:
using Android.App;
using Android.Widget;
using Android.OS;
using System;
using System.Collections.Generic;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Content;
namespace RecycleTest
{
[Activity(Label = "RecycleTest", MainLauncher = true)]
public class MainActivity : Activity
{
RecyclerView mRecyclerView;
MyAdapter adapter;
Button bt1, bt2, bt3;
List<double> list1, list2, list3;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
initView();
initData();
mRecyclerView = FindViewById<RecyclerView>(Resource.Id.rv);
mRecyclerView.SetLayoutManager(new LinearLayoutManager(this));
adapter = new MyAdapter(this, list1);
mRecyclerView.SetAdapter(adapter);
}
private void initData()
{
list1 = new List<double>();
list2 = new List<double>();
list3 = new List<double>();
list1.Add(1.1);
list1.Add(1.2);
list1.Add(1.3);
list2.Add(2.1);
list2.Add(2.2);
list2.Add(2.3);
list2.Add(2.4);
list3.Add(3.1);
list3.Add(3.2);
list3.Add(3.3);
list3.Add(3.4);
list3.Add(3.5);
}
public void initView() {
bt1 = FindViewById<Button>(Resource.Id.bt1);
bt2 = FindViewById<Button>(Resource.Id.bt2);
bt3 = FindViewById<Button>(Resource.Id.bt3);
bt1.Click += Bt1_Click;
bt2.Click += Bt2_Click;
bt3.Click += Bt3_Click;
}
private void Bt3_Click(object sender, System.EventArgs e)
{
adapter.change(list3);
}
private void Bt2_Click(object sender, System.EventArgs e)
{
adapter.change(list2);
}
private void Bt1_Click(object sender, System.EventArgs e)
{
adapter.change(list1);
}
}
class MyViewHolder : RecyclerView.ViewHolder
{
public TextView mtv;
public MyViewHolder(View itemView): base(itemView)
{
mtv = itemView.FindViewById<TextView>(Resource.Id.item_tv);
}
}
class MyFootViewHolder : RecyclerView.ViewHolder
{
public TextView mtv;
public MyFootViewHolder(View itemView):base(itemView)
{
mtv = itemView.FindViewById<TextView>(Resource.Id.foot_tv);
}
}
class MyAdapter :RecyclerView.Adapter{
Context mContext;
List<double> mList=new List<double>();
public override int ItemCount => mList.Count + 1;
public MyAdapter(Context context, List<Double> list)
{
this.mContext = context;
this.mList.AddRange(list);
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
if (GetItemViewType(position) == 0)
{
((MyViewHolder)holder).mtv.Text=(mList[position].ToString());
}
else
{
double price = 0;
for (int i = 0; i < mList.Count; i++)
{
price += mList[i];
}
((MyFootViewHolder)holder).mtv.Text=price + "";
}
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
RecyclerView.ViewHolder holder;
if (viewType == 0)
{
holder = new MyViewHolder(LayoutInflater.From(mContext).Inflate(Resource.Layout.item_layout, parent, false));
}
else
{
holder = new MyFootViewHolder(LayoutInflater.From(mContext).Inflate(Resource.Layout.foot_layout, parent, false));
}
return holder;
}
public override int GetItemViewType(int position)
{
int type;
if (position != mList.Count)
{
type = 0;
}
else
{
type = 1;
}
return type;
}
public void change(List<Double> list1)
{
mList.Clear();
mList.AddRange(list1);
NotifyDataSetChanged();
}
}
}
you can see gridview in the image which is not wrapping whole nestedscrollview Guys please help me I am stucked in this code, I need a collapsing toolbar layout and I succeed too but the gridview inside the nested scrollview is not wrapping total space of nestedscrollview, I tried many ways but failed.
This is the xml which is helping me to collapse toolbar and where I am getting gridview issues,
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.patelsanket.myalbum.activities.AlbumImageDisplayActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/headerImage1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/shows_header_image"
android:fitsSystemWindows="true"
android:scaleType="fitXY"
android:src="#drawable/cod" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<GridView
android:id="#+id/album_image_grid_view"
android:layout_width="match_parent"
android:layout_height="550dip"
android:choiceMode="multipleChoice"
android:horizontalSpacing="0.5dp"
android:numColumns="2"
android:paddingBottom="20dip" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/ic_add"
app:backgroundTint="#009688"
app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end|right" />
</android.support.design.widget.CoordinatorLayout>
Use RecyclerView with GridLayoutManager. As Recycler view is compitable with Collasping bartoolbar too...
I hope it helps..
After searching a lot I found that using expandable gridview it can be solved. I used the following class and it is working fine.
This is a custom implementation of the native GridView ,
Here we override the functionality pertaining to GridView's height.
This implementation does away with a fixed Height GridView , rather
the height of the GridView expands depending upon the number of child elements added into it.
public class ExpandableGridView extends GridView{
boolean expanded = false;
public boolean isExpanded()
{
return expanded;
}
public ExpandableGridView(Context context)
{
super(context);
}
public ExpandableGridView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableGridView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
}
}
add this code
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
album_image_grid_view.setNestedScrollingEnabled(true);
}
or you can use RecyclerView instead of GridView
I am porting a native Android app to Xamarin + MVVMCross. On my Native Android app I was easily able to create my custom DateDisplayPicker and use it. I'm having trouble in Xamarin. My control lives here:
My DateDisplayPicker.cs looks like so:
namespace XxxxXxxx.Droid.CustomViews
{
public class DateDisplayPicker : TextView, DatePickerDialog.IOnDateSetListener
{
private readonly Context context;
private readonly Calendar calendar;
protected DateDisplayPicker(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public DateDisplayPicker(Context context) : base(context)
{
}
public DateDisplayPicker(Context context, IAttributeSet attrs) : base(context, attrs)
{
this.context = context;
SetAttributes();
this.calendar = Calendar.Instance;
}
public DateDisplayPicker(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
this.context = context;
calendar = Calendar.Instance;
}
private void SetAttributes()
{
Click += (sender, args) => ShowDateDialog();
}
private void ShowDateDialog()
{
var c = Calendar.Instance;
var dp = new DatePickerDialog(context, this, c.Get(CalendarField.Year),
c.Get(CalendarField.Month), c.Get(CalendarField.DayOfMonth));
dp.Show();
}
public void OnDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
Text = String.Format("%s/%s/%s", monthOfYear + 1, dayOfMonth, year);
calendar.Set(year, monthOfYear, dayOfMonth);
}
public void AddDay()
{
calendar.Add(CalendarField.Date, 1);
Text = String.Format("%s/%s/%s", calendar.Get(CalendarField.Month) + 1,
calendar.Get(CalendarField.DayOfMonth), calendar.Get(CalendarField.Year));
}
public void SubtractDay()
{
calendar.Add(CalendarField.Date, -1);
Text = String.Format("%s/%s/%s", calendar.Get(CalendarField.Month) + 1,
calendar.Get(CalendarField.DayOfMonth), calendar.Get(CalendarField.Year));
}
public string GetDate()
{
var sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.Format(calendar.Time);
}
}
}
And them I'm accessing it in my Android fragment like so:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<com.workspace.xxxxxxxx.droid.customviews.DateDisplayPicker
android:id="#+id/managerDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:text="XX-XX-XXXX"
android:textSize="16sp"
android:textColor="#android:color/holo_blue_dark"
style="#android:style/Widget.Holo.Spinner"
android:hint="Date" />
<ImageButton
android:id="#+id/managerDateMinusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/managerDate"
android:layout_marginRight="#dimen/button_padding"
android:src="#drawable/minus"
android:background="#android:color/transparent" />
<ImageButton
android:id="#+id/managerDatePlusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/managerDate"
android:layout_marginLeft="#dimen/button_padding"
android:src="#drawable/plus"
android:background="#android:color/transparent" />
</RelativeLayout>
<ExpandableListView
android:id="#+id/managerExpandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:stackFromBottom="false"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp" />
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="invisible" />
<TextView
android:id="#+id/assignedMessageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="No tasks today."
android:visibility="visible"
android:layout_below="#id/progressBar1" />
</RelativeLayout>
From the few examples I've seen this is pretty much how they've been set up but I must be missing something. Any ideas? I've confirmed my namespace for the control in the axml is all lowercase. When my application errors out I get:
Pending exception is:
android.view.InflateException: Binary XML file line #1: Error inflating class com.workspace.xxxxxxxx.droid.customviews.DateDisplayPicker
(raw stack trace not found)
Caused by:
java.lang.ClassNotFoundException: Didn't find class "com.workspace.xxxxxxxx.droid.customviews.DateDisplayPicker" on path: DexPathList[[zip file "/data/app/com.workspace.xxxxxxxx-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.workspace.xxxxxxxx-1, /vendor/lib, /system/lib]]
Never mind I was just being dumb. If I leave off the "com." this works so it seems like the lesson learned here is to use EXACTLY what the namespace is on the control class, in this case it was "xxxxxxxx.droid.customviews.DateDisplayPicker" in the axml. Hope this helps anyone else who strugged with this!
I'm currently creating an app for Android using MvvmCross. Part of that app requires a MvxListView, where each item has 2 TextViews. One of these TextViews are hidden by default. I'm looking to implement an accordion like functionality, where clicking on the first TextView will show/hide the other TextView.
I've currently gotten most of this to work with the MvvmCross Visibility Plugin, but the click event is bound to the MvxListView instead of the TextView inside it. What I've currently gotten to work looks like this:
FirstViewModel:
public class FirstViewModel
: MvxViewModel
{
public FirstViewModel(IListService listService)
{
Interests = new ObservableCollection<Interest>();
List<Interest> tempInterests = listService.GetInterestFeeds("");
foreach (var interest in tempInterests)
{
interest._parent = this;
Interests.Add(interest);
}
var pluginLoader = new PluginLoader();
pluginLoader.EnsureLoaded();
}
private ObservableCollection<Interest> _interests;
public ObservableCollection<Interest> Interests
{
get { return _interests; }
set { _interests = value; RaisePropertyChanged(() => Interests); }
}
public ICommand ItemVisibleCommand
{
get
{
return new MvxCommand<Interest>(item => item.IsVisible = !item.IsVisible);
}
}
}
FirstView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
...>
<Mvx.MvxListView
...
local:MvxBind="ItemsSource Interests"
local:MvxItemTemplate="#layout/item_interests" />
</LinearLayout>
item_interests:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
local:MvxBind="Text InterestName" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
local:MvxBind="Text InterestDescription; Visibility IsVisible, Converter=Visibility" />
</LinearLayout>
In order to bind it to the TextView inside the MvxListView, I've been trying to modify my code to something similar to How to bind ItemClick in MvxListView in MvxListView as per the anwser by Stuart, resulting in the following code:
item_interest:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
local:MvxBind="Text InterestName; Click ItemVisibleCommand" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
local:MvxBind="Text InterestDescription; Visibility IsVisible, Converter=Visibility" />
</LinearLayout>
FirstViewModel:
public class FirstViewModel
: MvxViewModel
{
public FirstViewModel(IListService listService)
{
Interests = new ObservableCollection<Interest>();
List<Interest> tempInterests = listService.GetInterestFeeds("");
foreach (var interest in tempInterests)
{
interest._parent = this;
Interests.Add(interest);
}
var pluginLoader = new PluginLoader();
pluginLoader.EnsureLoaded();
}
private ObservableCollection<Interest> _interests;
public ObservableCollection<Interest> Interests
{
get { return _interests; }
set { _interests = value; RaisePropertyChanged(() => Interests); }
}
public void MakeItemVisible(bool isVisible)
{
isVisible = !isVisible;
}
Interest:
public class Interest : INotifyPropertyChanged
{
public string InterestId { get; set; }
public string InterestName { get; set; }
public string InterestDescription { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public FirstViewModel _parent { get; set; }
private bool _isVisible;
public bool IsVisible
{
get { return _isVisible; }
set
{
_isVisible = value;
onPropertyChanged(this, "IsVisible");
}
}
private void onPropertyChanged(object sender, string propertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
}
}
public Interest(string id, string name, string description)
{
//Initialisers
}
private MvxCommand<bool> _itemVisible;
public ICommand ItemVisibleCommand
{
get
{
_itemVisible = _itemVisible ?? new MvxCommand<bool>(IsVisible => _parent.MakeItemVisible(IsVisible));
return _itemVisible;
}
}
}
resulting in the following exception:
04-04 15:05:40.575 I/MonoDroid(18011): UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
04-04 15:05:40.575 I/MonoDroid(18011): at Cirrious.MvvmCross.ViewModels.MvxCommand`1<bool>.Execute (object) <IL 0x00010, 0x00088>
04-04 15:05:40.575 I/MonoDroid(18011): at Cirrious.MvvmCross.Binding.Droid.Target.MvxViewClickBinding.ViewOnClick (object,System.EventArgs) <IL 0x0001f, 0x000fb>
04-04 15:05:40.575 I/MonoDroid(18011): at Android.Views.View/IOnClickListenerImplementor.OnClick (Android.Views.View) [0x0000d] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/a1e3982a/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.Views.View.cs:1615
04-04 15:05:40.575 I/MonoDroid(18011): at Android.Views.View/IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/a1e3982a/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.Views.View.cs:1582
04-04 15:05:40.575 I/MonoDroid(18011): at (wrapper dynamic-method) object.a963c1ac-b573-4022-b41d-f0f002438c84 (intptr,intptr,intptr) <IL 0x00017, 0x00043>
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
Thanks in advance to anyone who's taken the time to read all that :)
UPDATE - I tried to do as Stuart suggested, and got the following solution:
First off, to preserve the original Interest entity, it got wrapped in an InterestWrapper.
public class InterestWrapper : INotifyPropertyChanged
{
private Interest _interest;
private InterestAndroidViewModel _parent; //TO-DO
public Interest Item { get { return _interest; } }
public event PropertyChangedEventHandler PropertyChanged;
private bool _isVisible;
public bool IsVisible
{
get { return _isVisible; }
set
{
_isVisible = value;
onPropertyChanged(this, "IsVisible");
}
}
private void onPropertyChanged(object sender, string propertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
}
}
public InterestWrapper(Interest interest, InterestAndroidViewModel parent)
{
IsVisible = false;
_interest = interest;
_parent = parent;
}
public IMvxCommand ItemVisibleCommand
{
get
{
return new MvxCommand(() => _parent.MakeItemVisible(_interest));
}
}
}
FirstViewModel
public class FirstViewModel
: MvxViewModel
{
public FirstViewModel(IListService listService)
{
Interests = new ObservableCollection<InterestWrapper>();
List<Interest> tempInterests = listService.GetInterestFeeds("");
foreach (var interest in tempInterests)
{
InterestWrapper wrapper = new InterestWrapper(interest, this);
Interests.Add(wrapper);
}
}
private ObservableCollection<InterestWrapper> _interests;
public ObservableCollection<InterestWrapper> Interests
{
get { return _interests; }
set { _interests = value; RaisePropertyChanged(() => Interests); }
}
public void MakeItemVisible(Interest interest)
{
if (interest.IsVisible)
{
interest.IsVisible = !interest.IsVisible;
}
else
{
foreach (var _interest in _interests)
{
_interest.Item.IsVisible = false;
}
interest.IsVisible = !interest.IsVisible;
}
}
}
item_interest:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
...
<RelativeLayout
...
<TextView
...
local:MvxBind="Text Item.InterestName; Click ItemVisibleCommand" />
<Mvx.MvxImageView
...
local:MvxBind="Visibility Item.IsVisible, Converter=InvertedVisibility; Click ShowEducationsCommand" />
</RelativeLayout>
</RelativeLayout>
The local:MvxBind="Text InterestName; Click ItemVisibleCommand" can only really call a non-parameterized MvxCommand - it can't call MvxCommand<bool> as it doesn't know what the bool value is.
If you wanted to, you could use the CommandParameter converter to pass in the value - e.g. local:MvxBind="Text InterestName; Click CommandParameter(ItemVisibleCommand, IsVisible)"
But overall, in this case I'd probably recommend rewriting ItemVisibleCommand as just a "toggle visible" command instead
Look through this question it's the as your one and has the solution suggested by Stuart, so think it's what you are looking for.
Binding button click in ListView template MvvMCross