-
首先extends RecyclerView.Adapter<ViewHolder>
-
编写构造函数(一般需要语境context以及数据list)
public HoleAdapter(Context hole_context, List<post> list) {
context = hole_context;
post_list = list;
}
-
再编写自己的myHolderr类,用来获取item对象(需要先创建item_XXX.xml文件)
public class myHolder extends RecyclerView.ViewHolder {
TextView post_text;
public myHolder(@NonNull View itemView) {
super(itemView);
post_text = itemView.findViewById(R.id.item_tv);
}
}
-
将RecyclerView.Adapter<ViewHolder>
改为RecyclerView.Adapter<myAdapter.myHolder>
-
重写函数onCreateViewHolder()
(绑定item视图)
@NonNull
@Override
public myHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hole, parent, false);
myHolder myHolder = new myHolder(view);
return myHolder;
}
-
重写函数onBindViewHolder()
(绑定item数据)
@Override
public void onBindViewHolder(@NonNull myHolder holder, int position) {
post post = post_list.get(position);
holder.post_text.setText(post.post_context);
}
-
重写函数getItemCount()
(返回item个数)
@Override
public int getItemCount() {
return post_list.size();
}
至此Recycler的设置完成