in RecyclerView i want onclicklistner operation

Course Queries Syllabus Queries 3 years ago

2.86K 2 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 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 
0 views
0 shares

profilepic.png
manpreet 3 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.

Similar Forum