Expandable Recyclerview in android

Expandable Recyclerview Adpter Code 





public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private List<Model> heroList;
private Context context;
private static int currentPosition;

public Adapter(List<Model> heroList, Context context) {
this.heroList = heroList;
this.context = context;
currentPosition=heroList.size();
}

@NonNull
@Override
public Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.job_find_layout, parent, false);
return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull Adapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
Model hero = heroList.get(position);
holder.profile.setText(hero.getProfile());
holder.profile_name.setText(hero.getProfile_name());
holder.Experience.setText(hero.getExperience());
holder.Experience_year.setText(hero.getExperience_year());
holder.profile2.setText(hero.getProfile2());
holder.textViewEx.setText(hero.getTextViewEx());
holder.Year_of_Experience2.setText(hero.getYear_of_Experience2().trim());



holder.responsibilitiesDetails.setText(hero.getResponsibilitiesDetails());
holder.KeySkill.setText(hero.getKeySkill());
holder.KeySkillDetails.setText(hero.getKeySkillDetails().trim());

holder.linear_layout2.setVisibility(View.GONE);
if (currentPosition == position) {
//creating an animation
Animation slideDown = AnimationUtils.loadAnimation(context, R.anim.anim);

//toggling visibility
holder.linear_layout2.setVisibility(View.VISIBLE);
// holder.linear_layout2.setVisibility(View.GONE);

//adding sliding effect
holder.linear_layout2.startAnimation(slideDown);
}
holder.linear_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.linear_layout2.setVisibility(View.VISIBLE);

//getting the position of the item to expand it
if(currentPosition==position){
holder.linear_layout2.setVisibility(View.GONE);
}else {
currentPosition = position;
//holder.linear_layout2.setVisibility(View.GONE);
}
//reloding the list
notifyDataSetChanged();
}
});


}

@Override
public int getItemCount() {
return heroList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
TextView profile, profile_name, Experience, Experience_year,

CardView linear_layout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
profile = itemView.findViewById(R.id.profile);
profile_name = itemView.findViewById(R.id.profile_name);
Experience = itemView.findViewById(R.id.Experience);
Experience_year = itemView.findViewById(R.id.Experience_year);
profile2 = itemView.findViewById(R.id.profile2);
textViewEx = itemView.findViewById(R.id.textViewEx);
Year_of_Experience2 = itemView.findViewById(R.id.Year_of_Experience2);
linear_layout = itemView.findViewById(R.id.linear_layout);
linear_layout2 = itemView.findViewById(R.id.linear_layout2);



}
}
}

Rajeshbhatt12

My name is Rajesh Bhatt. I am working as a senior android developer . I have created this blog for kotlin ,java and Android Development interview questions etc..

Previous Post Next Post