in RecyclerView i want onclicklistner operation

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago


Code is for recyclerview I want to implement click operation in child option separately. how should i implement the given code below this code?

this my project code with adapter,child,parent

adapter.java

package com.blipclap.engineering_solution.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bignerdranch.expandablerecyclerview.Adapter.ExpandableRecyclerAdapter;
import com.bignerdranch.expandablerecyclerview.Model.ParentObject;
import com.blipclap.engineering_solution.Models.TitleChild;
import com.blipclap.engineering_solution.Models.TitleParent;
import com.blipclap.engineering_solution.R;
import com.blipclap.engineering_solution.ViewHolder.TitleChildViewHolder;
import com.blipclap.engineering_solution.ViewHolder.TitleParentViewHolder;

import java.util.List;



public class adapter extends ExpandableRecyclerAdapter<TitleParentViewHolder,TitleChildViewHolder> {

    LayoutInflater inflater;

    public adapter(Context context, List<ParentObject> parentItemList) {
        super(context, parentItemList);
        inflater=LayoutInflater.from(context);
    }

    @Override
    public TitleParentViewHolder onCreateParentViewHolder(ViewGroup viewGroup) {
        View view=inflater.inflate(R.layout.list_parent,viewGroup,false);
    return new TitleParentViewHolder(view);
    }

    @Override
    public TitleChildViewHolder onCreateChildViewHolder(ViewGroup viewGroup) {
        View view=inflater.inflate(R.layout.list_child,viewGroup,false);
        return new TitleChildViewHolder(view);    }

    @Override
    public 
profilepic.png
manpreet 2 years ago

Define interface in your adapter class

public interface onItemClickListener {
        void onItemClicked(View view, int position);
    }

public void setOnItemClickListener(onItemClickListener listener) {
        this.onItemClickListener = listener;
    }

On your Custom View Holder Implement View.OnClickListner and set Click Listener for required view.

public static class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

CustomViewHolder(View itemView){
super(itemView);
yourview.setOnClickListener(this);
}
@Override
        public void onClick(View view) {
            onItemClickListener.onItemClicked(view, getAdapterPosition());
        }
}

Now in the Adapter object just add setOnItemClickListener and you can bifurcate click event using the id of the view.

yourAdapter.setOnItemClickListener(new YourAdapter.onItemClickListener() {
            @Override
            public void onItemClicked(View view, int position) {
    // view.getId()
});

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.