单击 ListView 中的 ListItem 没有响应,但是 longClickListener 正在响应。当用户单击列表视图项时,我想开始一个新活动。
我尝试了什么:
android:clickable="false" // and true - not working
android:descendantFocusability="blocksDescendants" // no change with or without it
为什么 onItemClickListener 没有响应?
MainActivity.xml
public class MainActivity extends Activity implements OnClickListener
{
private ListView remindersList;
private ImageView createEvent;
private ImageView deleteEvent;
private TextView noRemindersSet;
private String[] values = {"One", "Two", "Three"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createEvent = (ImageView) findViewById(R.id.imageView2);
deleteEvent = (ImageView) findViewById(R.id.imageView1);
createEvent.setOnClickListener(this);
deleteEvent.setOnClickListener(this);
remindersList = (ListView) findViewById(android.R.id.list);
ListViewArrayAdapter adapter = new ListViewArrayAdapter(MainActivity.this,
android.R.id.list, values);
//setting the adapter
remindersList.setAdapter(adapter);
remindersList.setTextFilterEnabled(true);
remindersList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.d("s click","id: ");
}});
remindersList.setLongClickable(true);
remindersList.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id)
{
Log.d("long clicked","id: " + id);
return true;
}
});
}
}
阵列适配器
public class ListViewArrayAdapter extends ArrayAdapter<String>
{
private final Context context;
String[] values;
private TextView textView;
public ListViewArrayAdapter(Context context, int resource, String[] values) {
super(context, resource, values);
// TODO Auto-generated constructor stub
this.context = context;
this.values = values;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
textView = (TextView) rowView.findViewById(R.id.textView1);
textView.setText(values[position]);
return rowView;
}
}
List_View.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#01B0F1"
android:longClickable="true"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:text="@string/textview"
android:textColor="#FFFFFF"
android:textSize="25sp" />
</LinearLayout>