ViewModel does not get called in MVVMCross - android

In the FundamentalView.cs, I have click event which triggers a fragment from the bottom of the view with having options (adding a new person and new calculations).
var addButton = view.FindViewById<ImageButton>(Resource.Id.addButton);
addButton.Click += OnAddButtonClick;
void OnAddButtonClick(object sender, System.EventArgs e)
{
var dialog = new CardDialogView();
dialog.Show(((MainView)Activity).SupportFragmentManager, "CardDialogView");
}
CardDialogView.axml
<?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="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/dash_add_computer"
android:textColor="#color/primary_text"
android:textSize="16sp"
android:text="New Calculation"
local:MvxBind="Click NewCalculationCommand"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/dash_add_head"
android:drawablePadding="28dp"
android:textColor="#color/primary_text"
android:textSize="16sp"
android:text="New Person" />
</LinearLayout>
CardDialogView.cs
public class CardDialogView : MvxDialogFragment<CardDialogViewModel>
{
public override Dialog OnCreateDialog(Bundle savedState)
{
......
return dialog ;
}
}
The following corresponding ViewModel does not get called? I wonder what I am missing?
CardDialogViewModel.cs
public class CardDialogViewModel : MvxViewModel
{
public ICommand NewCalculationCommand
{
get
{
return new MvxCommand(() => ShowViewModel<NewItemViewModel>(new { date = DateTime.Now }));
}
}
}

You are missing this from the CardDialogView.axml layout file xmlns:local="http://schemas.android.com/apk/res-auto"
CardDialogView.axml should be this:
<?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="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/dash_add_computer"
android:textColor="#color/primary_text"
android:textSize="16sp"
android:text="New Calculation"
local:MvxBind="Click NewCalculationCommand"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/dash_add_head"
android:drawablePadding="28dp"
android:textColor="#color/primary_text"
android:textSize="16sp"
android:text="New Person" />
</LinearLayout>
Think you need to set the viewmodel on the CardDialogView like so:
void OnAddButtonClick(object sender, System.EventArgs e)
{
var dialog = new CardDialogView();
dialog.ViewModel = new CardDialogViewModel();
dialog.Show(SupportFragmentManager, "CardDialogView");
}

Related

Viewholder clickable area has margin bottom that's not defined

Here is how the problem looks -
And here is my ViewHolder code -
<?xml version="1.0" encoding="utf-8"?>
<layout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/groups_list_root_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/list_selector_background"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="#+id/groups_list_group_image"
android:layout_width="60dp"
android:layout_gravity="center"
android:layout_height="60dp"
tools:src="#drawable/multi_user_group" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="14dp"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/groups_list_group_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="16sp"
tools:text="Galipoly 38 Apt" />
<TextView
android:id="#+id/groups_list_group_members"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxEms="15"
android:maxLines="1"
tools:text="Alon Shlider, Dekel Aslan, Omer..."
android:textColor="#color/colorPrimary"
android:textSize="16sp" />
<TextView
android:id="#+id/groups_list_group_open_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="9 Open Tasks"
android:textColor="#color/orange"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</layout>
class GroupsListViewHolder(private val binding: GroupsListViewHolderBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(model: GroupModel, onClick: (model: GroupModel) -> Unit) {
binding.groupsListGroupName.text = model.groupName
binding.groupsListGroupMembers.text = TextUtils.join(", ", model.usersFullNames)
if (model.tasksCounter < 1) {
binding.groupsListGroupOpenTasks.setAsGone()
} else {
binding.groupsListGroupOpenTasks.setAsVisible()
binding.groupsListGroupOpenTasks.text = model.tasksCounter.toString()
.plus(" ")
.plus(TeamitApplication.context!!.getString(R.string.ongoing_tasks))
.plus(TeamitApplication.context!!.getString(R.string.dot))
}
if (model.usersFullNames.size == 1) {
binding.groupsListGroupImage.setImageResource(R.drawable.single_user_group)
} else {
binding.groupsListGroupImage.setImageResource(R.drawable.multi_user_group)
}
binding.groupsListRootLayout.setOnClickListener {
onClick(model)
}
}
}
private fun initAdapter() {
fetchGroupData { groupModelList ->
if (groupModelList.isEmpty()) {
binding.groupsListNoGroupsMessageTitle.setAsVisible()
binding.groupsListNoGroupsMessageDescription.setAsVisible()
return#fetchGroupData
}
binding.groupsListNoGroupsMessageTitle.setAsGone()
binding.groupsListNoGroupsMessageDescription.setAsGone()
adapter.submitList(null)
adapter.submitList(groupModelList)
binding.groupsListRecyclerview.setAdapterWithItemDecoration(requireContext(), adapter)
}
}
fun <T, VH : RecyclerView.ViewHolder> RecyclerView.setAdapterWithItemDecoration(context: Context, adapter: ListAdapter<T, VH>) {
this.adapter = adapter
this.setHasFixedSize(true)
this.layoutManager = LinearLayoutManager(context)
this.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
}
As you can see, the click area has some margin bottom that I can't understand why happens. Sometimes it happens and sometimes it doesn't and I can't figure out what wrong.
As for the extension function that I am using - I am using it for the entire application
In your first LinearLayout you have defined android:padding="10dp". I assume this causes the unwanted margin.

