简体   繁体   English

Xamarin表单通过点击选项卡式页面顶部栏滚动到选项卡式页面内部的listView的顶部

[英]Xamarin forms scroll to top of listView which is inside a tabbed page by tapping Tabbed Page Top Bar

Current Situation 现在的情况

I have made a xamarin forms cross-platform app which has a tabbed Page with an infinite scroll listView. 我制作了一个xamarin表单跨平台应用程序,该应用程序具有带有无限滚动listView的选项卡式页面。 The listView is populated in increments of 10 items whenever the last item appears. 每当出现最后一项时,listView就会以10个项的增量进行填充。

The normal behaviour 正常行为

Usually when we tap the top bar of the tabbed page (the bar containing the tabbed page's Title) for the first time (if we are not viewing the tab at that moment), the app will switch to view that page. 通常,当我们第一次点击选项卡式页面的顶部栏(包含选项卡式页面标题的栏)时(如果我们当时不在查看选项卡),应用程序将切换为查看该页面。 Which is fine. 没关系

When we tap the top bar for the second time (when tapping the top bar of current viewing tabbed page), nothing happens (unlike twitter, u can tap to scroll to the top ) 当我们第二次点击顶部栏时(当点击当前查看选项卡式页面的顶部栏时),则没有任何反应(与twitter不同,您可以点击以滚动到顶部)

My Problem 我的问题

I want to be able to make the listView to scroll back to the top (to the first Item on the list) when the top bar is tapped (something like how twitter does in its android app) while still maintaining all the already loaded items in my listView. 我希望能够在点击顶部栏(类似于twitter在其android应用中的操作方式)的同时使listView滚动回到顶部(列表中的第一个项目),同时仍保留所有已加载项我的listView。

Any idea on how to make that top bar react to the tap to scroll the listView back to the top? 关于如何使顶部栏对水龙头做出反应的任何想法,以便将listView滚动回到顶部?

You should try something like this for scrolling up 您应该尝试像这样向上滚动

ListViewName.ScrollTo(yourobject,ScrollToPosition.Start, true);
// true means boolean for animated.

I hope this help you 希望对您有帮助

The TabbedPage exposes a CurrentPageChanged event (or you can override OnCurrentPageChanged) that fires only when the selected tab changes, so you'll need to subscribe to that if you want to scroll when the user changes tabs. TabbedPage公开了一个CurrentPageChanged事件(或者您可以覆盖OnCurrentPageChanged),该事件仅在选定的选项卡更改时才触发,因此如果要在用户更改选项卡时进行滚动,则需要订阅该事件。 It doesn't seem like this is needed, but just in case... 看来这不是必需的,但以防万一...

Xamarin.Forms doesn't have a built-in event that fires when the already-selected tab is selected a second time. Xamarin.Forms没有内置事件,当第二次选择已选择的选项卡时会触发该事件。 You can make that happen with custom renderers though, by subclassing Xamarin's TabbedPage renderers. 通过子类化Xamarin的TabbedPage渲染器,您可以使用自定义渲染器来实现。 I wrote it up here and have a working Xamarin.Forms solution for iOS, Android, and UWP here . 我写了这里 ,并有适用于iOS,Android和UWP工作Xamarin.Forms解决方案在这里

You don't mention the platforms, so assuming you want iOS and Android, the core bits are below. 您没有提到平台,因此假设您要使用iOS和Android,以下是核心内容。

iOS custom renderer iOS自定义渲染器

public class MainTabPageRenderer : TabbedRenderer
{
    private UIKit.UITabBarItem _prevItem;

    public override void ViewDidAppear(bool animated)
    {
        base.ViewDidAppear(animated);

        if (SelectedIndex < TabBar.Items.Length)
            _prevItem = TabBar.Items[SelectedIndex];
    }

    public override void ItemSelected(UIKit.UITabBar tabbar,
                                      UIKit.UITabBarItem item)
    {
        if (_prevItem == item && Element is MainPage)
        {
            // the same tab was selected a second time, so do something
        }
        _prevItem = item;
    }
}

Android custom renderer Android自定义渲染器

public class MainTabPageRenderer : TabbedPageRenderer, TabLayout.IOnTabSelectedListener
{
    void TabLayout.IOnTabSelectedListener.OnTabReselected(TabLayout.Tab tab)
    {
        if (Element is MainPage)
        {
            // the same tab was selected a second time, so do something
        }
    }
}

Once you have captured that event, use the ListView.ScrollTo method to scroll to the top. 捕获到该事件后,请使用ListView.ScrollTo方法滚动到顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM