这可能不是正确的方法,如果有更好的方式请求告诉我。 我创建了一类Custom 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;
}
该视图由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在布局中时,我可以点击它&amp; onClick()按预期触发,但我无法单击实际的列表项本身,即单击TextView项以触发已分配给它的ListView.onItemClick。如果我从布局中删除ImageButton,则单击该项时会触发ListView.onItemClick事件。有没有什么方法可以启用单击ListView项目&amp;布局中的按钮? 谢谢你们和他们加仑。
答案 0 :(得分: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;
}
}
要解决,只需从java调用setFocusable(false)
即可。或者使用ImageView
:)
myImageButton.setFocusable(false);
希望它有所帮助。
答案 1 :(得分:2)
你可以使两者都可以点击,但它并没有真正支持,而Romain Guy会对你大喊大叫。此外,您将无法使用轨迹球聚焦/按下按钮。话虽如此,您可以将以下属性添加到按钮,这应该都可以点击:
android:focusable="false"
android:focusableInTouchMode="false"
确保你能承受后果。
答案 2 :(得分:1)
尝试设置 相对布局上的android:clickable =“false”。
我在另一个Linearlayout中遇到了与LinearLayout相同的问题。 外部LinearLayout是clickable = true,结果: ListView.OnItemClickListener不会触发。 将它设置为clickable = false后就可以了。
答案 3 :(得分:0)
即。单击TextView项以激发已分配给它的ListView.onItemClick。
当ImageButton在那里时单击TextView会发生什么?它是否在按钮上注册了一次?或什么都不做?
ImageButton占用的行空间是多少?它可能足够大,你不能点击它外面的行。
答案 4 :(得分:0)
我有同样的问题。我的解决方案是为适配器内的视图设置onClick方法,而不是使用onItemClick。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
LayoutInflater inflater = context.getLayoutInflater();
v = inflater.inflate(R.layout.list_item, parent, false);
}
v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//This should replace the onListItemClick method
}
});
v.findViewById(R.id.someinnerview).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Write one of these for each of your inner views
}
});
return v;
}
希望有所帮助!根据程序的其余部分,这可能会迫使您将更多数据传递给适配器(我必须这样做),但它可以正常工作。
答案 5 :(得分:0)
这是列表视图中自定义适配器的示例。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dip">
<ImageView
android:layout_width="50dip"
android:layout_height="50dip"
android:id="@+id/imgViewLogo"
android:src="@drawable/icon"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:scaleType="center">
</ImageView>
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_width="wrap_content"
android:id="@+id/txtViewTitle"
android:layout_toRightOf="@+id/imgViewLogo"
android:layout_marginLeft="2dip">
</TextView>
<TextView
android:layout_height="wrap_content"
android:text="TextView"
android:layout_width="wrap_content"
android:id="@+id/txtViewDescription"
android:layout_toRightOf="@+id/imgViewLogo"
android:layout_below="@+id/txtViewTitle"
android:layout_marginLeft="2dip">
</TextView>
<TextView
android:layout_height="wrap_content"
android:text="TextView"
android:layout_width="wrap_content"
android:id="@+id/txtViewMobile"
android:layout_toRightOf="@+id/imgViewLogo"
android:layout_below="@+id/txtViewDescription"
android:layout_marginLeft="2dip">
</TextView>
并在BaseAdapter上创建java文件
public class ListViewCustomAdapter extends BaseAdapter {
Context context;
String[] mobile;
String[] month;
String[] number;
public LayoutInflater inflater;
public ListViewCustomAdapter(Context context,String[] month, String[] number, String[] mobile) {
// TODO Auto-generated constructor stub
super();
this.context = context;
this.month=month;
this.number=number;
this.mobile=mobile;
Log.i("88888888888888888","*******333********");
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//ADC71C 95AA1F
public int getCount() {
// TODO Auto-generated method stub
return month.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView txtViewTitle;
ImageView imgViewLogo;
TextView txtViewDescription;
TextView txtViewMobile;
}
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
Log.i("88888888888888888","*******444********");
ViewHolder holder;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
if (convertView == null)
{
Log.i("88888888888888888","*******555********");
convertView = inflater.inflate(R.layout.listitem_row, null);
holder = new ViewHolder();
holder.imgViewLogo = (ImageView) convertView.findViewById(R.id.imgViewLogo);
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.txtViewTitle);
holder.txtViewDescription = (TextView) convertView.findViewById(R.id.txtViewDescription);
holder.txtViewMobile = (TextView) convertView.findViewById(R.id.txtViewMobile);
convertView.setTag(holder);
}
else
{
Log.i("88888888888888888","*******666********");
holder = (ViewHolder) convertView.getTag();
}
Log.i("888888888888","Display the value of the textbox like(9856321584)other wise (TextView)");
holder.txtViewTitle.setText(month[position]);
holder.txtViewDescription.setText(number[position]);
holder.txtViewMobile.setText(mobile[position]);
return convertView;
}
}