单击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>
答案 0 :(得分:2)
设置TextView
android:focusableInTouchMode="false"
android:focusable="false"
这将使您的列表可以集中精力 否则焦点移动到TextView,您的列表视图不会被点击。
答案 1 :(得分:2)
在适配器上更改此方法:
@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);
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("s click","id: ");
Intent i = new Intent(context, yourActivity.class); //change your activity to the one you want to launch
context.startActivity(i); //you already had your context defined above so just use it to start the activity
}
});
textView = (TextView) rowView.findViewById(R.id.textView1);
textView.setText(values[position]);
return rowView;
}
}
然后移除您的remindersList.setOnItemClickListener
。这将有效;)
答案 2 :(得分:1)
尝试删除MainActivtiy中的implements语句。
答案 3 :(得分:0)
试试这个..
remindersList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int pos,
long id) {
Log.d("s click","pos: "+ pos);
Log.d("s click","id: "+ id);
}});