今天讲述团队项目第一阶段中对recyclerview的适配器配置,
package com.example.bowenwang; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; import java.util.List; public class RecyclerAdapter2 extends RecyclerView.Adapter<RecyclerAdapter2.Hoder> { List<Bean> mlist; private Context mContext; public RecyclerAdapter2(List<Bean> mlist) { this.mlist = mlist; } @NonNull @Override public Hoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { mContext=parent.getContext(); LayoutInflater inflater = LayoutInflater.from(parent.getContext()); View view=inflater.inflate(R.layout.list_item,parent,false); Hoder hoder=new Hoder(view); return hoder; } @Override public void onBindViewHolder(@NonNull Hoder holder, int position) { holder.title.setText(mlist.get(position).getTitle()); holder.time.setText(mlist.get(position).getTime()); holder.place.setText(mlist.get(position).getPlace()); String img=mlist.get(position).getImg(); if(img.equals("无")){ Glide.with(mContext).load("http://n.sinaimg.cn/default/feedbackpics/transform/116/w550h366/20180326/gx3A-fysqfnf9556450.png").into(holder.img); } else { Glide.with(mContext).load(img.trim()).into(holder.img); } int p=position; holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Bean bean=new Bean(); bean.setTitle(mlist.get(p).getTitle()); bean.setTime(mlist.get(p).getTime()); bean.setPlace(mlist.get(p).getPlace()); bean.setContent(mlist.get(p).getContent()); Toast.makeText(mContext,bean.getTitle(),Toast.LENGTH_SHORT).show(); Intent iti=new Intent(mContext,DetailActivity.class); Bundle bundle = new Bundle(); bundle.putSerializable("bean",bean); iti.putExtras(bundle); mContext.startActivity(iti); } }); } @Override public int getItemCount() { return mlist.size(); } class Hoder extends RecyclerView.ViewHolder{ TextView title; TextView time; TextView place; ImageView img; public Hoder(@NonNull View itemView) { super(itemView); title=itemView.findViewById(R.id.tv_title); time=itemView.findViewById(R.id.tv_time); place=itemView.findViewById(R.id.tv_place); img=itemView.findViewById(R.id.iv_portrait); } } }
这里是我的团队项目中的适配器,可以看到后需要配置一些什么。