call another java activity from fragment class

Hey i have a simple question. i have a mobile_navigation that contain code to call a fragment class, and i have the fragment class but i want to call / pass to another java class. for example i have Inputdata.xml that when tap will go to fragment class nah i want when i tap the input xml it will instantly go to another java class not stuck in fragment class
here is the example code for mobile_navigation
<fragment
android:id="#+id/nav_pantuan"
android:name="com.joshua.r0th.crud2.ui.gallery.GalleryFragment"
android:label="#string/pantauan"
tools:layout="#layout/fragment_pantauan" />
and here is the GalleryFragment code
public class GalleryFragment extends Fragment {
private GalleryViewModel GalleryViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
GalleryViewModel =
ViewModelProviders.of(this).get(GalleryViewModel.class);
View root = inflater.inflate(R.layout.fragment_pantauan, container, false);
final TextView textView = root.findViewById(R.id.text_gallery);
GalleryViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});
return root;
}
}
and this is the another java class that i willing to reach
public class pantauan extends AppCompatActivity {
database1 myDb;
EditText editNomorRumah,editJentikDalam,editJentikLuar;
Button btnAddData;
Button btnViewAll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pantauan);
myDb = new database1(this);
editNomorRumah = (EditText)findViewById(R.id.nomorrmh);
editJentikDalam = (EditText)findViewById(R.id.jentikdirumah);
editJentikLuar = (EditText)findViewById(R.id.jentikdiluarrumah);
btnAddData = (Button)findViewById(R.id.tambahdata);
btnViewAll = (Button)findViewById(R.id.lihatdata);
AddData();
viewAll();
}
//fungsi tambah
public void AddData() {
btnAddData.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isInserted = myDb.insertData(editNomorRumah.getText().toString(),
editJentikDalam.getText().toString(),
editJentikLuar.getText().toString() );
if(isInserted == true)
Toast.makeText(pantauan.this,"Data Iserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(pantauan.this,"Data Not Iserted",Toast.LENGTH_LONG).show();
}
}
);
}
//fungsi menampilkan data
public void viewAll() {
btnViewAll.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick(View v) {
Cursor res = myDb.getAllData();
if(res.getCount() == 0) {
// show message
showMessage("Error","Noting Found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext() ) {
buffer.append("NomorRumah :"+ res.getString(0)+"\n");
buffer.append("JentikDalam :"+ res.getString(1)+"\n");
buffer.append("JentikLuar :"+ res.getString(2)+"\n");
}
// show all data
showMessage("Data",buffer.toString());
}
}
);
}
//membuat alert dialog
public void showMessage(String title, String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
}
and here is the Input.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#drawable/gradient"
android:orientation="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="70dp"
android:background="#drawable/gradient"
android:elevation="4dp"
android:orientation="vertical"
android:padding="20dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="30dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nomor Rumah" />
<EditText
android:id="#+id/nomorrmh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/roundtext" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jentik Di Dalam Rumah" />
<EditText
android:id="#+id/jentikdirumah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/roundtext" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jentik Di Luar Rumah" />
<EditText
android:id="#+id/jentikdiluarrumah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/roundtext"
android:layout_marginBottom="10dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_gravity="center">
<Button
android:id="#+id/tambahdata"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="#d67601"
android:text="Tambah Data"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="18sp" />
<Button
android:id="#+id/lihatdata"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="#d67601"
android:text="contoh lihat data"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<TextView
android:id="#+id/textviewadd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
android:gravity="center_horizontal"
android:text="Input Data"
android:textColor="#fff"
android:textSize="26sp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/text_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
My goal is to pass the fragment into the java class that i want, because i dont know how to put that input class to the fragment always get cannot be cast to androidx.fragment.app.Fragment when i replace the GalleryFragment with pantauan.java code. thank you

