自定义ListView上的当前位置

时间:2012-10-13 11:32:16

标签: java android listview

我有一个自定义ListView。此ListView包含1个图像和3个TextView。如何单击ImageView并获取当前位置。 (不要点击其他元素,只有ImageView)

1 个答案:

答案 0 :(得分:1)

在自定义适配器类中,您正在使用getView(.. .. ..)方法:

@Override
    public View getView(final int position, final View convertView, final ViewGroup parent) {
//     >>>    >>>                 ^^^^^^  This is your posotion = index

        View row = convertView;
        Your_Holder holder = null;
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(resId, parent, false);
            holder = new Your_Holder();

            ....
            ....
            holder.yourImageView = (ImageView) row.findViewById(R.id.yourImageID);
            ....
            ....

            row.setTag(holder);
        }
        else{
            holder = (Your_Holder)row.getTag();
        }

   //    Set here your images click event
        holder.yourImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                System.out.println("Position = " + position)

            }
        });

    }