Android ListView自定义适配器ImageButton

Android ListView自定义适配器ImageButton,android,view,adapter,Android,View,Adapter,这可能不是正确的方法,如果有更好的方法请告诉我。 我已经创建了一个自定义适配器类&在我的getView方法中,我膨胀了我想要使用的视图 public View getView(int position, View convertView, ViewGroup parent) { View v = mInflater.inflate(R.layout.wherelayout, null); if (convertView != null)

这可能不是正确的方法,如果有更好的方法请告诉我。 我已经创建了一个自定义适配器类&在我的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;
    }
public View getView(int位置、视图转换视图、视图组父视图)
{
视图v=mInflater.充气(R.layout.wherelayout,空);
if(convertView!=null)
{
v=转换视图;
}
HashMap whereHash=(HashMap)this.getItem(位置);
if(whereHash!=null)
{
TextView-whereId=(TextView)v.findViewById(R.id.tvWhere);
TextView whereDetails=(TextView)v.findviewbyd(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(位置);
ibDelWhere.setOnClickListener(新的OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
//点击时做一些事情
}
}
);
}
}
返回v;
}
该视图由两个左对齐的文本视图和一个右对齐的图像按钮组成,我希望在单击按钮时能够从列表视图中删除该项。布局是这样的-

    <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项和按钮? 谢谢各位

i、 e.单击TextView项以启动已分配给它的ListView.onItemClick

当你点击文本视图而图像按钮在那里时会发生什么?它是否注册了对按钮的点击?还是什么都不做


ImageButton在行中占用多少空间?这可能是因为它太大了,以至于你不能点击它外面的那一行。

你可以让这两个都可以点击,但它实际上不受支持,Romain Guy会对你大喊大叫。此外,您将无法使用轨迹球对焦/按下按钮。话虽如此,您可以向按钮添加以下属性,这将使两者都可单击:

android:focusable="false"
android:focusableInTouchMode="false"

只要确保你能承受后果

我也有同样的问题。我的解决方案是在适配器内部为视图设置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;
}
希望有帮助!根据程序的其他部分,这可能会迫使您向适配器传递更多数据(我必须这样做),但它可以工作。

尝试设置 android:clickable=“false”在相对布局上

我在另一个线性布局中的线性布局也有同样的问题。 外部线性布局可点击=真,结果: ListView.OnItemClickListener不会启动。
将其设置为clickable=false后,它会工作。

您必须将imagebutton设置为非
可聚焦
和非
可聚焦到触摸模式
(单击即可)

请注意,与其他视图不同,您不能在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
:)


希望有帮助。

下面是列表视图中自定义适配器的示例

<?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>    

列表视图项在按钮区域外可见。listview和McClickListener事件根本不会触发,除非我移除按钮,然后它才会触发。抱歉,这不起作用。我还尝试更改包含TextView&ImageButton的布局的可聚焦属性——这也没有什么区别。谢谢。我将ImageButton更改为ImageView并连接了click侦听器,它也可以与listview.onItemClick事件一起正常工作。这看起来确实是一个焦点问题,不知道你的答案为什么不起作用。我想我会坚持使用ImageView,不需要一个按钮,只要它是可点击的。“无论如何,谢谢你。”戴维,这是我通常做的事谢谢,但这是试过的&不起作用。我没有按钮,而是将其更改为响应长时间单击。谢谢。“与其他视图不同,在xml中不能这样做”。这些信息非常有用。
<?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>    
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;  
}  
}