我一直在玩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 } }); }
希望你能接受这个答案.