其中大部分内容实现其实是和上篇文章是一样的,就不多叙述了,就一个地方不同,就是我们在适配器中绑定ViewHolder的方法中需要重新给我们的itemView布局设置height,我这里是生成随机数来设置高度的,代码实现为:
MainActivity.java:
publicclassMainActivityextendsAppCompatActivity {privateRecyclerView mRecyclerView;privateList
lists;privateMyRecyclerAdapter adapter;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
mRecyclerView= (RecyclerView)this.findViewById(R.id.recyclerView);
mRecyclerView.setItemAnimator(newDefaultItemAnimator());//mRecyclerView.addItemDecoration();//设置分割线mRecyclerView.setLayoutManager(newStaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));//设置RecyclerView布局管理器为2列垂直排布adapter =newMyRecyclerAdapter(this,lists);
mRecyclerView.setAdapter(adapter);
adapter.setOnClickListener(newMyRecyclerAdapter.OnItemClickListener() {
@OverridepublicvoidItemClickListener(View view,intpostion) {
Toast.makeText(MainActivity.this,"点击了:"+postion,Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidItemLongClickListener(View view,intpostion) {//长按删除lists.remove(postion);
adapter.notifyItemRemoved(postion);
}
});
}privatevoidinitData() {
lists=newArrayList();for(inti = 0; i < 100; i++) {
lists.add("" +i);
}
}
}
MyRecyclerAdapter.java::
publicclassMyRecyclerAdapterextendsRecyclerView.Adapter
{privateListlists;privateContext context;privateListheights;privateOnItemClickListener mListener;publicMyRecyclerAdapter(Context context,Listlists) {this.context =context;this.lists =lists;
getRandomHeight(this.lists);
}privatevoidgetRandomHeight(List
lists){//得到随机item的高度heights =newArrayList<>();for(inti = 0; i < lists.size(); i++) {
heights.add((int)(200+Math.random()*400));
}
}publicinterfaceOnItemClickListener{voidItemClickListener(View view,intpostion);voidItemLongClickListener(View view,intpostion);
}publicvoidsetOnClickListener(OnItemClickListener listener){this.mListener =listener;
}
@OverridepublicMyViewHolder onCreateViewHolder(ViewGroup parent,intviewType) {
View view= LayoutInflater.from(context).inflate(R.layout.item,parent,false);
MyViewHolder viewHolder=newMyViewHolder(view);returnviewHolder;
}
@OverridepublicvoidonBindViewHolder(finalMyViewHolder holder,intposition) {
ViewGroup.LayoutParams params= holder.itemView.getLayoutParams();//得到item的LayoutParams布局参数params.height = heights.get(position);//把随机的高度赋予item布局holder.itemView.setLayoutParams(params);//把params设置给item布局holder.mTv.setText(lists.get(position));//为控件绑定数据if(mListener!=null){//如果设置了监听那么它就不为空,然后回调相应的方法holder.itemView.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {intpos = holder.getLayoutPosition();//得到当前点击item的位置posmListener.ItemClickListener(holder.itemView,pos);//把事件交给我们实现的接口那里处理}
});
holder.itemView.setOnLongClickListener(newView.OnLongClickListener() {
@OverridepublicbooleanonLongClick(View v) {intpos = holder.getLayoutPosition();//得到当前点击item的位置posmListener.ItemLongClickListener(holder.itemView,pos);//把事件交给我们实现的接口那里处理returntrue;
}
});
}
}
@OverridepublicintgetItemCount() {returnlists.size();
}
}classMyViewHolderextendsRecyclerView.ViewHolder{
TextView mTv;publicMyViewHolder(View itemView) {super(itemView);
mTv=(TextView) itemView.findViewById(R.id.textView);
}
}