步骤 – 1 – 在 MainActivity 布局中添加 Recyclerview



    

Step – 2 – For our Card Here We Create New Resource File – layout -> new -> LayoutResourceFile -> New Layout Name / 这里我给card_layout



    

        
        
    

Step – 3 – 现在我们必须为 Model/Item 创建一个类,这里​​我创建一个名为 CourseModel 的新类

public class CourseModel {
    private String course_name;
    private int course_image;

    // Constructor
    public CourseModel(String course_name, int course_image) {
        this.course_name = course_name;
        this.course_image = course_image;
    }
    public String getCourse_name() {
        return course_name;
    }
    public void setCourse_name(String course_name) {
        this.course_name = course_name;
    }
    public int getCourse_image() {
        return course_image;
    }
    public void setCourse_image(int course_image) {
        this.course_image = course_image;
    }
}

Step – 4 – 这里我们必须创建一个新的类作为一个适配器,在这里我命名为 CourseAdapter

public class CourseAdapter extends RecyclerView.Adapter {
    private Context context;
    private ArrayList courseModelArrayList;
    // i3 create interface variable & add in constructor & solve main activity error by pass this in new CourseAdapter
    OnItemClickListener onItemClickListener;
    CourseModel item;
    public CourseAdapter(Context context, ArrayList courseModelArrayList, OnItemClickListener onItemClickListener) {
        this.context = context;

        this.courseModelArrayList = courseModelArrayList;
        this.onItemClickListener = onItemClickListener;
    }
    @NonNull
    @Override
    public CourseAdapter.Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout, parent, false);
        return new Viewholder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull CourseAdapter.Viewholder holder, int position) {
        CourseModel model = courseModelArrayList.get(position);
        holder.courseNameTV.setText(model.getCourse_name());
        holder.courseIV.setImageResource(model.getCourse_image());
        // can also set click event from Adapter class
        /*holder.courseIV.setOnClickListener(v -> {
            Toast.makeText(context, " -> "+model.getCourse_name(), Toast.LENGTH_SHORT).show();
        });*/
    }
    @Override
    public int getItemCount() {
        return courseModelArrayList.size();
    }
    public class Viewholder extends RecyclerView.ViewHolder {
        private ImageView courseIV;
        private TextView courseNameTV;
        public Viewholder(@NonNull View itemView) {

图片[1]-唐朝资源网

super(itemView); courseIV = itemView.findViewById(R.id.idIVCourseImage); courseNameTV = itemView.findViewById(R.id.idTVCourseName); // i5 itemView.setOnClickListener(view -> { onItemClickListener.onItemClick(item,getAdapterPosition()); notifyDataSetChanged(); }); } } }

步骤 – 5 – 现在让我们在这里创建接口,我通过名称 OnItemClickListener 创建接口

// I1
public interface OnItemClickListener {
    void onItemClick(CourseModel item, int position);
    void onLongItemClick(CourseModel item, int position);
}

步骤 – 6 – 在这最后一步中,在 ItemList 中的主活动中应用以下代码,您可以放置​​ Drawble 的图像

// i2 implement interface & solve error
public class MainActivity extends AppCompatActivity implements OnItemClickListener{
    private RecyclerView courseRV;
    private ArrayList courseItemList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        courseRV = findViewById(R.id.idRVCourse);

        itemList();
        
        // i4
        CourseAdapter courseAdapter = new CourseAdapter(this, courseItemList,this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        courseRV.setLayoutManager(linearLayoutManager);
        courseRV.setAdapter(courseAdapter);
    }
    private void itemList() {
        courseItemList = new ArrayList();
        courseItemList.add(new CourseModel("C",  R.drawable.ic_launcher_background));
        courseItemList.add(new CourseModel("C++",  R.drawable.ic_launcher_background));
        courseItemList.add(new CourseModel("Java",  R.drawable.ic_launcher_background));
        courseItemList.add(new CourseModel("Android",  R.drawable.ic_launcher_background));
        courseItemList.add(new CourseModel("Flutter", R.drawable.ic_launcher_background));
        courseItemList.add(new CourseModel("HTML",  R.drawable.ic_launcher_background));
        courseItemList.add(new CourseModel("CSS",  R.drawable.ic_launcher_background));
    }
    // i6
    @Override
    public void onItemClick(CourseModel item, int position) {
        Toast.makeText(this, ""+position+"n"+courseItemList.get(position).getCourse_name(), Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onLongItemClick(CourseModel item, int position) {
    }
}

© 版权声明
THE END
喜欢就支持一下吧
点赞165 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片