Android ListView自定义适配器ImageButton

dav*_*veD 6 android view adapter

如果有更好的方式请求告诉我,这可能不是正确的方法.我已经创建了一个自定义适配器类,在我的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;
    }
Run Code Online (Sandbox Code Playgroud)

该视图由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>
Run Code Online (Sandbox Code Playgroud)

问题是,当ImageButton在布局中时,我可以单击它并按预期触发onClick(),但是我无法单击实际的列表项本身,即单击TextView项以触发ListView.onItemClick已被分配给它.如果我从布局中删除ImageButton,则单击该项时会触发ListView.onItemClick事件.有没有什么方法可以启用单击ListView项目和布局中的按钮?谢谢你们和gals.

tac*_*one 24

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

请注意,与其他视图相反,您不能在xml中执行此操作,因为在构造函数中android:focusable会被覆盖ImageButton.更确切地说,这是ImageView和之间的少数差异之一ImageButton.亲眼看看,这是完整的来源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;
    }
}
Run Code Online (Sandbox Code Playgroud)

要解决,只需setFocusable(false)从java 调用即可.或使用ImageView:)

myImageButton.setFocusable(false);
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.