【问题标题】:Android ListView Custom Adapter ImageButtonAndroid ListView 自定义适配器 ImageButton
【发布时间】:2011-05-24 20:40:00
【问题描述】:

这可能不是正确的方法,如果有更好的方法请告诉我。 我创建了一个自定义适配器类 & 在我的 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 在布局中时,我可以单击它并按预期触发 onClick(),但我无法单击实际的列表项本身,即单击 TextView 项以触发 ListView .onItemClick 已分配给它。如果我从布局中删除 ImageButton,则当我单击该项目时会触发 ListView.onItemClick 事件。有什么方法可以让我同时点击布局中的 ListView 项目和按钮? 谢谢大家。

【问题讨论】:

    标签: android view adapter


    【解决方案1】:

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

    请注意,与其他视图相反,您不能在 xml 中执行此操作,因为 android:focusable 会在 ImageButton 的构造函数中被覆盖。 更准确地说,这是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);
    

    希望对你有帮助。

    【讨论】:

    • 谢谢。 “与其他视图相反,您不能在 xml 中这样做”。这些信息非常有用。
    【解决方案2】:

    你可以让两者都可点击,但它并不真正受支持,Romain Guy 会冲你大喊大叫。此外,您将无法使用轨迹球聚焦/按下按钮。话虽如此,您可以向按钮添加以下属性,这应该使两者都可点击:

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

    只要确保你能承受后果。

    【讨论】:

    • 对不起,那没用。我还尝试更改包含 TextView 和 ImageButton 的布局的 FOCUSABLE 属性 - 这也没有任何区别。谢谢。
    • 我将 ImageButton 更改为 ImageView 并连接了单击侦听器,它也可以与 listview.onItemClick 事件一起使用。它肯定看起来像一个焦点问题,不知道为什么你的答案不起作用。我想我会坚持使用 ImageView,只要它可以点击就不需要按钮。谢谢。
    • @daveD,这就是我通常做的。 :)
    【解决方案3】:

    尝试设置 android:clickable="false" 在相对布局上。

    我在另一个 LinearLayout 中的 LinearLayout 也遇到了同样的问题。 外部 LinearLayout 是 clickable=true,结果: ListView.OnItemClickListener 不会触发。 将其设置为 clickable=false 后,它就可以工作了。

    【讨论】:

    • 谢谢,但这已尝试过,但没有奏效。我没有使用按钮,而是将其更改为响应长按。
    【解决方案4】:

    即单击 TextView 项目以触发已分配给它的 ListView.onItemClick。

    当您在 ImageButton 存在时单击 TextView 会发生什么?它是否注册了单击按钮?还是什么都不做?

    ImageButton 在行中占用了多少空间?可能是它太大了,你不能点击它外面的行。

    【讨论】:

    • 列表视图项在按钮区域外可见。除非我删除按钮,否则 listview OnItemClickListener 事件根本不会触发,然后它会触发。
    【解决方案5】:

    我遇到了同样的问题。我的解决方案是在适配器内部为视图设置 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;
    }
    

    希望对您有所帮助!根据您程序的其余部分,这可能会迫使您将更多数据交给适配器(我必须这样做),但它可以工作。

    【讨论】:

      【解决方案6】:

      这是列表视图中自定义适配器的示例。

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

      【讨论】:

        猜你喜欢
        • 2019-05-23
        • 1970-01-01
        • 1970-01-01
        • 2016-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-23
        • 1970-01-01
        相关资源
        最近更新 更多