如何捕获列表项中不同视图上的点击事件?

发布于 2024-12-19 07:06:04 字数 152 浏览 0 评论 0原文

我有一个列表视图,其中包含图像视图和线性布局的文本。我想当用户单击图像视图和文本视图时执行不同的操作。我尝试过 onItemClickListener 但它捕获整个线性布局。我在自定义适配器中尝试了 onClickListener,但无法获取其位置。

我该如何解决这个问题?

I have a list view with an image view and text in a linear layout. I want to perform different operations when user clicks on the Image view and the Text view. I have tried onItemClickListener but it's capturing on the whole linear layout. I tried onClickListener in custom adapter, but were unable to get its position.

How can I resolve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜未央樱花落 2024-12-26 07:06:04

在这里我分享我的代码。我在每个列表项中有三个 Textview,在其中一个上我放置了一个侦听器。此代码仅供参考。从下面的代码中学习,您可以将侦听器放在不同的列表项上。现在我希望您可以编写按照你想要的方式编码。

static class ViewHolder {
    public TextView tv1,tv2,tv3;

        }

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View rowView = null;
    final ViewHolder holder = new ViewHolder();
    if (convertView == null) {

        LayoutInflater inflator = context.getLayoutInflater();
        rowView = inflator.inflate(R.layout.attach_pic, null);

        holder.tv1 = (TextView) rowView.findViewById(R.id.defectpic);
        holder.tv2 = (TextView) rowView.findViewById(R.id.no_of_uploded_pics);
        holder.tv3 = (TextView) rowView.findViewById(R.id.camera);

        holder.tv3.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub  

                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                cameraIntent.putExtra(TAG, position);
                context.startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

            }
        });

        rowView.setTag(holder);
        List itemVal1 = (List)getItem(position);
        String st1 = (String)itemVal1.get(0);
        holder.tv1.setText(st1);

        List itemVal2 = (List)getItem(position);
        String st2 = (String)itemVal2.get(1);
        holder.tv2.setText(st2);

    } else {
        rowView = convertView;
        ((ViewHolder) rowView.getTag()).tv1.setTag(list.get(position));
        ((ViewHolder) rowView.getTag()).tv2.setTag(list.get(position));
        ((ViewHolder) rowView.getTag()).tv3.setTag(list.get(position));
    }

    return rowView;
}

Here i m sharing my code.i have three Textviews in each list item and on one of them i put a listener.This code is for reference.Learning from the below code U can put listeners on different list items.Now i hope u can write code as u want.

static class ViewHolder {
    public TextView tv1,tv2,tv3;

        }

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View rowView = null;
    final ViewHolder holder = new ViewHolder();
    if (convertView == null) {

        LayoutInflater inflator = context.getLayoutInflater();
        rowView = inflator.inflate(R.layout.attach_pic, null);

        holder.tv1 = (TextView) rowView.findViewById(R.id.defectpic);
        holder.tv2 = (TextView) rowView.findViewById(R.id.no_of_uploded_pics);
        holder.tv3 = (TextView) rowView.findViewById(R.id.camera);

        holder.tv3.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub  

                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                cameraIntent.putExtra(TAG, position);
                context.startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

            }
        });

        rowView.setTag(holder);
        List itemVal1 = (List)getItem(position);
        String st1 = (String)itemVal1.get(0);
        holder.tv1.setText(st1);

        List itemVal2 = (List)getItem(position);
        String st2 = (String)itemVal2.get(1);
        holder.tv2.setText(st2);

    } else {
        rowView = convertView;
        ((ViewHolder) rowView.getTag()).tv1.setTag(list.get(position));
        ((ViewHolder) rowView.getTag()).tv2.setTag(list.get(position));
        ((ViewHolder) rowView.getTag()).tv3.setTag(list.get(position));
    }

    return rowView;
}
╭ゆ眷念 2024-12-26 07:06:04

为您要单击的每个视图设置标签作为其位置和位置使用 gettag 获取

set tag for each view you are going to click as its position & get that using gettag

遥远的她 2024-12-26 07:06:04

首先使您的文本视图和图像视图可点击且可聚焦。

其次,在 getView 中的适配器类中,将事件添加到 textview 和 imageview。

Firstly make both your textview and the imageview clickable and focusable.

Secondly Inside Your adapter class within getView add events to both textview and imageview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文