首页 文章

Xamarin表格可折叠StackLayout

提问于
浏览
1

我正在尝试实现一种可折叠的StackLayout . 用户单击按钮的每个时间点,它会展开或折叠stacklayout以显示/隐藏更多详细信息 .

enter image description here

我能够通过下面的代码实现更多/更少的这个,但它看起来不正确并且效果不是很好,因为它会立即增长并且我将效果应用于其他元素 .

你对此有什么建议,我使用的是xamarin Forms吗?

XAML

<?xml version="1.0" encoding="utf-8" ?>
<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Sample.MyStackLayout" >

  <StackLayout x:Name="TopLayout">
    <StackLayout Orientation="Horizontal">
      <Label Text="some text" VerticalOptions="Center" HorizontalOptions="StartAndExpand"  />
      <Label Text="123" VerticalOptions="Center" HorizontalOptions="End" FontSize="Large" />
    </StackLayout>

    <BoxView Color="Black" HeightRequest="1" />

    <StackLayout Orientation="Horizontal">
      <Label Text="some text" VerticalOptions="Center" HorizontalOptions="StartAndExpand"  />
      <Label Text="123" VerticalOptions="Center" HorizontalOptions="End" FontSize="Large" />
    </StackLayout>

    <StackLayout Orientation="Horizontal">
      <Label Text="some text" VerticalOptions="Center" HorizontalOptions="StartAndExpand"  />
      <Label Text="123" VerticalOptions="Center" HorizontalOptions="End" FontSize="Large" />
    </StackLayout>

    <Button x:Name="btn" Text="Button" Clicked="btnClicked" />
  </StackLayout>


  <StackLayout x:Name="MoreDetails" IsVisible="False">
    <Label Text="some text 1"></Label>
    <Label Text="some text 2"></Label>
    <Label Text="some text 3"></Label>
    <Label Text="some text 4"></Label>
    <Label Text="some text 5"></Label>
    <Label Text="some text 6"></Label>
    <Label Text="some text 7"></Label>
    <Label Text="some text 8"></Label>
  </StackLayout>
</StackLayout>

Code

public AccountInfo()
{
    InitializeComponent();
}

bool isExpanded = false;
protected async void btnClicked(object sender, EventArgs e)
{
    if (isExpanded)
    {
        await MoreDetails.FadeTo(0);
        MoreDetails.IsVisible = !isExpanded;
    }
    else
    {
        MoreDetails.IsVisible = !isExpanded;
        await MoreDetails.FadeTo(1);
    }

    isExpanded = !isExpanded;
}

2 回答

  • 0

    您可以创建一个自定义控件来执行此操作 . 如果您使用Xaml创建“ExpandableView”内容视图,例如:

    <ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MyProject.CustomControls.ExpandableView">
        <StackLayout  x:Name="Layout" Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <StackLayout x:Name="SummaryRegion">               
            <StackLayout x:Name="DetailsRegion"/>
        </StackLayout>
    </ContentView>
    

    并像这样连接.cs类:

    public partial class ExpandableView: ContentView
        {
    
            private TapGestureRecognizer _tapRecogniser;
            private StackLayout _summary;
            private StackLayout _details;
    
            public ExpandableView()
            {
                InitializeComponent();
                InitializeGuestureRecognizer();
                SubscribeToGuestureHandler();    
            }
    
            private void InitializeGuestureRecognizer()
            {
                _tapRecogniser= new TapGestureRecognizer();
                SummaryRegion.GestureRecognizers.Add(_tapRecogniser);
            }
    
            private void SubscribeToGuestureHandler()
            {
                _tapRecogniser.Tapped += TapRecogniser_Tapped;
            }
    
            public virtual StackLayout Summary
            {
                get { return _summary; }
                set
                {
                    _summary = value;    
                    SummaryRegion.Children.Add(_summary);
                    OnPropertyChanged();
                }
            }
    
            public virtual StackLayout Details
            {
               get { return _details; }
               set 
               {
                  _details = value;
                  DetailsRegion.Children.Add(_details);
                  OnPropertyChanged();
               }
           }
    
           private void TapRecogniser_Tapped(object sender, EventArgs e)
        {
            if (DetailsRegion.IsVisible)
            {
                DetailsRegion.IsVisible = false;
            }
            else
            {
                 DetailsRegion.IsVisible = true;
            }
        }
    

    并在你的xaml中定义它,如下所示:

    <CustomControls:ExpandableView>
                                <CustomControls:ExpandableView.Summary>
                                       <StackLayout>
                                        YOUR STUFF HERE 
                                    </StackLayout>
                                </CustomControls:ExpandableView.Summary>
                                <CustomControls:ExpandableView.Details>
                                    <StackLayout>
                                        YOUR STUFF HERE 
                                    </StackLayout>
                                </CustomControls:ExpandableView.Details>
                            </CustomControls:ExpandableView>
    

    其中CustomControls是对ExpandableView存在的命名空间的引用 .

    您可以通过在展开时添加动画等内容来进一步扩展,在展开时突出显示“摘要区域”等...

  • 4

    将您的所有内容包装到MoreDetails到另一个堆栈布局并将其命名为“TopLayout”

    void ShowMore(){
        TopLayout.TranslateTo(0, -TopLayout.Bounds.Height, 300,  Easing.Linear);
        MoreDetails.LayoutTo(new Rectangle(0, 0, MoreDetails.Bounds.Width, MoreDetails.Bounds.Height + TopLayout.Bounds.Height), 300, Easing.Linear);
        }
    
        void ShowLess(){
        TopLayout.TranslateTo(0, 0, 300,  Easing.Linear);
        MoreDetails.LayoutTo(new Rectangle(0, MoreDetails.Bounds.Height, MoreDetails.Bounds.Width, MoreDetails.Bounds.Height - MoreDetails.Bounds.Height), 300, Easing.Linear);
        }
    

    100 - 这是你的位移值

    作为奖励:

    MoreLessImage.RotateXTo(180, Duration, TargetEasing);
    

    您可以像这样变形按钮来为ShowMore / ShowLess图像设置动画

相关问题