Android L的RecyclerView中的挑战

我一直在玩RecyclerView一点点.有没有简单的方法来放置RecyclerView中的项目OnClickListener?我试过在ViewHolder中实现它. onClick事件从未触发过.

我已经使用notifyItemInserted(position)将新值添加到RecyclerView中.用户界面没有自动刷新.需要上下刷新.但是当我调用notifyDatasetChanged(..)时,没关系.

我已经将DefaultItemAnimator应用到RecyclerView.但是,当新的项目添加时,没有看到任何动画.

感谢提前任何想法.

这是我测试过的第一个Android L组件,我正在那里.

这是我的适配器类:

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();
}
}

解决方法

只需将其添加到您的适配器中即可:
@Override
public void onBindViewHolder(AdapterRecyclerView.MyViewHolder viewHolder,int position) {
    yourItems.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
          //do your stuff
       }
   });
}

希望你能接受这个答案.

相关文章

以下为个人理解,如错请评 CE: 凭据加密 (CE) 存储空间, 实际路径/data/user_ce/ DE: 设备加密 (DE) 存...
转载来源:https://blog.csdn.net/yfbdxz/article/details/114702144 用EventLog.writeEvent打的日志(或...
事件分发机制详解 一、基础知识介绍 1、经常用的事件有:MotionEvent.ACTION_DOWN,MotionEvent.ACTION...
又是好久没有写博客了,一直都比较忙,最近终于有时间沉淀和整理一下最近学到和解决的一些问题。 最近进...
Android性能优化——之控件的优化 前面讲了图像的优化,接下来分享一下控件的性能优化,这里主要是面向...
android的开源库是用来在android上显示gif图片的。我在网上查了一下,大家说这个框架写的不错,加载大的...