How to set visibility for ProgressBar in android M

2020-05-04 03:05发布

问题:

I am trying to set visibility for ProgressBar as GONE.


In XML

<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    local:MvxBind="Visibility Visibility(ProgressVisibility)"
    android:background="@drawable/sel_custom_progress" />

In ViewModel

private bool _progressVisibility;
public bool ProgressVisibility
{
    get { return _progressVisibility; }
    set { _progressVisibility = value; RaisePropertyChanged(() => ProgressVisibility); }
}

I am setting ProgressVisibility = false;


What is happening: The view is still visible and not getting hidden. How to resolve this.

回答1:

You need to create a custom value converter for Visibility or you can use Visibility plugin

public class MyVisibilityValueConverter : MvxBaseVisibilityValueConverter<bool>
{
    protected override MvxVisibility Convert(string value, object parameter, CultureInfo culture)
    {
        return (value ==true) ? MvxVisibility.Visible : MvxVisibility.Collapsed;
    }
}

xml:

local:MvxBind="Visibility ProgressVisibility,Converter=MyVisibility

More info here