1.导包 implementation ‘com.android.support:recyclerview-v7:28.0.0’
2.创建布局
4.得到数据后请设置Adapter
///单条目
4.1 创建Adapter 类 让他继承与RecyclerView.Adapter
4.2 其他详情如下:
public class Adapter extends RecyclerView.Adapter {
private ArrayList list=new ArrayList<>();
private Context context;
public Adapter(ArrayList list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item, null);
//这里的 holder不用踢出去 如下:
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
//这里需要哪个ViewHolder 就强制转换成 哪个ViewHolder
ViewHolder holder = (ViewHolder)viewHolder;
holder.name1.setText(list.get(i).getTitle());
holder.name2.setText(list.get(i).getPrice());
holder.name3.setText(list.get(i).getTime());
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private final TextView name2;
private final TextView name3;
private final TextView name1;
public ViewHolder(@NonNull View itemView) {
super(itemView);
//寻找插件
name1 = itemView.findViewById(R.id.name1);
name2 = itemView.findViewById(R.id.name2);
name3 = itemView.findViewById(R.id.name3);
}
}
}
-
///多条目
public class Adapter extends RecyclerView.Adapter {
private ArrayList list=new ArrayList<>();
private Context context;
public Adapter(ArrayList list, Context context) {
this.list = list;
this.context = context;
}
//1.有几个条目要展示 就写几个ViewHolder类 每个ViewHolder类要继承RecyclerView.ViewHolder
//2.展示多条目得写一个方法 getItemViewType
//3.getItemViewType的使用:你想展示几个条目就在getItemViewType中 retrun position%几;
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (getItemViewType(i)==0){
View view = LayoutInflater.from(context).inflate(R.layout.item, null);
//这里的holder不准踢出去
ViewHolder holder = new ViewHolder(view);
return holder;
}
else {
View view = LayoutInflater.from(context).inflate(R.layout.item1, null);
//这里的holder不准踢出去
ViewHolder1 holder1 = new ViewHolder1(view);
return holder1;
}
}@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
if (getItemViewType(i)==0){
//这里需要哪个ViewHolder 就强制转换成 哪个ViewHolder
ViewHolder holder=(ViewHolder) viewHolder;
holder.name1.setText(list.get(i).getTitle());
holder.name2.setText(list.get(i).getPrice());
holder.name3.setText(list.get(i).getTime());
}else{
//这里需要哪个ViewHolder 就强制转换成 哪个ViewHolder
ViewHolder1 holder=(ViewHolder1) viewHolder;
holder1.name1.setText(list.get(i).getTitle());
holder1.name2.setText(list.get(i).getPrice());
holder1.name3.setText(list.get(i).getTime());
}
}
@Override
public int getItemViewType(int position) {
return position%2;
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private final TextView name2;
private final TextView name3;
private final TextView name1;
public ViewHolder(@NonNull View itemView) {
super(itemView);
name1 = itemView.findViewById(R.id.name1);
name2 = itemView.findViewById(R.id.name2);
name3 = itemView.findViewById(R.id.name3);
}
}
public class ViewHolder1 extends RecyclerView.ViewHolder{
private final TextView name2;
private final TextView name3;
private final TextView name1;
public ViewHolder1(@NonNull View itemView) {
super(itemView);
name1 = itemView.findViewById(R.id.name4);
name2 = itemView.findViewById(R.id.name5);
name3 = itemView.findViewById(R.id.name6);
}
}
}