【问题标题】:Not able to focus on Entry inside nested ListView in Xamarin Forms无法专注于 Xamarin 表单中嵌套 ListView 内的条目
【发布时间】:2018-08-08 14:12:20
【问题描述】:

我在 ViewCell 中有一个带有 2 个控件的 ListView。 1- 条目,2- 另一个带有条目的 ListView。

现在我面临着独特的问题。

案例 1:如果我点击嵌套 ListView 中的条目,我将无法点击。它的行为就像只读。

案例 2:如果我首先点击不在嵌套 ListView 内的条目,我可以专注于它。在此之后,我还可以点击/关注嵌套 ListView 内的 Entry。

这意味着我必须先点击Entry(没有嵌套的ListView),然后我才能点击或关注Entry(有嵌套的ListView)。

我在这里附加了代码请查看代码并发送适当的答案。

MainPage.Xaml

<ListView Grid.Row="1" HasUnevenRows="True" IsPullToRefreshEnabled="False"
    ItemsSource="{Binding MainListViewItemSource}" CachingStrategy="RecycleElement"
    ItemTapped="OnListViewItemTapped" ItemSelected="OnListViewItemSelected">
    <ListView.ItemTemplate>
        <DataTemplate>
            <local:MyCell/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

MyCell.xaml

<Image Source="{Binding PlusMinusImage}>
    <Image.GestureRecognizers>
        <TapGestureRecognizer Tapped="ListOpenCommand" NumberOfTapsRequired="1"/>
    </Image.GestureRecognizers>
</Image>
<StackLayout IsVisible={Binding IsVisible}>
    <Label Text="Mainlabel" FontSize="15" FontFamily="Roboto" TextColor="Black"/>
    <Entry x:Name="MainEntry" TextColor="Gray" Text="{Binding MainEntryValue}">
    <ListView ItemsSource="{Binding MyInnerListViewItemSource}" 
        ItemTapped="OnListViewItemTapped" ItemSelected="OnListViewItemSelected">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Label Margin="0" Text="SampleInnerText" Grid.Column="0"/>
                        <Entry Margin="0" Text="{Binding SampleInnerTextValue}" 
                            Grid.Column="1"/>
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>

MainPage.xaml.cs/MyCell.xaml.cs

private void OnListViewItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    (sender as ListView).SelectedItem = null;
    return;
}
private void OnListViewItemTapped(object sender, ItemTappedEventArgs e)
{
    if (e.Item == null) return;
    ((ListView)sender).SelectedItem = null;
}

Android 列表视图渲染器

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    base.OnElementPropertyChanged(sender, e);
    if (Control!=null)
    {
        Control.HorizontalScrollBarEnabled = false;
        Control.VerticalScrollBarEnabled = false;
        Control.Focusable = true;
        Control.FocusableInTouchMode = true;
    }

}

【问题讨论】:

  • 分享你的代码,这样你就知道永远不要将列表视图相互嵌套,这被认为是一种不好的做法
  • @G.hakim 我添加了代码你能帮我解决这个问题吗?谢谢
  • 你能给我一个你想要做什么的图示吗?其他一切似乎都还好
  • On image Tap stack panel visibility is set and after open it like first Focus or select main ListView and than inner ListView as I got above
  • 尝试使用RepeaterView而不是listview depblog.weblogs.us/2017/09/24/xamarin-forms-repeaterview

标签: android listview xamarin xamarin.forms xamarin.android


【解决方案1】:

以下是不可滚动列表视图的代码,看看:

 public class NonScrollableListViewRenderer : ListViewRenderer
{
    public NonScrollableListViewRenderer(Context context) : base(context)
    {

    }
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ListView> e)
    {
        base.OnElementChanged(e);
        try
        {
            if (e == null || e.NewElement == null)
            {
                return;
            }
            var lit = e.NewElement;
            if (e.NewElement != null)
            {
                var listView = this.Control as Android.Widget.ListView;
                listView.NestedScrollingEnabled = true;
            }
        }
        catch (System.Exception ex)
        {
            AppLogger.LogException(ex);
        }
    }



    public Android.Views.View getViewByPosition(int pos, Android.Widget.ListView listView)
    {
        try
        {


            int firstListItemPosition = listView.FirstVisiblePosition;
            int lastListItemPosition = firstListItemPosition + listView.Adapter.Count - 1;

            if (pos < firstListItemPosition || pos > lastListItemPosition)
            {
                return listView.Adapter.GetView(pos, null, listView);
            }
            else
            {
                int childIndex = pos - firstListItemPosition;
                return listView.GetChildAt(childIndex);
            }
        }
        catch (System.Exception ex)
        {
            AppLogger.LogException(ex);
            return null;
        }
    }


    public override void OnViewAdded(Android.Views.View child)
    {
        base.OnViewAdded(child);

    }


    protected override void OnDetachedFromWindow()
    {
        try
        {
            if (Control == null)
            {

            }
            if (Element == null)
            {

            }
            base.OnDetachedFromWindow();

        }
        catch (System.Exception ex)
        {
            AppLogger.LogException(ex);
        }
    }


    protected override void Dispose(bool disposing)
    {
        disposing = false;
        if (Element == null)
        {

        }
        if (Control == null)
        {

        }
        base.Dispose(disposing);
    }
}

【讨论】:

  • 但是滚动对我来说不是问题我的问题焦点进入内部列表视图。
  • 由于多次滚动,您无法集中注意力,我已经告诉过您这是一种不好的方法
  • 哦,再次感谢您的帮助。让我们尝试调试您共享的代码。
  • 看起来一样,没有解决我的问题,抱歉。 :(
  • 好吧抱歉帮不了你,万一我发现任何与此相关的事情会回复你
猜你喜欢
  • 1970-01-01
  • 2018-05-09
  • 2017-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多