初识Recyclerview的小Demo,下面是代码:
public classMainActivityextendsAppCompatActivity{
private RecyclerView rv;
@Override
protected voidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
rv=newRecyclerView(this);
setContentView(rv);
/**
*这是ListView的效果
*/
rv.setLayoutManager(newLinearLayoutManager(this));
rv.setAdapter(newRecyclerView.Adapter() {
class ViewHolder extends RecyclerView.ViewHolder{
private TextView tv;
public ViewHolder(TextViewitemView) {
super(itemView);
tv=itemView;
}
publicTextViewgetTv() {
returntv;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroupparent,intviewType) {
return new ViewHolder(newTextView(parent.getContext()));
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolderholder,intposition) {
ViewHolder vh=(ViewHolder)holder;
vh.getTv().setText("初识RecyclerView"+position);
// vh.getTv().setText(data[position]);
}
//获取RecyclerView子对象的数量
@Override
public int getItemCount() {
return 100;
// return data.length;
}
//数组
private String[] data=new String[]{"TOM","JACK","ROSE"};
});
}
}