Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Challenges in RecyclerView of Android L

I have been playing around with RecyclerView for a little bit. Is there any easy way to put OnClickListener for items in RecyclerView? I have tried implementing it in ViewHolder. The onClick event never got triggered.

And I have used notifyItemInserted(position) for adding new value into RecyclerView. The UI does not got refreshed automatically. Needed to pull up and down to refresh. But when I invoke notifyDatasetChanged(..), it is ok.

I have applied DefaultItemAnimator to RecyclerView. But, not seeing any animation when new item added.

Thanks advance for any idea.

This is the first Android L component I have tested out and I am stucking there.

Here is my Adapter class:

public class AdapterRecyclerView extends RecyclerView.Adapter<AdapterRecyclerView.MyViewHolder> {

private List<String> arrExperiences;

//Provide a reference to the type of views that you are using - Custom ViewHolder
public class MyViewHolder extends RecyclerView.ViewHolder {

    public TextView tvExperienceTitle;
    public TextView tvExperienceDesc;

    public MyViewHolder(RelativeLayout itemView) {
        super(itemView);
        tvExperienceTitle = (TextView) itemView.findViewById(R.id.tv_experience_title);
        tvExperienceDesc = (TextView) itemView.findViewById(R.id.tv_experience_desc);
    }

}

//Provide a suitable constructor : depending on the kind of dataset.
public AdapterRecyclerView(List<String> arrExperiences){
    this.arrExperiences = arrExperiences;
}

//Create new view : invoke by a Layout Manager
@Override
public AdapterRecyclerView.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RelativeLayout view = (RelativeLayout) LayoutInflater.from(parent.getContext()).inflate(R.layout.view_item_recycler, parent, false);
    MyViewHolder myViewHolder = new MyViewHolder(view);
    return myViewHolder;
}

@Override
public void onBindViewHolder(AdapterRecyclerView.MyViewHolder viewHolder, int position) {
    //get element from your dataset at this position.
    //replace the content of the view with this element.
    viewHolder.tvExperienceTitle.setText(arrExperiences.get(position));
}

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

public void addExperience(String experience, int position){
    arrExperiences.add(position, experience);
    notifyItemInserted(position);
    //notifyDataSetChanged();
}

public void removeExperience(){
    int index = (int) (Math.random() * arrExperiences.size());
    arrExperiences.remove(index);
    notifyItemRemoved(index);
    //notifyDataSetChanged();
}
}
like image 631
Aung Pyae Avatar asked Jul 03 '14 20:07

Aung Pyae


1 Answers

Simply add this in your Adapter:

@Override
public void onBindViewHolder(AdapterRecyclerView.MyViewHolder viewHolder, int position) {
    yourItems.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
          //do your stuff
       }
   });
}
like image 98
Lorenzo Camaione Avatar answered Sep 23 '22 13:09

Lorenzo Camaione