无法专注于Xamarin Forms中嵌套ListView内的Entry

时间:2018-08-08 14:12:20

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

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

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

案例1:如果我在嵌套的ListView中点击Entry,则无法单击。它的行为就像只读的。

情况2:如果首先点击不在嵌套ListView内的Entry,则可以专注于此。之后,我还可以轻按/专注于嵌套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 Listview Renderer

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;
    }

}

1 个答案:

答案 0 :(得分:0)

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

 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);
    }
}