我想在ListView的整个布局空间上设置onClickListener(),即,如果我单击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) {
adapter.add(list.get(i));
i++;
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>
答案 0 :(得分:0)
为什么不对bool_val BOOL CHECK(bool_val IN(TRUE,FALSE))
的父级布局执行onClickListener()
?
您甚至尝试添加listview
或setOnTouchListener()
列表视图吗?
setOnClickListener
在Java方面
<LinearLayout
android:id="@+id/layout_list"
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>
答案 1 :(得分:0)
尝试此示例:
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>
希望有帮助!