MvvmCross Databinding Not working as expected

I am currently experiencing a weird issue with MvvmCross on android. I want to data bind a a view to a ViewModel. My setup is as follows
public class ForumTopicItemsViewModel : MvxViewModel
{
ObservableCollection<ForumReplyListModel> _forumComments;
ForumTopicDetailsModel _topic;
long _forumId;
long _topicId;
bool _busy;
int _page;
public ForumTopicItemsViewModel()
{
_topic = new ForumTopicDetailsModel()
{
DateCreated = DateTime.Now.AddDays(-1),
Discussion = "Just the fact that I dont care about what is displayed here",
ForumId = ForumId,
TopicId = TopicId,
Topic = "Another Heading, Just for the sake of it"
};
_forumComments = new ObservableCollection<ForumReplyListModel>
{
new ForumReplyListModel()
{
CreatedBy = "Peter Edike",
DateCreated = DateTime.Now.AddDays(-1),
Reply = "Just a reply to show how far I have come"
}
};
}
public int Page
{
get { return _page; }
set { SetProperty(ref _page, value); }
}
private static bool IsHostReachable()
{
var nReachability = Mvx.Resolve<IMvxReachability>();
return nReachability.IsHostReachable(AppConfiguration.RequestUrl);
}
public ObservableCollection<ForumReplyListModel> Comments
{
get { return _forumComments; }
set { SetProperty(ref _forumComments, value); }
}
public ForumTopicDetailsModel Topic
{
get { return _topic; }
set { SetProperty(ref _topic, value); }
}
public string Heading => this.Topic.Topic;
public string Description => this.Topic.Discussion;
public string DateCreatedString => this.Topic.DateCreatedString;
public bool Busy
{
get { return _busy; }
set { SetProperty(ref _busy, value); }
}
public long ForumId
{
get { return _forumId; }
set {SetProperty(ref _forumId, value); }
}
public long TopicId
{
get { return _topicId; }
set { SetProperty(ref _topicId, value); }
}
public void Init(long TopicId, long ForumId)
{
this.TopicId = TopicId;
this.ForumId = ForumId;
}
}
and I have the Android XML View Like So
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:MvxBind="Refreshing Busy; RefreshCommand LoadReplyCommand">
<MvxRecyclerView
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:MvxItemTemplate="#layout/listitem_commentlist"
app:MvxBind="ItemsSource Comments;" />
</MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout>
<View
android:layout_width="match_parent"
android:layout_height="4px"
android:background="#color/grey_soft" />
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardCornerRadius="#dimen/spacing_medium"
app:cardElevation="#dimen/spacing_xsmall"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/spacing_medium"
android:layout_marginLeft="#dimen/spacing_large"
android:layout_marginRight="#dimen/spacing_large"
android:layout_marginTop="#dimen/spacing_large"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textColor="#color/upload_photo_button_color"
app:MvxBind="Text Heading" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/spacing_large"
android:text="#string/lorem_ipsum"
app:MvxBind="Text Description"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/grey_dark" />
<View
android:layout_width="match_parent"
android:layout_height="2px"
android:background="#color/grey_soft" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/spacing_large"
app:MvxBind="Text DateCreatedString"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/upload_photo_button_color"
android:textStyle="italic" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Problem:
The contents of the MvxRecyclerView are never displayed even though i made sure that the collection had items in the constructor.
Every other binding works as expected. If I however comment out everything except the MvxRecyclerView, it becomes visible.
Please I think I did something wrong.
Can someone point me in the right direction?
Thank You.
None of your LinearLayout's seem to have an orientation set. I think the default one is set to Horizontal meaning your layout flows from left to right and go outside of the visible area of your screen.
Also another problem is that your layout is awfully nested, meaning it will probably have a significant amount of overdraw and extra measurement passes when it is going to be drawn on the screen. You can un-nest it by using a single RelativeLayout instead of the 4 nested layers of LinearLayout.
Something like this might work:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout
android:layout_alignParentTop="true"
android:layout_above="#+id/divider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:MvxBind="Refreshing Busy; RefreshCommand LoadReplyCommand">
<MvxRecyclerView
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:MvxItemTemplate="#layout/listitem_commentlist"
app:MvxBind="ItemsSource Comments" />
</MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout>
<View
android:id="#+id/divider"
android:layout_above="#+id/heading_card"
android:layout_width="match_parent"
android:layout_height="4px"
android:background="#color/grey_soft" />
<android.support.v7.widget.CardView
android:id="#+id/heading_card"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardCornerRadius="#dimen/spacing_medium"
app:cardElevation="#dimen/spacing_xsmall"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/spacing_medium"
android:layout_marginLeft="#dimen/spacing_large"
android:layout_marginRight="#dimen/spacing_large"
android:layout_marginTop="#dimen/spacing_large"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textColor="#color/upload_photo_button_color"
app:MvxBind="Text Heading" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/spacing_large"
android:text="#string/lorem_ipsum"
app:MvxBind="Text Description"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/grey_dark" />
<View
android:layout_width="match_parent"
android:layout_height="2px"
android:background="#color/grey_soft" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/spacing_large"
app:MvxBind="Text DateCreatedString"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/upload_photo_button_color"
android:textStyle="italic" />
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>

