单击侦听器到RecyclerView

时间:2019-02-20 13:15:34

标签: java android android-recyclerview onitemclicklistener

我要完成的工作是在RecyclerView行内为ImageView创建一个侦听器。

此代码已经在工作,但这不是我想要的解决方案,因为您需要双击 ImageView,然后才能获得所需的结果。

// row click listener
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {

    @Override
    public void onClick(View view, final int position) {

        ImageView viewContent = (ImageView)view.findViewById(R.id.btnViewContent);
        ImageView deleteContent = (ImageView)view.findViewById(R.id.btnDeleteContent);

        viewContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "VIEW CONTENT", Toast.LENGTH_SHORT).show();
            }
        });

        deleteContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "DELETE CONTENT", Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void onLongClick(View view, int position) {}

}));

有什么想法可以将其转换为单击解决方案吗?咨询甚至是一条评论都对我有很大帮助。

3 个答案:

答案 0 :(得分:1)

这不是我们朋友建议的正确方法,在滚动过程中一次又一次地对onBindViewHolder进行校正,因此在其中添加侦听器不是最佳实践。

最好的方法是按照我的建议将其添加到ViewHolder中。检查上面的答案。

在OnBindViewHolder方法中添加imageView点击监听器

InputStream

}

答案 1 :(得分:0)

在OnBindViewHolder方法中添加imageView点击监听器

css

答案 2 :(得分:0)

Then there is no need to implement recyclerView.addOnItemTouchListener ,  inside Viewholder just add clicklistener on the view(image) you want below is the example for reference.

 ` public static class HeaderViewHolder extends RootViewHolder {

        @BindView(R.id.cardview)
        CardView cardview;
        @BindView(R.id.main_container)
        LinearLayout main_container;
        @BindView(R.id.music_cardview)
        CardView music;
        @BindView(R.id.shabad_cardview)
        CardView shabadvaani;
        @BindView(R.id.news_cardview)
        CardView news;
        @BindView(R.id.donate_cardview)
        CardView donate;
        @BindView(R.id.bs_cardview)
        CardView bs;
        @BindView(R.id.bl_cardview)
        CardView bl;
        @BindView(R.id.bng_cardview)
        CardView bng;

        @BindView(R.id.more_cardview)
        CardView more;

        @BindView(R.id.vid_cardview)
        CardView vid;

        @BindView(R.id.medi_cardview)
        CardView medi;
//
//        @BindView(R.id.ama_cardview)
//        CardView ama;




        public HeaderViewHolder(final View itemView,final OnItemClickListener mOnItemClickListener) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            news.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.openDrawer();
                    }


                    //Intent i= new Intent(ctx,);
                    //open drawer code
                }
            });

            shabadvaani.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent i = new Intent(ctx, IndexActivity.class);
                    ctx.startActivity(i);

                }
            });


            music.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent i = new Intent(ctx, MainActivity.class);
                    i.putExtra("slug","audiobhajan");
                    ctx.startActivity(i);

                    //open drawer code
                }
            });

            more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

//                    Intent i = new Intent(ctx, stayrocks.jambh.vani.auth.MainActivity.class);
//                    ctx.startActivity(i);

                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.openDrawer();
                    }


                    //open drawer code
                }
            });

            bs.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent= new Intent(ctx, AmaActivity.class);
                    ctx.startActivity(intent);

                }
            });

            bl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent i = new Intent(ctx, ItemListActivity.class);
                    ctx.startActivity(i);
                    //open drawer code
                }
            });

            bng.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    String appPackage = "com.my.bishnoi.nextgen";
//                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackage));
//                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    Intent intent= new Intent(ctx, WallpaperActivity.class);
                    ctx.startActivity(intent);
                    //open drawer code
                }
            });

            medi.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    String appPackage = "com.my.bishnoi.nextgen";
//                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackage));
//                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    Intent intent= new Intent(ctx, stayrocks.jambh.vani.activities.jyot.MainActivity.class);
                    ctx.startActivity(intent);
                    //open drawer code
                }
            });

            vid.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    String appPackage = "com.my.bishnoi.nextgen";
//                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackage));
//                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    Intent intent= new Intent(ctx, VideoListDemoActivity.class);
                    ctx.startActivity(intent);
                    //open drawer code
                }
            });

//            ama.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
////                    String appPackage = "com.my.bishnoi.nextgen";
////                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackage));
////                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    Intent intent= new Intent(ctx, AmaActivity.class);
//                    ctx.startActivity(intent);
//                    //open drawer code
//                }
//            });
        }
    }
`