android - 如何在整个 ListView(而不是 ListView 的单个项目)上设置点击监听器?
问题描述
我想在 ListView 的整个布局空间上设置 onClickListener(),即如果我单击 ListView 布局区域的任何位置,则应执行特定任务。如何在 Android 中使用 java 实现这一点?
请注意,我不想要 onItemClickListener(),点击屏幕上的任何地方都应该执行任务。
聊天活动.java
chatList = (ListView) findViewById(R.id.chat_list);
adapter = new ChatMessageAdapter(ChatActivity.this, tempList);
chatList.setAdapter(adapter);
chatList.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// code here
return false;
}
});
活动聊天.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/parent_layout"
tools:context=".MainActivity"
android:layout_marginRight="18dp"
android:layout_marginLeft="18dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="18dp"
android:backgroundTint="@color/colorAccent">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Person 1"
android:textStyle="bold"
android:gravity="left"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Person 2"
android:textStyle="bold"
android:gravity="right"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/chat_list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"/>
</LinearLayout>
</LinearLayout>
解决方案
试试这个示例:
MainActivity.java
public class MainActivity extends AppCompatActivity {
final private static float TOUCH_TOLERANCE = 10;
float fingerDownX, fingerDownY;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> datalist = new ArrayList<>();
for(int i=0; i<30; i++){
datalist.add("Item " + i);
}
ListView listView = findViewById(R.id.listView);
ArrayAdapter adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, datalist);
listView.setAdapter(adapter);
listView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
float fingerX = motionEvent.getX();
float fingerY = motionEvent.getY();
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
fingerDownX = fingerX;
fingerDownY = fingerY;
}
if((motionEvent.getAction() == MotionEvent.ACTION_UP)&&
(Math.abs(fingerDownX - fingerX) < TOUCH_TOLERANCE)&&
(Math.abs(fingerDownY - fingerY) < TOUCH_TOLERANCE)){
Toast.makeText(getApplicationContext(), "List Clicked!", Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
}
activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
希望有帮助!
推荐阅读
- mysql - GROUP_CONCAT 根据分组行的其他字段的值
- python - 按类别分组并查找给定频率的百分比变化
- hadoop - 启用 Kerberos 后无法访问 Hadoop CLI
- c# - IDbTransaction.Commit 在事务完成之前返回
- sql - 如何在oracle中添加主键列而不截断表
- java - 堆大小:getRuntime().maxMemory() 远小于 -Xmx 参数
- mysql - 将sqlite更改为mysql后出现错误,表不存在?
- jwt - 更改 JWT 密码时如何防止注销用户?
- terraform - 在 Rundeck 中运行 Terraform 进程
- javascript - 在选择选项中更改字符串