如何处理单独的视图点击onItemClick?

时间:2012-11-23 12:56:52

标签: android listview onclick

我在ListView中将以下布局作为项目布局。如何处理第一个ImageView点击,其中iv_info为ID?

我是否必须同时使用onItemClickonClick或其他任何内容?

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

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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;               
          }
     }