简体   繁体   English

通过onClickListener调用ListView项目onItemClick

[英]ListView item onItemClick is being called via onClickListener

In My list i have items that has a button inside of it, currently whenever i try clicking the button it also triggers the onItemClick. 在“我的列表”中,我有一些带有按钮的项目,目前,每当我尝试单击该按钮时,它也会触发onItemClick。 The code below is inside the getView 下面的代码在getView内部

holder.btnAdd.setTag(tg);       
    holder.btnAdd.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int id = v.getId();
            Toast.makeText(getParent().getActivity(), "clicked this button",Toast.LENGTH_LONG).show();
    });

Here is the onItemClick , this is placed outside the adapter. 这是onItemClick,它位于适配器之外。

HorizontalListView hlv = (HorizontalListView) view
            .findViewById(R.id.gallery);

    GalleryAdapter ga = new GalleryAdapter(offerItemDetailsFragment,
            result);
    ga.setListView(hlv);
    hlv.setAdapter(ga);
    hlv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
             RelatedOffer relOffer = (RelatedOffer) parent.getAdapter().getItem(position);
            ...
        }
    });

You can't use an OnItemClickListener with lists that have internal clickable elements without having this effect. 如果不使用带有内部可点击元素的列表,则不能使用OnItemClickListener Instead, try adding an OnClickListener to the root layout of your row and execute your item click logic there. 相反,尝试将OnClickListener添加到行的根布局中,然后在其中执行项目单击逻辑。

If you have button at yours list than try to retrieve id of the button and than have a click listener with respect to the button or else write listener for the button at custom adapter if you have a custom adapter!! 如果您的按钮在列表中,则尝试检索该按钮的ID,并对该按钮具有单击侦听器,或者如果您有一个自定义适配器,则在自定义适配器上为该按钮编写侦听器!

please visit http://www.androidhub4you.com/2013/02/muftitouch-listview-multi-click.html hope it will help you. 请访问http://www.androidhub4you.com/2013/02/muftitouch-listview-multi-click.html希望对您有所帮助。

suppose you have yours holder as follows 假设您的持有人如下

static class UserHolder {
  TextView textName;
  TextView textAddress;
  TextView textLocation;
  Button btnEdit;
  Button btnDelete;
 }

with respect to this try to write getView as follows 关于这个尝试写getView如下

@Override
 public View getView(int position, View convertView, ViewGroup parent) {
  View row = convertView;
  UserHolder holder = null;

  if (row == null) {
   LayoutInflater inflater = ((Activity) context).getLayoutInflater();
   row = inflater.inflate(layoutResourceId, parent, false);
   holder = new UserHolder();
   holder.textName = (TextView) row.findViewById(R.id.textView1);
   holder.textAddress = (TextView) row.findViewById(R.id.textView2);
   holder.textLocation = (TextView) row.findViewById(R.id.textView3);
   holder.btnEdit = (Button) row.findViewById(R.id.button1);
   holder.btnDelete = (Button) row.findViewById(R.id.button2);
   row.setTag(holder);
  } else {
   holder = (UserHolder) row.getTag();
  }
  User user = data.get(position);
  holder.textName.setText(user.getName());
  holder.textAddress.setText(user.getAddress());
  holder.textLocation.setText(user.getLocation());
  holder.btnEdit.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Log.i("Edit Button Clicked", "**********");
    Toast.makeText(context, "Edit button Clicked",
      Toast.LENGTH_LONG).show();
   }
  });
  holder.btnDelete.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Log.i("Delete Button Clicked", "**********");
    Toast.makeText(context, "Delete button Clicked",
      Toast.LENGTH_LONG).show();
   }
  });
  return row;

 }

so yours custom adapter can look like 这样您的自定义适配器可以看起来像

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class UserCustomAdapter extends ArrayAdapter<User> {
 Context context;
 int layoutResourceId;
 ArrayList<User> data = new ArrayList<User>();

 public UserCustomAdapter(Context context, int layoutResourceId,
   ArrayList<User> data) {
  super(context, layoutResourceId, data);
  this.layoutResourceId = layoutResourceId;
  this.context = context;
  this.data = data;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  View row = convertView;
  UserHolder holder = null;

  if (row == null) {
   LayoutInflater inflater = ((Activity) context).getLayoutInflater();
   row = inflater.inflate(layoutResourceId, parent, false);
   holder = new UserHolder();
   holder.textName = (TextView) row.findViewById(R.id.textView1);
   holder.textAddress = (TextView) row.findViewById(R.id.textView2);
   holder.textLocation = (TextView) row.findViewById(R.id.textView3);
   holder.btnEdit = (Button) row.findViewById(R.id.button1);
   holder.btnDelete = (Button) row.findViewById(R.id.button2);
   row.setTag(holder);
  } else {
   holder = (UserHolder) row.getTag();
  }
  User user = data.get(position);
  holder.textName.setText(user.getName());
  holder.textAddress.setText(user.getAddress());
  holder.textLocation.setText(user.getLocation());
  holder.btnEdit.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Log.i("Edit Button Clicked", "**********");
    Toast.makeText(context, "Edit button Clicked",
      Toast.LENGTH_LONG).show();
   }
  });
  holder.btnDelete.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Log.i("Delete Button Clicked", "**********");
    Toast.makeText(context, "Delete button Clicked",
      Toast.LENGTH_LONG).show();
   }
  });
  return row;

 }

 static class UserHolder {
  TextView textName;
  TextView textAddress;
  TextView textLocation;
  Button btnEdit;
  Button btnDelete;
 }
}

Read more: http://www.androidhub4you.com/2013/02/muftitouch-listview-multi-click.html#ixzz2zm0i96yo 了解更多: http : //www.androidhub4you.com/2013/02/muftitouch-listview-multi-click.html#ixzz2zm0i96yo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM