RecyclerView ItemAnimator Conversionof Wasabeef?
A discussion of converting Recycler ItemAnimators from Wasabeef. Who knows, I may volunteer to help with the cause.
https://gitter.im/wasabeef/recyclerview-animators/archives/2015/01/08
A discussion of converting Recycler ItemAnimators from Wasabeef. Who knows, I may volunteer to help with the cause.
https://gitter.im/wasabeef/recyclerview-animators/archives/2015/01/08
With the latest updates to the Android Support v7 AppCompat Library, revision 22.1, integrating Google’s Material Design has never been easier. If you need to get caught up with AppCompat, we have already covered how to add Material Theming to your apps with AppCompat and how to replace your ActionBar with the new advanced Toolbar […]
The post Android Tips: Hello AppCompatActivity, Goodbye ActionBarActivity appeared first on Xamarin Blog.
Android developers have been flocking to Material Design since its introduction with the release of Android Lollipop. With the recent update to the Support v7 AppCompat library it has never been easier to add Material Design to target older Android operating systems. Material Design is much more than just the core theming and styling that […]
The post More Material Design for Your Android Apps appeared first on Xamarin Blog.
Since Xamarin Forms Listview now reuses cells in iOS (as it should) one small piece was overlooked. The reused cells now displays previously displayed images for a short while until the new image is downloaded.
This behavior is mitigated by a cache, so once an image is loaded it’s set instantly the next time the cell is displayed.
I’ve also filed a bug with a sample project available at https://bugzilla.xamarin.com/show_bug.cgi?id=28628 and the sample project to visualize the issue at here.
There are a number of ways around this.
namespace ImageListView.iOS.Renderers
{
public class CustomImageRenderer : ImageRenderer
{
public CustomImageRenderer()
{
}
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if ((sender as Image).IsLoading)
{
// The image will be reset in base if there is one already in the cache.
// If it isn‘t already loaded we don‘t want to display the previous image
// This is however called each time a cell comes into view so it can‘t
// be used for opacity fading or likewise...
// This should be handled by the code in the ImageRenderer
Control.Image = null;
}
base.OnElementPropertyChanged(sender, e);
}
}
}
Wrap your image in a custom control and handle all the loading and caching yourself.
Write your own Image-renderer from scratch to take into account when an image is fetched from the cache or not to enable a nice fade in. This is what I hope will come out of the bug report.
The solution to this is simple, just hook up to the ItemSelected event for the ListView and set the SelectedItem property to null.
So the short version of you quick-googlers would be the following line of code. (in the View)
// Reset the selected item to make it selectable again
duckListView.ItemSelected += (s, e) => {
duckListView.SelectedItem = null;
And the navigation should be done in the ViewModel
public Duck SelectedDuck
{
set
{
if (value != null)
{
// IoC omitted, we should really get someone else to
// create these objects for us.
var viewModel = new DuckViewModel() { Duck = value };
var page = new DuckView(viewModel);
_navigation.PushAsync(page);
}
}
}
You could also navigate directly from this event handler, but you should feel it deep in your heart that that is just wrong. Instead we handle navigation in the ViewModel. I’ve created a sample project to do this. Also, I’m doing this without any additional framework that would handle navigation for you so that’s why I need to provide my ViewModel with a navigation interface.
I’ll present each file to you below or just download the sample solution from here.
We’ve got two ViewModels, but it’s really only the MainViewModel that’s interesting. It initializes it’s own data, which usually should be done async from another source. It doesn’t implement INotifyPropertyChanged either, as is should but for this sample it’s good enough.
What to focus on is the SelectedDuck property that handles the navigation. We only implement a setter for this since we reset the selected item anyhow in the view itself and on top of that navigate away from the page.
/// <summary>
/// The sample ViewModel. Should implement INotifyPropertyChanged
/// </summary>
public class MainViewModel
{
private INavigation _navigation;
public MainViewModel(INavigation navigation)
{
_navigation = navigation;
Ducks = new List<Duck>()
{
new Duck() { Name = “George“ },
new Duck() { Name = “Bob“ },
new Duck() { Name = “Sarah“ },
new Duck() { Name = “Clint“ },
};
}
/// <summary>
/// A list of ducks
/// </summary>
/// <value>The ducks.</value>
public List<Duck> Ducks
{
get;
set;
}
public Duck SelectedDuck
{
set
{
if (value != null)
{
// IoC omitted, we should really get someone else to
// create these objects for us.
var viewModel = new DuckViewModel() { Duck = value };
var page = new DuckView(viewModel);
_navigation.PushAsync(page);
}
}
}
}
The other ViewModel (DuckViewModel) simply references the selected duck on the page we navigate to.
public class DuckViewModel
{
public DuckViewModel()
{
}
public Duck Duck
{
get;
set;
}
}
// Reset the selected item to make it selectable again
duckListView.ItemSelected += (s, e) => {
duckListView.SelectedItem = null;
};
}
}
<ListView x:Name=“duckListView“
IsGroupingEnabled=“false“
ItemsSource=“{Binding Ducks}“
HasUnevenRows=“true“
SelectedItem=“{Binding SelectedDuck}“>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Font=“Large“ Text=“{Binding Name}“ />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>
Please give feedback what ever you feel like! And if there’s a better way, I would love for you to enlighten me! 😀