【问题标题】:Is there an event for when I click the ListView where there is no item?当我单击没有项目的 ListView 时是否有事件?
【发布时间】:2020-03-25 00:36:11
【问题描述】:

我基本上想在单击没有项目的 ListView 时做一些事情,所以我不想使用 ListView.ItemClick 事件处理程序。当我尝试使用 ListView.Click 时,它告诉我必须使用项目单击。有什么帮助吗? 我以这种方式尝试了卢卡斯的解决方案:

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/lvQueue"/>
<ListView.GestureRecognizers>
    <TapGestureRecognizer
        Tapped="OnTapGestureRecognizerTapped"
        NumberOfTapsRequired="1"/>
</ListView.GestureRecognizers>

也是这样:

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/lvQueue">
    <ListView.GestureRecognizers>
    <TapGestureRecognizer
        Tapped="OnTapGestureRecognizerTapped"
        NumberOfTapsRequired="1"/>
</ListView.GestureRecognizers>
    </ListView>

【问题讨论】:

  • 据我了解,您试图点击 ListView,而不是 ListView Items 正确?如果不介意解释
  • 是的,我正在尝试单击 ListView 但在项目上。
  • 那么 Lucas anwser 有什么问题呢?他向您展示了如何在列表视图上获得点击,您是否在事件开始的地方设置了断点以查看它是否命中?
  • 我尝试时xaml文件中显示错误
  • 我添加了一个编辑

标签: android listview xamarin events xamarin.android


【解决方案1】:

您可以在 ListView 上添加TapGestureRecognizer

在 XML 中

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/lvQueue"/>

在后面的代码中

ListView listView = FindViewById<ListView>(Resource.Id.lvQueue); ;
listView.SetOnTouchListener(new GestureListener());

public class GestureListener : Java.Lang.Object, View.IOnTouchListener
{
    public bool OnTouch(View v, MotionEvent e)
    {

        // do something you want

        return true;
    }
}

更新

因为touch事件总是在item点击动作之前被调用,所以如果你只想在ListView的item很少的时候在页面的空白处添加点击事件。我们可以在列表视图下方定义另一个视图,并将点击事件设置为解决方法。

在xml中

将 ListView 放入 LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main">



        <ListView
        android:id="@+id/listView"
        android:background="@android:color/holo_blue_light"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
            />

    <TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_weight="1"   // set the weight as 1 , so that it will warp the white space of the screen auto
         android:background="@android:color/holo_blue_light"  // set the background color the same with thelistview
         android:id="@+id/view"/>



</LinearLayout>

在后面的代码中

TextView view = FindViewById<TextView>(Resource.Id.view);
view.Click += View_Click;
private void View_Click(object sender, EventArgs e)
{
  //...          
}

【讨论】:

  • 这对我不起作用...我添加了它,我收到一条消息说我需要 layout_width 和 layout_height
  • 感谢您部分为我工作,当我单击列表视图中的项目时,也会发生 OnTouch 方法。有什么办法可以预防吗?
  • Touch 事件会覆盖 ItemClick,有什么办法可以解决这个问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
相关资源
最近更新 更多