1

我有以下布局作为我的ListView. 如何处理以ImageViewiv_info 作为 ID 的第一次点击?

我必须同时使用onItemClickandonClick或其他任何东西吗?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_list_row_9patch"
    android:orientation="horizontal"
    android:padding="@dimen/fifteen_dp" >

    <ImageView
        android:id="@+id/iv_info"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="@dimen/exercise_list_row_iv_weight"
        android:contentDescription="@string/help"
        android:src="@drawable/btn_info" />

    <TextView
        android:id="@+id/erl_tv_exsercise_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="@dimen/exercise_list_row_tv_weight"
        android:paddingLeft="@dimen/ten_dp"
        android:textColor="@android:color/white"
        android:textSize="@dimen/list_row_text_size"
        android:textStyle="bold" >
    </TextView>

    <TextView
        android:id="@+id/erl_tv_participant_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="@dimen/exercise_list_row_tv_weight"
        android:gravity="right"
        android:textColor="@android:color/white"
        android:textSize="@dimen/list_row_text_size" >
    </TextView>

    <ImageView
        android:id="@+id/iv_exercise_status"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="@dimen/exercise_list_row_iv_weight"
        android:src="@drawable/btn_info" />

</LinearLayout>
4

2 回答 2

3

在适配器的getView方法中,获取图像视图并设置onClickListener..

例子:

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    ViewHolder holder;
    if(convertView==null){
        vi = inflater.inflate(R.layout.item, null);
        holder=new ViewHolder();
        holder.text=(TextView)vi.findViewById(R.id.text);;
        holder.image=(ImageView)vi.findViewById(R.id.image);
        vi.setTag(holder);
    }
    else
        holder=(ViewHolder)vi.getTag();

    holder.text.setText("item "+position);
    holder.image.setTag(data.get(position));
    holder.image.setOnClickListener(this);
    holder.text.setOnClickListener(this);
    imageLoader.DisplayImage(data.get(position), activity, holder.image);
    return vi;
}
于 2012-11-23T12:59:06.707 回答
1
    View vi=convertView;
    ViewHolder holder;

    if(convertView==null){
        vi = inflater.inflate(R.layout.item, null);
        holder=new ViewHolder();       

        holder.iv_info=(ImageView)vi.findViewById(R.id.iv_info);
        holder.iv_info.setOnClickListener(activity);

        holder.iv_exercise_status=(ImageView)vi.findViewById(R.id.iv_exercise_status);
        holder.image3.setOnClickListener(activity);

        vi.setTag(holder);
    } else
    holder=(ViewHolder)vi.getTag();


    public void onClick(View v) {
           // Perform action on click
          switch(v.getId()) {
            case R.id.iv_info:
              // do it smth
              break;
            case R.id.iv_exercise_status:

              break;               
          }
     }
于 2012-11-23T13:02:08.110 回答