How to Display New Item on MvxListView on Button Click in Android Xamarin Visual Studio

I want to display a new line of TextView everytime I click a button. I am using MvvmCross and I know that I should use MvxListView which is assigned to a layout template, but I do not know how to display it on screen on button click.
Can someone give me a simple code example of doing this from ViewModel and very short explanation about which line of code that initiate the display of new TextView?
UPDATE
I provide my code below:
ViewModel.cs
public class FirstViewModel
: MvxViewModel
{
private ObservableCollection<Unit> unitCodes;
public ObservableCollection<Unit> UnitCodes
{
get { return unitCodes; }
set { unitCodes = value; RaisePropertyChanged(() => UnitCodes); }
}
public IMvxCommand ButtonCommand { get; private set; }
public FirstViewModel()
{
unitCodes = new ObservableCollection<Unit>();
ButtonCommand = new MvxCommand(() =>
{
UnitCodes = UnitCodes ?? new ObservableCollection<Unit>();
UnitCodes.Add(new Unit("123", "Test Name"));
});
}
public class Unit : MvxViewModel
{
private string unitCode;
public string UnitCode
{
get { return unitCode; }
set { unitCode = value; RaisePropertyChanged(() => UnitCode); }
}
private string unitName;
public string UnitName
{
get { return unitName; }
set { unitName = value; RaisePropertyChanged(() => UnitName); }
}
public Unit() { }
public Unit(string unitCode, string unitName)
{
UnitCode = unitCode;
UnitName = unitName;
}
}
}
View.axml
<?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">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
local:MvxBind="Click ButtonCommand"
android:text="Click To Add" />
<Mvx.MvxListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
local:MvxItemTemplate="#layout/unitcodeitemlayout"
local:MvxBind="ItemSource UnitCodes" />
</LinearLayout>
unitcodeitemlayout.axml
<?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="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
local:MvxBind="Text UnitName" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
local:MvxBind="Text UnitCode" />
</LinearLayout>
My code does not have any compilation or run-time error, however there is nothing happening on button click. What I want is to add "123" and "Test Name" to the display every time I click the button.
Your code is right. However, you misspelled the ItemsSource under MvxListView axml.
It should be:
<Mvx.MvxListView
...
local:MvxBind = "ItemsSource UnitCodes"/>
In order to set the ListView, take a look at this MvvmCross tutorial
You could end up with something like this:
ViewModel:
public class MyListViewModel : MvxViewModel
{
private ObservableCollection<TextViewCellViewModel> _listItems;
public virtual ObservableCollection<TextViewCellViewModel> ListItems {
get {
return _listItems;
}
set {
_listItems = value;
RaisePropertyChanged(() => ListItems);
}
}
private IMvxCommand _addTextViewCommand;
public IMvxCommand AddTextViewCommand {
get {
_addTextViewCommand = _addTextViewCommand ?? new MvxCommand(AddTextView);
return _addTextViewCommand;
}
}
private void AddTextView()
{
ListItems = ListItems ?? new ObservableCollection<TextViewCellViewModel>();
ListItems.Add(new TextViewCellViewModel());
}
public class TextViewCellViewModel : MvxViewModel
{
private string _text;
public string Text { get { return _text; } set { _text = value; RaisePropertyChanged(() => Text); } }
public TextViewCellViewModel() { }
}
}
Android ListView Layout:
<?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"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="fill_vertical"
android:paddingRight="20dp"
android:paddingLeft="20dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/frameLayout"
android:paddingBottom="50dp">
<MvxListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:id="#+id/list_view_tripplanner_result"
android:divider="#android:color/transparent"
android:paddingTop="10dp"
android:clipToPadding="false"
android:dividerHeight="7dp"
android:descendantFocusability="afterDescendants"
local:MvxItemTemplate="#layout/cell_textview"
local:MvxBind="ItemsSource ListItems;" />
</FrameLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingTop="5dp"
android:paddingBottom="10dp">
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
local:MvxBind="Click AddTextViewCommand"
android:text="Add TextView"
android:textColor="#color/primary_white"
android:background="#android:color/darker_gray" />
</LinearLayout>
</RelativeLayout>
Which should look like this: (notice button on the bottom of the Listview)
Where #layout/cell_textview would look something like this:
layout for the ItemTemplate for the ListView would be:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:textSize="20dp"
local:MvxBind="Text Text"/>
</LinearLayout>

