首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin:用于Android和windows UWP的Xamarin表单中的分组列表的垂直字母索引(跳转列表)

Xamarin:用于Android和windows UWP的Xamarin表单中的分组列表的垂直字母索引(跳转列表)
EN

Stack Overflow用户
提问于 2016-08-03 16:11:32
回答 1查看 3.6K关注 0票数 9

我已经为ios,android和windows平台设计了一个Xamarin表单的分组列表视图。当我在列表视图中设置GroupShortNameBindingproperty时,在IOS中会自动显示垂直索引(跳转列表)。但是跳转列表不会出现在android中。如何在android和windows中使用自定义渲染获得垂直索引的支持。如果任何人可以提供支持此功能跨平台的自定义渲染源代码。

EN

回答 1

Stack Overflow用户

发布于 2016-08-08 23:07:58

最简单的方法是使用XAML hack,如果你不需要CustomRenders的话。

您可以将ListView包装在高度和宽度等于父页面(内容页面)的RelativeLayout中。

对于列表视图,使用高度作为父对象,宽度为父对象的90%。添加一个宽度为10%的堆栈布局,从相对布局的90%开始,并将高度作为父级。使其方向垂直。将所有字母表作为标签添加到堆栈布局中,并实现其TapGestureScrollTo特定位置。

将Android的宽度设置为90%,iOS和windows保持为100%,堆栈布局宽度为0%,IsVisible=false

ViewModel:

代码语言:javascript
复制
public class JumpListViewModel : INotifyPropertyChanged
{
    private ObservableCollection<Item> _allItems;
    private List<string> _alphabetList;

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

    public JumpListViewModel()
    {
        AllItems = new ObservableCollection<Item>(new List<Item> { new Item { MyText = "1" }, new Item { MyText = "2" }, new Item { MyText = "3" } });

        AlphabetList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray().Select(x => x.ToString()).ToList();
    }

    public ObservableCollection<Item> AllItems
    {
        get { return _allItems; }
        set
        {
            _allItems = value;
            OnPropertyChanged();
        }
    }

    public List<string> AlphabetList
    {
        get { return _alphabetList; }
        set
        {
            _alphabetList = value;
            OnPropertyChanged();
        }
    }
}

查看:

代码语言:javascript
复制
<RelativeLayout VerticalOptions="FillAndExpand">

    <ListView VerticalOptions="FillAndExpand" HasUnevenRows="True" ItemsSource="{Binding AllItems}"
              SeparatorColor="Transparent" SeparatorVisibility="None" BackgroundColor="Transparent"
              RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1}">
      <RelativeLayout.WidthConstraint>
        <OnPlatform x:TypeArguments="Constraint" Android="{ConstraintExpression Type=RelativeToParent, Property=Width,Factor=0.9}"
                    iOS="{ConstraintExpression Type=RelativeToParent, Property=Width,Factor=1}"
          WinPhone="{ConstraintExpression Type=RelativeToParent, Property=Width,Factor=1}" />
      </RelativeLayout.WidthConstraint>

      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>

            <StackLayout HorizontalOptions="FillAndExpand" BackgroundColor="Silver">

              <Label Text="{Binding MyText}" />
              <Button Text="button" />
              <BoxView HeightRequest="1" Color="Gray" BackgroundColor="Gray" HorizontalOptions="FillAndExpand" />

            </StackLayout>

          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>

    <ListView VerticalOptions="FillAndExpand" HasUnevenRows="True" ItemsSource="{Binding AlphabetList}"
              SeparatorColor="Transparent" SeparatorVisibility="None" BackgroundColor="Transparent"
              RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.9}"
      RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.05}"
      RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.9}">
      <RelativeLayout.WidthConstraint>
        <OnPlatform x:TypeArguments="Constraint" Android="{ConstraintExpression Type=RelativeToParent, Property=Width,Factor=0.1}"
                    iOS="{ConstraintExpression Type=RelativeToParent, Property=Width,Factor=0, Constant=0}"
          WinPhone="{ConstraintExpression Type=RelativeToParent, Property=Width,Factor=0, Constant=0}" />
      </RelativeLayout.WidthConstraint>
      <ListView.IsVisible>
        <OnPlatform x:TypeArguments="x:Boolean" WinPhone="False" iOS="False" Android="True" />
      </ListView.IsVisible>
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <Label Text="{Binding .}" TextColor="Red" FontSize="Micro" />
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>

  </RelativeLayout>

Android截图:

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38737980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档