1. Implement thư viện:
- Sử dụng thư viện của Material: https://material.io/develop/android/docs/getting-started/
- Vào Gradle Scripts -> build.gradle (Module:app)
- Thêm vào trong dependencies {} dòng sau:
1 |
implementation 'com.google.android.material:material:+' |
2. Tạo Class Student (folder: app/java/com.example.test)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.example.projectname; public class Student { private String name; private Double point; public Student(String name, Double point) { this.name = name; this.point = point; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getPoint() { return point; } public void setPoint(Double point) { this.point = point; } } |
3. Tạo file item_layout.xml (folder: app/res/layout)
- Cần nhớ là: layout_height="wrap_content" nhá.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:padding="20dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24sp" android:layout_weight="1" android:text="Name"/> <TextView android:id="@+id/tv_point" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24sp" android:text="Point"/> </LinearLayout> |
4. Sửa file activity_main.xml (folder: app/res/layout)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rv_list_student" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> </androidx.recyclerview.widget.RecyclerView> </LinearLayout> |
5. Tạo Class StudentAdapter (folder: app/java/com.example.nameproject)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package com.example.test; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.StudentHolder> { private ArrayList<Student> studentArrayList; LayoutInflater inflater; public StudentAdapter(LayoutInflater inflater) { this.inflater = inflater; } public void setStudentArrayList(ArrayList<Student> studentArrayList) { this.studentArrayList = studentArrayList; notifyDataSetChanged(); } @NonNull @Override public StudentHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View studentView = inflater.inflate(R.layout.item_layout,parent,false); return new StudentHolder(studentView); } @Override public void onBindViewHolder(@NonNull StudentHolder holder, int position) { holder.bindView(studentArrayList.get(position)); } @Override public int getItemCount() { return studentArrayList == null ? 0 : studentArrayList.size(); } public class StudentHolder extends RecyclerView.ViewHolder { private TextView tvName; private TextView tvPoint; public StudentHolder(@NonNull View itemView) { super(itemView); tvName = itemView.findViewById(R.id.tv_name); tvPoint = itemView.findViewById(R.id.tv_point); } public void bindView(Student item){ tvName.setText(item.getName()); tvPoint.setText(item.getPoint()+""); } } } |
6. Sửa Class MainActivity (folder: app/java/com.example.nameproject)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.example.test; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private RecyclerView rvStudent; private StudentAdapter studentAdapter; private ArrayList<Student> studentList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { rvStudent = findViewById(R.id.rv_list_student); studentAdapter = new StudentAdapter(getLayoutInflater()); rvStudent.setAdapter(studentAdapter); for (int i=0;i<100;i++){ arrStudent.add(new Student("Duy",9.8)); arrStudent.add(new Student("Trang",9.5)); arrStudent.add(new Student("Dương",10.0)); } studentAdapter.setStudentArrayList(studentList); } } |