ListView的setOnItemClickListener不工作通过添加button

我有一个列表视图与文本和button在每一行,列表视图setOnItemClickListener()不工作。 是否有可能处理项目点击和button点击事件不同(项目点击应该调用ActivityA和button点击应该调用ActivityB)。 有没有人有解决scheme

private ArrayList<String> userIDArr = null; private ArrayList<String> userNameArr = null; private DatabaseHelper dbHelper = null; private ListView userListView=null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list_view); dbHelper = new DatabaseHelper(this.getApplicationContext()); Map<String,ArrayList<String>> displayMap = dbHelper.getUserListToDisplay(); userIDArr = displayMap.get("UserID"); userNameArr = displayMap.get("FirstName1"); userListView = (ListView) findViewById(R.id.listView2); userListView.setAdapter(new UserListAdapter(this,userIDArr)); userListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { Toast.makeText(usersListActivity.this, "Item in position " + position + " clicked", Toast.LENGTH_LONG).show(); } }); } public class UserListAdapter extends ArrayAdapter<String> { Activity context; public UserListAdapter(Activity context, ArrayList<String> names) { super(context, R.layout.list_item, names); this.context = context; } private class ViewHolder { public TextView UserNameAndID; public TextView Description; public Button UploadBtn; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; View rowView = convertView; if (rowView == null) { LayoutInflater inflater = context.getLayoutInflater(); rowView = inflater.inflate(R.layout.list_item, null, true); holder = new ViewHolder(); holder.UserNameAndID = (TextView) rowView.findViewById(R.id.User_detailsTxt); holder.Description = (TextView) rowView.findViewById(R.id.User_status); holder.UploadBtn = (Button) rowView.findViewById(R.id.uploadbutton); holder.UploadBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Toast.makeText(usersListActivity.this," Button clicked",Toast.LENGTH_SHORT).show(); } }); rowView.setTag(holder); } else { holder = (ViewHolder) rowView.getTag(); } String s = userNameArr.get(position)+","+userIDArr.get(position); holder.UserNameAndID.setText(s); holder.Description.setText("U r in middle"); return rowView; } } }` 

尝试设置你的button(或任何其他意见你想处理点击列表中的项目),如下所示:

 android:focusable="false" android:focusableInTouchMode="false" 

有时List仍然无法让Click Listener通过。 而在这种情况下,你可能不得不添加一个属性。

 android:descendantFocusability="blocksDescendants" 

而且这个属性必须被添加到你提供了ListView元素的XML的最顶层布局中。

如果在列表视图中有一个活动的视图/可聚焦的视图,那么它将禁用onItemClickListener …您可以尝试通过将: android:focusable="false" focusable android:focusable="false"到任何通常可以聚焦的视图来使其无法聚焦。

Batter的事情是添加两个Listener到整个rowViewAdapter内的Button 。 像这样的东西。

 public class MyAdapter extends BaseAdapter implements View.OnClickListener{ @Override public View getView(int position, View convertView, ViewGroup parent) { View rowView = convertView; if(rowView == null) { //intialize rowView, and set onclick listener only once. rowView = someIntilizationMehhodOrInflatorMethod(); //add listener to the button also and also to the row view rowView.setOnClickListener(this); } //all your inflation and setting values goes here and at the end, //set position as tag to get the correct position, rather buggy one. rowView.setTag(String.valueOf(position)); return rowView; } public void onClick(View v) { //now get the tag of View v and convert it to integer. int pos = Integer.parseInt(v.getTag().toString()); Toast.makeText(context,"Item in position " + pos + " clicked",Toast.LENGTH_LONG).show(); } } 

尝试设置setClickable(false)为每个button,ImageButton等和视图像这样:

  view.setClickable(false); button.setClickable(false); imagebutton.setClickable(false); 

你也要添加

 android:descendantFocusability="blocksDescendants" 

到主要(一级)布局

而不是添加button添加imageview,并提供setOnitemclcik列表,这将正常工作

  public View getView(final int position, View convertView, ViewGroup parent) { MyViewHolder mViewHolder; if(convertView == null) { convertView = inflater.inflate(R.layout.youtubesearchrow, null); mViewHolder = new MyViewHolder(); mViewHolder.alarm = (ImageView)convertView.findViewById(R.id.alarm); convertView.setTag(mViewHolder); } else { mViewHolder = (MyViewHolder) convertView.getTag(); } mViewHolder.tvTitle = detail(convertView, R.id.tvTitle, // mViewHolder.tvDesc = detail(convertView, R.id.tvDesc, mViewHolder.ivIcon = detail_image(convertView, R.id.ivIcon, mViewHolder.alarm.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } }); return convertView; } 

我不知道为什么,但有时当您设置适配器时,侦听器被清除。 然后,您必须在setAdapter或setListAdapter之后调用setOnClickListener。

我也得到同样的错误。 如果在列表布局中有任何buttonreplace为textView,ImageButton与ImageView。 对于参考他们是我的代码:

  private Activity activity; private LayoutInflater inflater; private List<ItemList> itemListsItems; private Bookmark_SharedPref pref; ImageLoader imageLoader = AppController.getInstance().getImageLoader(); public CustomListAdapter_Item(Activity activity,List<ItemList> itemListsItems){ this.activity=activity; this.itemListsItems=itemListsItems; pref = new Bookmark_SharedPref(activity); } @Override public int getCount() { return itemListsItems.size(); } @Override public Object getItem(int position) { return itemListsItems.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { View v = convertView; ViewHolder holder; if(inflater == null){ inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } if(convertView == null){ convertView = inflater.inflate(R.layout.item_layout,null); holder = new ViewHolder(); convertView.setTag(holder); } else{ holder = (ViewHolder)convertView.getTag(); } if(imageLoader == null){ imageLoader = AppController.getInstance().getImageLoader(); } holder.img = (NetworkImageView)convertView.findViewById(R.id.item_image); holder.Title = (TextView)convertView.findViewById(R.id.item_title); holder.Cat = (TextView)convertView.findViewById(R.id.item_category); holder.id = (TextView)convertView.findViewById(R.id.itemid); holder.Desc = (TextView)convertView.findViewById(R.id.item_description); holder.book = (ImageView)convertView.findViewById(R.id.bookmark_star); // getting blog data or the row ItemList il = itemListsItems.get(position); //setting image holder.img.setImageUrl(il.getImageUrl(), imageLoader); // setting title holder.Title.setText(il.getTitle()); //setting category holder.Cat.setText(il.getCategory()); //setting blog id holder.id.setText(il.getId()); //setting description holder.Desc.setText(il.getDescription()); //bookmarking the post holder.book.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { itemListsItems.get(position).isFav = !itemListsItems.get(position).isFav; ((ImageView)v.findViewById(R.id.bookmark_star)).setImageResource(itemListsItems.get(position).isFav ? R.mipmap.active_star : R.mipmap.inactive_star); if(itemListsItems.get(position).isFav){ String data = Integer.toString(itemListsItems.get(position).id); Toast.makeText(v.getContext(),"fav hai "+data,Toast.LENGTH_SHORT).show(); pref.addPref(data); } else { String data = Integer.toString(itemListsItems.get(position).id); Toast.makeText(v.getContext(),"not fav"+ data,Toast.LENGTH_SHORT).show(); pref.removePref(data); } //notifyDataSetChanged(); } }); ((ImageView)convertView.findViewById(R.id.bookmark_star)).setImageResource(itemListsItems.get(position).isFav ? R.mipmap.active_star : R.mipmap.inactive_star); /* if(itemListsItems.get(position).isFav){ String data = Integer.toString(itemListsItems.get(position).id); Toast.makeText(convertView.getContext(),"fav hai "+data,Toast.LENGTH_SHORT).show(); pref.addPref(data); } else { String data = Integer.toString(itemListsItems.get(position).id); //Toast.makeText(convertView.getContext(),"not fav"+ data,Toast.LENGTH_SHORT).show(); pref.removePref(data); } */ return convertView; } private static class ViewHolder { public TextView Title, Desc,Cat,id; ImageView book; NetworkImageView img; } 

还要记住要使列表布局为android:focussable =“true”,并为该button/文本参数使用android:focussable =“false”

从listView的内部服装视图中删除android:clickable =“true”

使父可点击的属性为这样的“ android:clickable =”false“ ”。

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="false" android:orientation="vertical" > 

再次面临问题,然后为所有的子视图添加这个可点击的假。