简体   繁体   English

RecyclerView如何在每个项目上设置OnClickListener?

[英]RecyclerView how to setOnClickListener on every item?

What I want this code to do is: whenever the user clicks on the item (example: like or dislike), I want something to happen to my firebase (eg. set the value of the like to 1).我想要这段代码做的是:每当用户点击该项目(例如:喜欢或不喜欢)时,我希望我的 firebase 发生一些事情(例如,将 like 的值设置为 1)。 I'm trying so hard to set a click listener for every item (like, dislike, happy emote, report).我正在努力为每个项目(比如,不喜欢,快乐的表情,报告)设置一个点击监听器。 Even if I set the click listener inside the static class, I can't call my Database Reference.即使我在 static class 中设置了点击侦听器,我也无法调用我的数据库参考。 I also tried to do CommentsActivity.this.mReviewsDatabase..etc but it doesn't work because it doesn't want a static class.我也尝试过 CommentsActivity.this.mReviewsDatabase..etc 但它不起作用,因为它不需要 static class。 And if I remove the static from the class, the app crashes.如果我从 class 中删除 static,应用程序就会崩溃。

public class CommentsActivity extends AppCompatActivity {

    private RecyclerView mCommentList;
    public DatabaseReference mReviewsDatabase;

    private FirebaseUser mCurrentUser;

    private ImageView happyEmote, thumpUp, thumbDown ,reportReview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comments);

        String title = getIntent().getStringExtra("EXTRA_SESSION_ID");

        mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();


        mReviewsDatabase = FirebaseDatabase.getInstance().getReference().child("Film_reviews").child(title);

        mCommentList = (RecyclerView)findViewById(R.id.reviews_list);
        mCommentList.setHasFixedSize(true);
        mCommentList.setLayoutManager(new LinearLayoutManager(this));

        FirebaseRecyclerAdapter<AllCommClass, CommentsActivity.ReviewsViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllCommClass, CommentsActivity.ReviewsViewHolder>(
                AllCommClass.class,
                R.layout.comment_single_layout,
                CommentsActivity.ReviewsViewHolder.class,
                mReviewsDatabase
        ) {
            @Override
            protected void populateViewHolder(CommentsActivity.ReviewsViewHolder reviewsViewHolder, AllCommClass allCommClass, int i) {
                reviewsViewHolder.setUsername(allCommClass.getUsername());
                reviewsViewHolder.setReview(allCommClass.getReview());
                reviewsViewHolder.setVoto(allCommClass.getVoto());

                reviewsViewHolder.setReport();
                reviewsViewHolder.setLike();
                reviewsViewHolder.setDislike();
                reviewsViewHolder.setHappyEmote();

                reviewsViewHolder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {                    }
                });
            }
        };
        mCommentList.setAdapter(firebaseRecyclerAdapter);

    }





    public static class ReviewsViewHolder extends RecyclerView.ViewHolder {
        View mView;
        public ReviewsViewHolder(@NonNull View itemView) {
            super(itemView);
            mView = itemView;

        }
        public void setReport() {
            ImageView reportReview = mView.findViewById(R.id.reportReview);
            reportReview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.v("Tag","You've reported the comment number "+getAdapterPosition());
                }
            });
        }

        public void setLike() {
            ImageView thumpUp = mView.findViewById(R.id.thumbUp);
            thumpUp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.v("Tag","You've liked the comment number "+getAdapterPosition());
                }
            });
        }

        public void setDislike() {
            ImageView thumpDown = mView.findViewById(R.id.thumbDown);
            thumpDown.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.v("Tag","You've disliked the comment number "+getAdapterPosition());
                }
            });
        }

        public void setHappyEmote() {
            ImageView happyEmote = mView.findViewById(R.id.happyEmote);
            happyEmote.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.v("Tag","You've added a reaction to the comment number "+getAdapterPosition());
                }
            });
        }

        public void setUsername(String username){
            TextView titleView = mView.findViewById(R.id.user_single_username_comment_section);
            titleView.setText(username);
        }
        public void setReview(String review){
            TextView titleView = mView.findViewById(R.id.user_single_review);
            titleView.setText(review);
        }

        public void setVoto(int voto){
            TextView titleView = mView.findViewById(R.id.user_single_rating);
            titleView.setText(String.valueOf(voto));
        }
    }}

"

There are basically 2 ways as a beginner to set click listener to a recycler view item.作为初学者,基本上有两种方法可以将点击侦听器设置为回收站视图项。

  1. The first was is the easy but nonoptimal way to do it.第一个是简单但非最佳的方法。 Inside your onBindViewHolder of your adapter class set an onClickListener to your holder.itemview在您的适配器 class 的 onBindViewHolder 中,将 onClickListener 设置为您的 holder.itemview
  2. The second way is to use an onClickInterface and then call it from the calling/main activity.第二种方法是使用 onClickInterface,然后从调用/主活动中调用它。 Please let me know if you need any further guidance如果您需要任何进一步的指导,请告诉我

When we are fetching data from firebase当我们从 firebase 获取数据时

  1. The process is to match the Id of the item on firebase you are fetching through your model class to populate your recycleView.该过程是匹配您通过 model class 获取的 firebase 上的项目的 ID 以填充您的 recycleView。
  2. If you want your application to remember the number of like or dislike on a certain button you need to set count on the id of the item on which the user will perform the click.如果您希望您的应用程序记住某个按钮上的喜欢或不喜欢的数量,您需要在用户将执行点击的项目的 id 上设置计数。
  3. You can show that count or keep track of the button click in that manner.您可以显示该计数或以这种方式跟踪按钮单击。

If you need the code for it let me know如果您需要它的代码,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM