Samsung / Tizen.CircularUI

Tizen Wearable CircularUI project is to develop an open source software motivate software developer to creating Tizen Wearable Xamarin Forms app more easily and efficiently.
Other
80 stars 32 forks source link

Obtain current focused item of CircleListView when scrolling list #339

Closed axa88 closed 3 years ago

axa88 commented 4 years ago

Describe the bug

It doesn't appear possible to get the currently focused item in a CircleListView. When rotating the bezel, or scrolling the list, i would like to know which item has been scrolled to.

Example here I would like to obtain the index of this list item that comes into focus: image

Item is not selected or tapped so ItemTapped or ItemSelected events are irrelevant. Focused event only is relevant to the Entire ListView not individual items. Scrolled, ItemAppearing, ItemDisappearing event args do not provide information of the focused item.

To Reproduce Steps to reproduce the behavior:

  1. Populate a CircleListView
  2. scroll the list by any means
  3. try to obtain the index when the focused list item changes

Expected behavior Be able to obtain the index of the focused item of a CircleListView Perhaps a property FocusedItem could be added.

Environment (please complete the following information):

Target Product: image Tizen Platform Version [e.g. Tizen 5.5] Tizen.CircularUI Version [1.4.0 > 1.5.0]

Additional context Perhaps im ignorant, but i do not see a way to do this.

Machinero commented 4 years ago

Hello @axa88 ,

I had the same problem but, I have found a workaround for that. The native control provides us with the GetItemByPosition method, which returns a list item on a given position on the screen. Thanks to that method, I can obtain the middle item on the screen immediately after the ScrollAnimationStopped event has been raised.

Workaround:

CustomControl

namespace FocusItemListView.Controls
{
    public class MyCustomListView : CircleListView
    {
        public event EventHandler<int> ItemHighlighted;

        public void NotifyHighlightedItem(object item, int itemIndex)
        {
            ItemHighlighted?.Invoke(item, itemIndex);
        }
    }
}

CustomRenderer

[assembly: ExportRenderer(typeof(MyCustomListView), typeof(MyCustomListViewRenderer))]
namespace FocusItemListView.Renderers
{
    public class MyCustomListViewRenderer : CircleListViewRenderer
    {
        new MyCustomListView Element => base.Element as MyCustomListView;

        protected override void OnElementReady()
        {
            base.OnElementReady();
            Control.ScrollAnimationStopped += OnScrollAnimationStopped;
        }

        private void OnScrollAnimationStopped(object sender, EventArgs arrgs)
        {
            GenListItem item = Control.GetItemByPosition(180, 180, out int pos);
            if (item.Data is NListView.ItemContext itemContext && pos == 0)
            {
                var obj = itemContext.Cell.BindingContext;
                var index = Element.TemplatedItems.GetGlobalIndexOfItem(obj);
                Log.Debug("TEST", $"Item index: {index}");
                Element.NotifyHighlightedItem(obj, index);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && Control != null)
            {
                Control.ScrollAnimationStopped -= OnScrollAnimationStopped;
            }
            base.Dispose(disposing);
        }
    }
}
myroot commented 4 years ago

We does not provide API to get current focused item, but as workaround commented on above, you can get it

axa88 commented 4 years ago

@myroot understanding this is a workaround, but couldn't this issue be kept open as enhancement? i think this workaround is a bit much for rapid application development, and get focus item is worthy addition for CircularUI API.

rookiejava commented 4 years ago

@axa88 Thank you for your suggestion. This is the same one I suggested to Xamarin.Forms two years ago. https://github.com/xamarin/Xamarin.Forms/issues/2181

We'll discuss internally whether to add this to CircularUI by itself. Thanks!

ghost commented 3 years ago

Is there any progress on this?

hacqua commented 3 years ago

Hi @Machinero. Thanks for the workaround provided, however I cannot get the following line from the custom renderer to compile:

if (item.Data is NListView.ItemContext itemContext && pos == 0)

"NListView" is not being recognized, is that correct?

rookiejava commented 3 years ago

Thank you for waiting so long. 🙏 I've just published a new version (1.5.3) to nuget that support this.. (See also release note )

Plus, thanks to @Machinero for providing great idea. ❤️