Adapter绑定视图
class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> 该语句调用的自定义的 ViewHolder ;自定义的 ViewHolder 放在该类中
class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
// 一个集合的数据
private List<Student> students;
// 生成的构造方法
StudentAdapter(List<Student> students) {
this.students = students;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// 视图绑定 item_student
ItemStudentBinding binding = ItemStudentBinding.inflate(LayoutInflater.from(parent.getContext()),parent,false);
return new ViewHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final Student student = students.get(position);
holder.binding.tvName.setText(student.getName());
holder.binding.tvClassmate.setText(student.getClassmate());
holder.binding.tvAge.setText(String.valueOf(student.getAge()));
}
@Override
public int getItemCount() {
return students.size();
}
//自定义ViewHolder类
public class ViewHolder extends RecyclerView.ViewHolder{
ItemStudentBinding binding;
public ViewHolder(@NonNull ItemStudentBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
}
// 关联的时候这么拿
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// 不关联的时候这么拿
ItemStudentBinding binding = ItemStudentBinding.inflate(LayoutInflater.from(parent.getContext()),parent,false);
return new ViewHolder(binding);
MainActivity加载进StudentAdapter
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
private ToolbarLayoutBinding toolbarBinding;
private List<Student> students;
private StudentAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 关联的时候这么拿
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
toolbarBinding = ToolbarLayoutBinding.bind(binding.getRoot());
setSupportActionBar(toolbarBinding.toolbar);
// 初始化数据
initData();
// 初始化视图
// 1. 视图布局
binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
// 2. 视图分割线
binding.recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
// 创建适配器 Adapter
adapter = new StudentAdapter(students);
binding.recyclerView.setAdapter(adapter);
}
private void initData() {
students = new ArrayList<>();
for(int i = 0;i < 3;i++){
Student stu = new Student("单" + (i + 1),"软件2146",21);
students.add(stu);
}
}
}
新建drawable文件夹下的item_selector.xml文件(为了实现点击时外观的着重显示)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 点击和获得焦点的时候改变颜色-->
<!-- 点击-->
<item android:drawable="@color/darker_gray" android:state_selected="true"/>
<!-- 获得焦点-->
<item android:drawable="@color/darker_gray" android:state_focused="true"/>
<!-- 没有点击也没有获得焦点-->
<item android:drawable="@color/white"/>
</selector>
对鼠标点击样式加重进行设置
class StudentAdapter extends RecyclerView.Adapter<ViewHolder> {
// 一个集合的数据
private List<Student> students;
// 为添加选中加重样式服务
private int currentIndex = 0;
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final Student student = students.get(position);
holder.binding.tvName.setText(student.getName());
holder.binding.tvClassmate.setText(student.getClassmate());
holder.binding.tvAge.setText(String.valueOf(student.getAge()));
// 设置 条目信息 鼠标点击时样式加重
holder.itemView.setSelected(currentIndex == position);
}
}
自定义添加点击事件(不内置需要自己加)
class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
// 一个集合的数据
private List<Student> students;
// 为添加选中加重样式服务
private int currentIndex = 0;
// 点击事件
private View.OnClickListener onClickListener;
// 生成的构造方法
StudentAdapter(List<Student> students) {
this.students = students;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// 视图绑定 item_student
ItemStudentBinding binding = ItemStudentBinding.inflate(LayoutInflater.from(parent.getContext()),parent,false);
return new ViewHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final Student student = students.get(position);
holder.binding.tvName.setText(student.getName());
holder.binding.tvClassmate.setText(student.getClassmate());
holder.binding.tvAge.setText(String.valueOf(student.getAge()));
// 设置 条目信息 鼠标点击时样式加重
holder.itemView.setSelected(currentIndex == position);
}
@Override
public int getItemCount() {
return students.size();
}
public View.OnClickListener getOnClickListener() {
return onClickListener;
}
public void setOnClickListener(View.OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
public void setCurrentIndex(int position){
// 更新视图
notifyItemChanged(currentIndex);
notifyItemChanged(position);
// 设置当前选中项索引
this.currentIndex = position;
}
//自定义ViewHolder类
public class ViewHolder extends RecyclerView.ViewHolder{
ItemStudentBinding binding;
public ViewHolder(@NonNull ItemStudentBinding binding) {
super(binding.getRoot());
this.binding = binding;
this.itemView.setTag(this);
this.itemView.setOnClickListener(onClickListener);
}
}
}
public class MainActivity extends AppCompatActivity {
// 自定义的点击事件
adapter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StudentAdapter.ViewHolder viewHolder = (StudentAdapter.ViewHolder) view.getTag();
final int position = viewHolder.getAdapterPosition();
adapter.setCurrentIndex(position);
currentStudednt = students.get(position);
final Intent intent = new Intent(MainActivity.this,StudentUpdateActivity.class);
final Bundle bundle = new Bundle();
bundle.putSerializable("student",currentStudednt);
intent.putExtras(bundle);
launcher.launch(intent);
}
});
binding.recyclerView.setAdapter(adapter);
}
点击列表项进入新建或修改
包含数据传递(activity之间)、数据接收等知识点
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
private ToolbarLayoutBinding toolbarBinding;
private List<Student> students;
private StudentAdapter adapter;
private Student currentStudednt;
private ActivityResultLauncher<Intent> launcher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
//
Toast.makeText(MainActivity.this, "修改后返回的结果", Toast.LENGTH_SHORT).show();
}
}
);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 关联的时候这么拿
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
toolbarBinding = ToolbarLayoutBinding.bind(binding.getRoot());
setSupportActionBar(toolbarBinding.toolbar);
// 初始化数据
initData();
// 初始化视图
// 1. 视图布局
binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
// 2. 视图分割线
binding.recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
// 创建适配器 Adapter
adapter = new StudentAdapter(students);
binding.recyclerView.setAdapter(adapter);
// 自定义的点击事件
adapter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StudentAdapter.ViewHolder viewHolder = (StudentAdapter.ViewHolder) view.getTag();
final int position = viewHolder.getAdapterPosition();
adapter.setCurrentIndex(position);
currentStudednt = students.get(position);
final Intent intent = new Intent(MainActivity.this,StudentUpdateActivity.class);
final Bundle bundle = new Bundle();
bundle.putSerializable("student",currentStudednt);
intent.putExtras(bundle);
launcher.launch(intent);
}
});
binding.recyclerView.setAdapter(adapter);
}
private void initData() {
students = new ArrayList<>();
for(int i = 0;i < 3;i++){
Student stu = new Student("单" + (i + 1),"软件2146",21);
students.add(stu);
}
}
// 显示页面顶部菜单项
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_student,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
return super.onOptionsItemSelected(item);
}
}
public class StudentUpdateActivity extends AppCompatActivity {
ActivityStudentUpdateBinding binding;
private Student currentStudent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityStudentUpdateBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// 获取主页面传来的数据
final Intent intent = getIntent();
final Bundle bundle = intent.getExtras();
if (bundle != null){
currentStudent = (Student) bundle.get("student");
}
// currentStudent 不为空修改,为空添加
if (currentStudent != null){
// 姓名和年龄显示
binding.etName.setText(currentStudent.getName());
binding.etAge.setText(String.valueOf(currentStudent.getAge()));
// 列表显示(较难)
SpinnerAdapter spinnerAdapter = binding.spClassmate.getAdapter();
for (int i = 0;i < spinnerAdapter.getCount();i++){
if (spinnerAdapter.getItem(i).toString().equals(currentStudent.getClassmate())) {
binding.spClassmate.setSelection(i);
break;
}
}
}
binding.btnConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Student(binding.etName.getText().toString(),
binding.spClassmate.getSelectedItem().toString(),
Integer.parseInt((binding.etAge.getText().toString())));
if (currentStudent != null){
// 更新到数据库
}else {
// 插入数据库
}
Intent intent = new Intent();
setResult(RESULT_OK,new Intent());
finish();
}
});
}
}
对应的.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".StudentUpdateActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="姓名"
android:textSize="20sp" />
<EditText
android:id="@+id/et_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="班级"
android:layout_marginEnd="16dp"
android:textSize="20sp" />
<Spinner
android:id="@+id/sp_classmate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="@array/student_arr"
android:spinnerMode="dropdown"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="年龄"
android:textSize="20sp" />
<EditText
android:id="@+id/et_age"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_confirm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="8dp"
android:text="确 定"
android:textSize="20sp" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取 消"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
因为Spinner 标签内的 android:entries="@array/student_arr" 语句,修改了string.xml文件
string.xml文件
<string-array name="student_arr" >
<item>软件2148</item>
<item>软件2148</item>
<item>软件2148</item>
<item>软件2148</item>
<item>软件2148</item>
<item>软件2148</item>
<item>软件2148</item>
</string-array>