我有一个自定义列表视图,其中包含TextView和图像 我想要做的是当我点击图像时,会发生一件事 如果我点击TextView,没有任何事情发生。
我可以在Activity类中处理它吗?
请帮我解决这个问题。
我搜索的太多,但我找不到我想要的东西。
答案 0 :(得分:0)
您是否只能在活动中找到视图并设置侦听器,而不是使用onItemClickListener(或Overriding on OnLIstItemClick())?
答案 1 :(得分:0)
您只需要从适配器的构造函数中存储活动上下文的副本,然后可以在getView覆盖中使用它来将ImageView的单击处理程序设置为活动onClick成员函数。
public class Activity1 extends Activity
{
public void onClick(final View v)
{
// handle click event
}
public class ListAdapter extends ArrayAdapter<string>
{
private Context mContext;
private List<string> lineItems;
public ListAdapter(Context context, int textViewResourceId, List<string> lineItems)
{
super(context, textViewResourceId, lineItems)
mContext = context;
this.lineItems = lineItems;
}
@Override
public View GetView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater li = (LayoutInflater) this.Context.GetSystemService(Context.LayoutInflaterService);
v = li.Inflate(R.Layout.list_item_layout, null);
}
Imageiew imageView = (ImageView)v.FindViewById(R.Id.image_view_id);
imageView.setOnClickListener(mContext.onClick);
return v;
}
}
}