美文网首页
Glide在Adapter中怎么用

Glide在Adapter中怎么用

作者: 今天也要努力呀y | 来源:发表于2019-07-23 20:35 被阅读0次

    首先加上依赖
    implementation 'com.github.bumptech.glide:glide:4.8.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'

    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
            final Student student = studentList.get(i);
            Glide.with(context).load(student.getImagePath()).into(viewHolder.student_picture);
            viewHolder.stusent_major.setText(student.getMajor());
            viewHolder.student_name.setText(student.getName());
            viewHolder.student_id.setText(student.getStudent_number());
            viewHolder.student_grade.setText(student.getGrade());
            viewHolder.detail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                //Toast.makeText(v.getContext(),"你点击了第"+i+"个按钮",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(context, ShowStudent.class);
                    intent.putExtra("position",i);
                    intent.putExtra("path",student.getImagePath());
                    context.startActivity(intent);
                }
            });
    
        }
    

    顺便写一个Adapter的示例:

    public class list_Adapter extends RecyclerView.Adapter<list_Adapter.ViewHolder> {
        private List<Student> studentList = new ArrayList<>();
        Context context;
    
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_list_item, viewGroup, false);
            return new ViewHolder(view);
        }
    
        public list_Adapter(List<Student> students,Context context) {
            this.studentList = students;
            this.context = context;
        }
    
        @SuppressLint("CheckResult")
        @Override
        public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
            final Student student = studentList.get(i);
            Glide.with(context).load(student.getImagePath()).into(viewHolder.student_picture);
            viewHolder.stusent_major.setText(student.getMajor());
            viewHolder.student_name.setText(student.getName());
            viewHolder.student_id.setText(student.getStudent_number());
            viewHolder.student_grade.setText(student.getGrade());
            viewHolder.detail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                //Toast.makeText(v.getContext(),"你点击了第"+i+"个按钮",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(context, ShowStudent.class);
                    intent.putExtra("position",i);
                    intent.putExtra("path",student.getImagePath());
                    context.startActivity(intent);
                }
            });
    
        }
    
        @Override
        public int getItemCount() {
            return studentList.size();
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView student_name;
            TextView student_id;
            TextView student_grade;
            TextView stusent_major;
            ImageView student_picture;
            LinearLayout detail;
    
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                student_grade = itemView.findViewById(R.id.student_grade);
                student_id = itemView.findViewById(R.id.student_id);
                student_name = itemView.findViewById(R.id.student_name);
                stusent_major = itemView.findViewById(R.id.student_major);
                student_picture = itemView.findViewById(R.id.student_image);
                detail = itemView.findViewById(R.id.detail);
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Glide在Adapter中怎么用

          本文链接:https://www.haomeiwen.com/subject/hpqzlctx.html