我有ListView
使用android.R.layout.simple_list_item_checked
,我想要设置onItemLongClick()
侦听器和onItemClick()
侦听器来执行不同的操作。如何长按仅来检查/取消选中该项,而不是(短)点击?感谢
我已尝试将长按返回false,因此不会消耗该事件,但长按不会检查/取消选中。只需短暂点击即可。我还尝试人为地撤消短按(已注释掉的代码)中的检查,但这并不符合预期。
如果我删除短按回调,则长按仍然不会选中/取消选中。此外,短暂点击将继续检查/取消选中。
编辑 - 让我完整地展示代码:
package com.example.checked;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView myListView;
ArrayList<String> myData = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myListView = (ListView) findViewById(R.id.myListView);
// test data
myData = new ArrayList<String>();
myData.add("apple");
myData.add("banana");
myData.add("cherry");
myData.add("durian");
myData.add("eggplant");
ArrayAdapter myAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_checked, myData) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
CheckedTextView ctv = (CheckedTextView) view.findViewById(android.R.id.text1);
ctv.setFocusable(false);
ctv.setText(myData.get(position));
((ListView)parent).setItemChecked(position, false);
return view;
}
};
myListView.setAdapter(myAdapter);
myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
myListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView ctv = (CheckedTextView) view;
Log.i("onItemLongClick", "position=" + position + ", id=" + id + ", isChecked=" + ctv.isChecked());
// do something
return false;
}
});
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView ctv = (CheckedTextView) view;
Log.i("onItemClick", "position=" + position + ", id=" + id + ", isChecked=" + ctv.isChecked());
//if (ctv.isChecked()) ctv.setChecked(false);
//else if (!ctv.isChecked()) ctv.setChecked(true);
// do something else
}
});
}
}
和xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.checked.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:weightSum="100">
<ListView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_weight="80"
android:id="@+id/myListView" />
</LinearLayout>
</RelativeLayout>
执行以下操作的日志输出:
01-17 10:23:45.628 14928-14928/com.example.checked I/onItemLongClick: position=0, id=0, isChecked=false
01-17 10:23:46.242 14928-14928/com.example.checked I/onItemClick: position=0, id=0, isChecked=true
01-17 10:23:50.655 14928-14928/com.example.checked I/onItemLongClick: position=0, id=0, isChecked=true
01-17 10:23:51.002 14928-14928/com.example.checked I/onItemClick: position=0, id=0, isChecked=false
01-17 10:23:53.756 14928-14928/com.example.checked I/onItemClick: position=1, id=1, isChecked=true
01-17 10:23:55.131 14928-14928/com.example.checked I/onItemClick: position=1, id=1, isChecked=false
以上含义是onItemLongClick()
没有检查/取消选中该项目而只检查onItemLick()
,这与我想要的完全相反。
编辑#2 - 我尝试将listview xml更改为以下内容,同时保持java代码不变:
<ListView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_weight="80"
android:longClickable="true"
android:clickable="false"
android:id="@+id/myListView" />
但短暂的点击仍然生效,提供以下日志:
01-17 10:38:18.703 25598-25598/com.example.checked I/onItemLongClick: position=0, id=0, isChecked=false
01-17 10:38:18.973 25598-25598/com.example.checked I/onItemClick: position=0, id=0, isChecked=true
01-17 10:38:22.212 25598-25598/com.example.checked I/onItemLongClick: position=0, id=0, isChecked=true
01-17 10:38:22.353 25598-25598/com.example.checked I/onItemClick: position=0, id=0, isChecked=false
01-17 10:38:28.951 25598-25598/com.example.checked I/onItemClick: position=1, id=1, isChecked=true
01-17 10:38:30.391 25598-25598/com.example.checked I/onItemClick: position=1, id=1, isChecked=false
编辑#3 - 现在如果我onItemLongClick()
返回true
而不是false
,则长点击仍然无法选中/取消选中,但短线点击仍会继续检查/取消选中,给出以下日志:
01-17 10:43:45.181 28980-28980/com.example.checked I/onItemLongClick: position=0, id=0, isChecked=false
01-17 10:43:49.386 28980-28980/com.example.checked I/onItemLongClick: position=0, id=0, isChecked=false
01-17 10:43:53.662 28980-28980/com.example.checked I/onItemClick: position=1, id=1, isChecked=true
01-17 10:43:55.211 28980-28980/com.example.checked I/onItemClick: position=1, id=1, isChecked=false
答案 0 :(得分:0)
将android:longClickable="true"
添加到您的listview
。