Я хочу установить onClickListener () во всем пространстве макета ListView, т. Е. Если я щелкну в любом месте области макета ListView, должна быть выполнена конкретная задача.Как я могу сделать это в Android, используя java?
Обратите внимание, что я не хочу onItemClickListener (), касание в любом месте экрана должно выполнить задачу.
ChatActivity.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;
}
});
activity_chat.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>