Android 数据展示 demo 实现流程
1. 准备工作
首先,我们需要创建一个新的 Android 项目。打开 Android Studio,点击 "Start a new Android Studio project",然后按照向导创建一个新的项目。选择合适的项目名称和位置,以及最低支持的 Android 版本。
2. 设计数据结构
在开始编写代码之前,我们需要先设计好数据结构来存储要展示的数据。在本示例中,我们假设要展示一个简单的学生列表,每个学生包含姓名和年龄两个属性。我们可以创建一个 Student
类来表示每个学生的信息。
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
3. 创建布局文件
接下来,我们需要创建一个布局文件来展示学生列表。在 res/layout 目录下创建一个新的 XML 文件,命名为 activity_main.xml
。在布局文件中,我们可以使用 RecyclerView 来展示学生列表。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="
xmlns:app="
xmlns:tools="
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_student" />
</androidx.constraintlayout.widget.ConstraintLayout>
4. 创建列表项布局
我们还需要创建一个用于学生列表项的布局文件。在 res/layout 目录下创建一个新的 XML 文件,命名为 item_student.xml
。在布局文件中,我们可以使用 TextView 来显示学生姓名和年龄。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView
android:id="@+id/textAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@android:color/darker_gray" />
</LinearLayout>
5. 创建适配器类
我们需要创建一个适配器类,用于将数据绑定到 RecyclerView 上。在项目中创建一个新的 Java 类,命名为 StudentAdapter
。适配器继承自 RecyclerView.Adapter
,并实现必要的方法。
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
private List<Student> students;
public StudentAdapter(List<Student> students) {
this.students = students;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_student, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Student student = students.get(position);
holder.textName.setText(student.getName());
holder.textAge.setText(String.valueOf(student.getAge()));
}
@Override
public int getItemCount() {
return students.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView textName;
public TextView textAge;
public ViewHolder(View itemView) {
super(itemView);
textName = itemView.findViewById(R.id.textName);
textAge = itemView.findViewById(R.id.textAge);
}
}
}
6. 实例化 RecyclerView
在 MainActivity 的 onCreate 方法中,我们需要实例化 RecyclerView,并将适配器设置给它。
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private StudentAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
List<Student> students = new ArrayList<>();
students.add(new Student("John Doe", 20));
students.add(new Student("Jane Smith", 22));
students.add