public class ShowRecyclerAdapter extends RecyclerView.Adapter<ShowRecyclerAdapter.RecyclerViewHolder>{
private Context mContext;
private List<ShowData.DataBean> data;
public ShowRecyclerAdapter(Context context, List<ShowData.DataBean> data) {
mContext = context;
this.data = data;
}
@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view=LayoutInflater.from(mContext).inflate(R.layout.recycler_item,viewGroup,false);
RecyclerViewHolder holder=new RecyclerViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder recyclerViewHolder, int i) {
recyclerViewHolder.showData.setText(data.get(i).getTitle());
RecyclerView.LayoutParams params= (RecyclerView.LayoutParams) recyclerViewHolder.itemView.getLayoutParams();
params.height= (int) (200+Math.random()*400);
Picasso.with(mContext).load(data.get(i).getThumbnail_pic_s()).into(recyclerViewHolder.showImage);
recyclerViewHolder.itemView.setLayoutParams(params);
}
@Override
public int getItemCount() {
return data.size();
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView showImage;
TextView showData;
public RecyclerViewHolder(@NonNull View itemView) {
super(itemView);
showData=itemView.findViewById(R.id.model_data);
showImage=itemView.findViewById(R.id.model_image);
showData.setOnClickListener(this);
showImage.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int position = getPosition();
mOnItemClickListener.onClick(v,position);
}
}
OnItemClickListener mOnItemClickListener;
public interface OnItemClickListener{
void onClick(View view,int postion);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
mOnItemClickListener = onItemClickListener;
}
public void addData(){
ShowData.DataBean dataBean=new ShowData.DataBean();
dataBean.setTitle("呵呵");
dataBean.setThumbnail_pic_s("http://thirdqq.qlogo.cn/qqapp/100424468/B853A0A0AC2A47327FE11D0A5E08D1D9/100");
data.add(0,dataBean);
notifyItemInserted(0);
}
public void delData(int postion){
postion=postion-1;
data.remove(postion);
notifyItemRemoved(postion);
}
}
网友评论