Add some ScrollTo support to ListView

This commit is contained in:
Frank A. Krueger 2018-04-15 22:55:23 -07:00
parent 42c1c113e4
commit 79e4e87b95
No known key found for this signature in database
GPG Key ID: 0471C67474FFE664
6 changed files with 53 additions and 3 deletions

View File

@ -77,7 +77,7 @@ namespace Ooui.Forms.Extensions
}
else {
if (c == ' ') {
if (i >= lineStartIndex && text[i-1] != ' ')
if (i >= lineStartIndex && i > 0 && text[i-1] != ' ')
firstSpaceX = px;
lastSpaceIndex = i;
}

View File

@ -27,6 +27,7 @@ namespace Ooui.Forms.Renderers
{
var templatedItems = TemplatedItemsView.TemplatedItems;
templatedItems.CollectionChanged -= OnCollectionChanged;
e.OldElement.ScrollToRequested -= ListView_ScrollToRequested;
}
if (e.NewElement != null)
@ -41,6 +42,7 @@ namespace Ooui.Forms.Renderers
var templatedItems = TemplatedItemsView.TemplatedItems;
templatedItems.CollectionChanged += OnCollectionChanged;
e.NewElement.ScrollToRequested += ListView_ScrollToRequested;
UpdateItems ();
UpdateBackgroundColor();
@ -70,6 +72,7 @@ namespace Ooui.Forms.Renderers
{
var templatedItems = TemplatedItemsView.TemplatedItems;
templatedItems.CollectionChanged -= OnCollectionChanged;
Element.ScrollToRequested -= ListView_ScrollToRequested;
}
_disposed = true;
@ -139,6 +142,24 @@ namespace Ooui.Forms.Renderers
Element.NotifyRowTapped(ndx, null);
}
void ListView_ScrollToRequested (object sender, ScrollToRequestedEventArgs e)
{
if (Control == null)
return;
var oe = (ITemplatedItemsListScrollToRequestedEventArgs)e;
var item = oe.Item;
var group = oe.Group;
switch (e.Position) {
case ScrollToPosition.Start:
Control.Send (Ooui.Message.Set (Control.Id, "scrollTop", 0));
break;
case ScrollToPosition.End:
Control.Send (Ooui.Message.Set (Control.Id, "scrollTop", new Ooui.Message.PropertyReference { TargetId = Control.Id, Key = "scrollHeight" }));
break;
}
}
private void UpdateBackgroundColor()
{
var backgroundColor = Element.BackgroundColor.ToOouiColor();

View File

@ -295,6 +295,7 @@ function processMessage (m) {
}
function fixupValue (v) {
var x, n;
if (Array.isArray (v)) {
for (x in v) {
v[x] = fixupValue (v[x]);
@ -307,6 +308,9 @@ function fixupValue (v) {
return getNode (v);
}
}
else if (!!v && v.hasOwnProperty("id") && v.hasOwnProperty("k")) {
return fixupValue(v["id"])[v["k"]];
}
return v;
}

View File

@ -29,6 +29,13 @@ namespace Ooui
Value = args,
};
public static Message Set (string targetId, string property, object value) => new Message {
MessageType = MessageType.Set,
TargetId = targetId,
Key = property,
Value = value,
};
public static Message Event (string targetId, string eventType, object value = null) => new Message {
MessageType = MessageType.Event,
TargetId = targetId,
@ -36,6 +43,15 @@ namespace Ooui
Value = value,
};
public class PropertyReference
{
[JsonProperty ("id")]
public string TargetId = "";
[JsonProperty ("k")]
public string Key = "";
}
public void WriteJson (System.IO.TextWriter w)
{
w.Write ('{');

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
@ -6,6 +6,7 @@
<ContentPage.Content>
<StackLayout>
<ListView
x:Name="list"
ItemsSource="{Binding Data}"
HeightRequest="200">
<ListView.ItemTemplate>
@ -22,7 +23,8 @@
Text="{Binding Input}" />
<Button
Text="Add Item"
Command="{Binding AddCmd}" />
Command="{Binding AddCmd}"
Clicked="Handle_Clicked"/>
</StackLayout>
</StackLayout>
</ContentPage.Content>

View File

@ -1,5 +1,6 @@
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Linq;
namespace Samples
{
@ -12,5 +13,11 @@ namespace Samples
BindingContext = new RefreshListViewModel ();
}
void Handle_Clicked (object sender, System.EventArgs e)
{
string item = ((RefreshListViewModel)BindingContext).Data.LastOrDefault ();
list.ScrollTo (item, ScrollToPosition.End, true);
}
}
}