Android的ListView中自定义适配器的ImageButton [英] Android ListView Custom Adapter ImageButton
问题描述
这可能不是正确的做法,如果有更好的方法的请求告诉我。
我创建了一个类定义适配器和放大器;在我的getView方法我膨胀的观点我想用
This may not be the correct approach, if there is a better way pleas tell me. I've created a class of Custom Adapter & in my getView method I inflate the view I want to use
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中删除的项目单击该按钮时。布局是这样的 -
The view consists of 2 TextView aligned to the left & an ImageButton aligned to the right, I want to be able to delete the item from the ListView when the button is clicked. the layout is like this -
<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项和放大器;布局中的按钮?
感谢球员和放大器;加仑。
The problem is that when the ImageButton is in the layout, I can click it & the onClick() fires as expected, but I can't click the actual list item itself, i.e. click on the TextView items to fire the ListView.onItemClick that was assigned to it already. If I remove the ImageButton from the layout, then the ListView.onItemClick event fires when I click the item. Is there any way I can enable clicking both the ListView item & the button within the layout ? Thanks guys & gals.
推荐答案
您必须设置ImageButton的非可聚焦
和非 focusableInTouchMode
(点击就可以了)。
You have to set the imagebutton as non focusable
and non focusableInTouchMode
(clickable is ok).
请注意,而不是其他的意见,你不能这样做,在XML中,因为机器人:可聚焦
在被覆盖的ImageButton
的构造。
为了更precise,这就是的ImageView
和的ImageButton
之间的一些区别之一。见自己,这是在完成源的ImageButton
。
Please note, as opposed as other views, you can't do that in xml because the android:focusable
gets overwritten in ImageButton
's constructor.
To be more precise, that's one of the few differences between ImageView
and ImageButton
. See for yourself, this is the complete source of 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;
}
}
要解决,只需拨打 setFocusable(假)
从Java。或使用的ImageView
:)
To solve, just call setFocusable(false)
from java. Or use an ImageView
:)
myImageButton.setFocusable(false);
希望它帮助。
这篇关于Android的ListView中自定义适配器的ImageButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!