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 { else {
if (c == ' ') { if (c == ' ') {
if (i >= lineStartIndex && text[i-1] != ' ') if (i >= lineStartIndex && i > 0 && text[i-1] != ' ')
firstSpaceX = px; firstSpaceX = px;
lastSpaceIndex = i; lastSpaceIndex = i;
} }

View File

@ -27,6 +27,7 @@ namespace Ooui.Forms.Renderers
{ {
var templatedItems = TemplatedItemsView.TemplatedItems; var templatedItems = TemplatedItemsView.TemplatedItems;
templatedItems.CollectionChanged -= OnCollectionChanged; templatedItems.CollectionChanged -= OnCollectionChanged;
e.OldElement.ScrollToRequested -= ListView_ScrollToRequested;
} }
if (e.NewElement != null) if (e.NewElement != null)
@ -41,6 +42,7 @@ namespace Ooui.Forms.Renderers
var templatedItems = TemplatedItemsView.TemplatedItems; var templatedItems = TemplatedItemsView.TemplatedItems;
templatedItems.CollectionChanged += OnCollectionChanged; templatedItems.CollectionChanged += OnCollectionChanged;
e.NewElement.ScrollToRequested += ListView_ScrollToRequested;
UpdateItems (); UpdateItems ();
UpdateBackgroundColor(); UpdateBackgroundColor();
@ -70,6 +72,7 @@ namespace Ooui.Forms.Renderers
{ {
var templatedItems = TemplatedItemsView.TemplatedItems; var templatedItems = TemplatedItemsView.TemplatedItems;
templatedItems.CollectionChanged -= OnCollectionChanged; templatedItems.CollectionChanged -= OnCollectionChanged;
Element.ScrollToRequested -= ListView_ScrollToRequested;
} }
_disposed = true; _disposed = true;
@ -139,6 +142,24 @@ namespace Ooui.Forms.Renderers
Element.NotifyRowTapped(ndx, null); 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() private void UpdateBackgroundColor()
{ {
var backgroundColor = Element.BackgroundColor.ToOouiColor(); var backgroundColor = Element.BackgroundColor.ToOouiColor();

View File

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

View File

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

View File

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

View File

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