在我的应用中设置此布局
<FrameLayout
android:gravity="center"
android:layout_width="35dp"
android:layout_height="35dp"
android:clickable="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:id="@+id/avatar_layout">
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/knotable_icon"
android:clickable="true"/>
<TextView
android:id="@+id/char_text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/character"
android:clickable="true"
android:textStyle="bold"
android:textColor="@color/black"
android:textSize="22sp" />
</FrameLayout>
它实际上是用户的个人资料图片。 我需要为这个FrameLayout实现onClick监听器。
在幕后我有AvatarListAdapter.java
和
public class AvatarListAdapter extends ArrayAdapter<AvatarUiItem> {
...
private class ViewHolder {
private View.OnClickListener mAvatarClickListener;
}
private View.OnClickListener mAvatarClickListener;
holder.avatarView=(FrameLayout) convertView.findViewById(R.id.avatar_layout); //inflates FrameLayout here
holder.avatarView.setTag(itemInfo);
holder.avatarView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Log.d("tag", "here we are");
if (mAvatarClickListener != null) {
mAvatarClickListener.onClick(view);
}
}
});
然后在主要活动中:
mAdp = new AvatarListAdapter(this, mAvatarUiItemArray, mApp,
mImageLoader, mDisplayMetrics, mPartCounterNum);
mListView.setAdapter(mAdp);
mAdp.setOnAvatarClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Object object = v.getTag();
if (object instanceof KnoteUiItem) {
KnoteUiItem uiItem = (KnoteUiItem) object;
Log.d("tag","avatar click");
}
}
});
这里的问题是OnClick事件永远不会发生。 真的很感激一些帮助。
答案 0 :(得分:1)
使用OnItemClickListener
holder.setTag(itemInfo); // Set the tag on the root view rather than the avatarView
.
.
.
.
OnItemClickListener listener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object object = v.getTag();
if (object instanceof KnoteUiItem) {
KnoteUiItem uiItem = (KnoteUiItem) object;
Log.d("tag","avatar click");
}
}
};
mListView.setOnItemClickListener(listener);