TextView Clickable in MVVMCross

I have floating button in the FundamentalView.axml
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="22dp"
android:paddingRight="22dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true">
<ImageButton
android:id="#+id/addButton"
android:layout_width="56dp"
android:layout_height="56dp"
android:src="#drawable/ic_add_white_24dp" />
</FrameLayout>
In the FundamentalView.cs, I have click event which triggers a fragment from the bottom of the view with having options (adding a new person and new calculations).
var addButton = view.FindViewById<ImageButton>(Resource.Id.addButton);
addButton.Click += OnAddButtonClick;
void OnAddButtonClick(object sender, System.EventArgs e)
{
var dialog = new CardDialogView();
dialog.Show(((MainView)Activity).SupportFragmentManager, "CardDialogView");
}
CardDialogView.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/dash_add_computer"
android:textColor="#color/primary_text"
android:textSize="16sp"
android:text="New Calculation"
local:MvxBind="Click NewCalculationCommand"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/dash_add_head"
android:drawablePadding="28dp"
android:textColor="#color/primary_text"
android:textSize="16sp"
android:text="New Person" />
</LinearLayout>
My question is how to make TextView clickable and know which textview clicked in mvvmcross?
CardDialogView.cs
public class CardDialogView : MvxDialogFragment<CardDialogViewModel>
{
public override Dialog OnCreateDialog(Bundle savedState)
{
.....
return dialog;
}
}
CardDialogViewModel.cs
public class CardDialogViewModel : MvxViewModel
{
public ICommand NewCalculationCommand
{
get
{
// it does not come here!
return new MvxCommand(() => ShowViewModel<NewItemViewModel>(new { date = DateTime.Now }));
}
}
}
You can bind textview click just like as button click:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/dash_add_computer"
android:textColor="#color/primary_text"
android:textSize="16sp"
local:MvxBind="Click NewCalculationCommand"
android:text="New Calculation" />
For the second textview bind it to another command. This way when the corresponding command is invoked you will know which textview triggered it.

Categories

Resources