【发布时间】:2011-05-24 20:40:00
【问题描述】:
这可能不是正确的方法,如果有更好的方法请告诉我。 我创建了一个自定义适配器类 & 在我的 getView 方法中我膨胀了我想使用的视图
public View getView(int position, View convertView, ViewGroup parent)
{
View v = mInflater.inflate(R.layout.wherelayout, null);
if (convertView != null)
{
v = convertView;
}
HashMap<String, Object> whereHash = (HashMap<String, Object>) this.getItem(position);
if (whereHash != null)
{
TextView whereId = (TextView) v.findViewById(R.id.tvWhere);
TextView whereDetails = (TextView) v.findViewById(R.id.tvWhereDetails);
ImageButton ibDelWhere = (ImageButton) v.findViewById(R.id.ibDelWhere);
whereId.setText((CharSequence) whereHash.get("where"));
whereDetails.setText((CharSequence) whereHash.get("details"));
if (ibDelWhere != null)
{
ibDelWhere.setId(position);
ibDelWhere.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//do stuff when clicked
}
}
);
}
}
return v;
}
视图由 2 个向左对齐的 TextView 和一个向右对齐的 ImageButton 组成,我希望能够在单击按钮时从 ListView 中删除该项目。布局是这样的-
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal" android:clickable="true">
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="25sp" android:id="@+id/tvWhere" android:textColor="#00FF00" android:text="TextView" android:gravity="top|left" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"></TextView>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/tvWhereDetails" android:textColor="#0000FF" android:text="TextView" android:textSize="18sp" android:layout_below="@+id/tvWhere" android:gravity="bottom|left" android:layout_alignParentLeft="true"></TextView>
<ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/eraser" android:id="@+id/ibDelWhere" android:layout_alignParentRight="true" android:layout_alignParentTop="true"></ImageButton>
</RelativeLayout>
问题是,当 ImageButton 在布局中时,我可以单击它并按预期触发 onClick(),但我无法单击实际的列表项本身,即单击 TextView 项以触发 ListView .onItemClick 已分配给它。如果我从布局中删除 ImageButton,则当我单击该项目时会触发 ListView.onItemClick 事件。有什么方法可以让我同时点击布局中的 ListView 项目和按钮? 谢谢大家。
【问题讨论】: