gpt4 book ai didi

Android ListView 自定义适配器 ImageButton

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:20:20 24 4
gpt4 key购买 nike

这可能不是正确的方法,如果有更好的方法请告诉我。我创建了一个自定义适配器类,在我的 getView 方法中我膨胀了我想使用的 View

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;
}

该 View 由 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 项目和布局中的按钮吗?谢谢伙计们。

最佳答案

您必须将图像按钮设置为非 focusable 和非 focusableInTouchMode(可点击即可)。

请注意,与其他 View 不同,您不能在 xml 中执行此操作,因为 android:focusableImageButton 的构造函数中被覆盖。更准确地说,这是 ImageViewImageButton 之间的少数差异之一。您自己看看,这是 ImageButton完整源代码。

@RemoteView
public class ImageButton extends ImageView {
public ImageButton(Context context) {
this(context, null);
}

public ImageButton(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.imageButtonStyle);
}

public ImageButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFocusable(true);
}

@Override
protected boolean onSetAlpha(int alpha) {
return false;
}
}

要解决,只需从 java 中调用 setFocusable(false)。或者使用 ImageView :)

myImageButton.setFocusable(false);

希望对您有所帮助。

关于Android ListView 自定义适配器 ImageButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6116583/

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