gpt4 book ai didi

android - 当我在自定义适配器中使用图像按钮时,setOnItemClickListener 在 ListView 中不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 09:27:26 25 4
gpt4 key购买 nike

在 ListView 中,我通过 CustomAdapter 类(扩展 ArrayAdapter)添加 TextView 和 ImageButton。当我在 CustomAdapter 类中将 onClickListener 添加到 ImageButton 时,ListView onItemClickListener 不起作用。如果我想选择多个列表以及监听器不工作的单个列表,并行 ImageButton onClickListener 工作。我希望 ListView 和 ImageButton onClickListener 和 onItemClickListener 在我的 Activity 中工作。如何解决这个问题?

谁给点建议!

代码如下:

public class ViewHolder {
TextView username;
ImageButton download;
TextView time;
ImageView contactpic; }

public interface ButtonListener {
public void onButtonClickListner(int position, VoiceMailRecords value);
}

public void setCustomButtonListner(ButtonListener listener) {
this.customListner = listener;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
viewHolder = new ViewHolder();
if(convertView==null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.recorded_item, parent, false);
//TextView date = (TextView) convertView.findViewById(R.id.date);
viewHolder.time = (TextView) convertView.findViewById(R.id.callingTime);
//TextView number = (TextView) convertView.findViewById(R.id.callerNumber);
viewHolder.username = (TextView) convertView.findViewById(R.id.callerName);
viewHolder.contactpic = (ImageView) convertView.findViewById(R.id.imageView_round);
// ImageView checked = (ImageView) convertView.findViewById(R.id.is_checked);
viewHolder.download = (ImageButton) convertView.findViewById(R.id.is_download);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
final VoiceMailRecords user = getItem(position);
viewHolder.username.setText(user.getCallerNumber());

return convertView;


}

Activity 中

voiceMailRecordAdapter=new VoiceMailRecordAdapter(this,voiceMailRecords,this);
voicemailList.setAdapter(voiceMailRecordAdapter);
voiceMailRecordAdapter.notifyDataSetChanged();

voicemailList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ListVoiceMailRecord.this,"Position :"+position+" clicked",Toast.LENGTH_LONG).show();
return true;
}
});

@Override
public void onButtonClickListner(int position, VoiceMailRecords value) {
Log.d(TAG, "VoiceMailApi onButtonClickListner " );
Toast.makeText(ListVoiceMailRecord.this,"onButtonClickListner :"+position+"",Toast.LENGTH_LONG).show();
}

最佳答案

您需要将 ImageButton 的 focusableInTouchMode 设置为 false

在你的 imageButton 中添加这一行

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

并且代替onItemClick()尝试onClick() for convertview

convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("clicked"," yes");
}
}

关于android - 当我在自定义适配器中使用图像按钮时,setOnItemClickListener 在 ListView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49689823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com