android - 当我单击没有项目的 ListView 时是否有事件?
问题描述
我基本上想在单击没有项目的 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>
解决方案
您可以TapGestureRecognizer
在 ListView 上添加
以 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 click 动作之前被调用,所以如果你只想在 itemListView
很少的时候在页面的空白处添加 click 事件。我们可以在列表视图下方定义另一个视图,并将点击事件设置为解决方法。
在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)
{
//...
}
推荐阅读
- java - Java如何增加数据库表中双精度的值
- javascript - 如何在 localStorage 中存储列表
- python-3.x - 使用 Python BeautifulSoup 4 逐步展开 HTML 标签复合结构的最佳方法?
- python - 迭代一组列表以找到 Python 中的最高平均值
- c# - C# 替换资源文件值
- android - 在我将 FireBase 添加到我的项目后,出现了这个错误
- javascript - Veu 未定义
- ruby-on-rails - 使用 chrome.identity 和 Rail 的 Doorkeeper 撤销 OAuth 令牌
- mysql - 外键约束 -(错误 1215)。请协助:)
- javascript - 我在使用 for in 循环删除对象键时遇到问题