onclicklistener关于android中的recyclerview的具体项目 [英] onclicklistener on the specific item of the recyclerview in android

查看:89
本文介绍了onclicklistener关于android中的recyclerview的具体项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会问非常基本的问题,但我长期陷入困境。

I am going to ask very basic question, but i am stuck in it for a long time.

在卡片视图后面有一个recycleview,每行有2张图片。
现在我想在图像而不是recycleview上创建点击监听器。

after card view there is an recycleview which has 2 images in each row. now i want to create the click listener on the images rather than the recycleview.

此活动的相应布局(layout_main.xml)(MainActivity.java)仅包含recyclerview。每行的元素都在另一个布局中(layout_images.xml)。我从layout_images.xml获取图像并在适配器类(Adapter.java)中对它们进行充气。

the corresponding layout(layout_main.xml) of this activity(MainActivity.java) contain only recyclerview. the elements of each row is in another layout(layout_images.xml). i am getting the images from layout_images.xml and inflate them in the adapter class(Adapter.java).

现在如何将动作侦听器放在图像上。

now how to put action listener on the images only.

其次,我想得到我点击的图像。怎么做到这一点。
喜欢,当我们点击视图时,我们创建一些方法

secondly, i want to get the image on which i clicked. how to get that. like, when we click on a view we create some method as

public void onClick(View view){
    // some code here
}

其中view是我们的对象点击。在我的情况下如何获取我点击的图像。
使用类型转换它可能会在用户没有点击图像时抛出异常。

where view is the object on which we clicked. in my case how to get the image on which i clicked. using type cast it might be throw an exception when user doesnot click on image.

推荐答案

一个内部的多个onClick事件recyclerView:

Multiple onClick events inside a recyclerView:

    public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{

            public ImageView iconImageView;
            public TextView iconTextView;

            public MyViewHolder(final View itemView) {
                super(itemView);

                iconImageView = (ImageView) itemView.findViewById(R.id.myRecyclerImageView);
                iconTextView = (TextView) itemView.findViewById(R.id.myRecyclerTextView);
    // set click event
                itemView.setOnClickListener(this);
                iconTextView.setOnClickListener(this);
// set long click event
                iconImageView.setOnLongClickListener(this);
            }

            // onClick Listener for view
            @Override
            public void onClick(View v) {

                if (v.getId() == iconTextView.getId()){
                    Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
                }
            }


            //onLongClickListener for view
            @Override
            public boolean onLongClick(View v) {

                final AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                builder.setTitle ("Hello Dialog")
                        .setMessage ("LONG CLICK DIALOG WINDOW FOR ICON " + String.valueOf(getAdapterPosition()))
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });

                builder.create().show();
                return true;
            }
        }

要获取点击的项目,您需要与视图ID匹配ievgetId()== yourViewItem.getId()

To get which item was clicked you match the view id i.e. v.getId() == yourViewItem.getId()

这篇关于onclicklistener关于android中的recyclerview的具体项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