• ViewBinding 初探-在Activity和Adapter中使用


    升级android studio后(主要是gradle升级后),butterknife 不再被支持;

    现在google主推ViewBinding 

    一下就是我用ViewBinding 做的一个小demo,展示ViewBinding 的使用

    一、build.gradle配置

            1、ViewBinding

    1. android {
    2. ……
    3. buildFeatures {
    4. viewBinding = true
    5. }
    6. }

            2、dependencies 相关配置

            

    1. dependencies {
    2. ……
    3. //RecyclerView 列表
    4. implementation 'androidx.recyclerview:recyclerview:1.2.0-beta01'
    5. }

    二、界面展示

    三、activity代码

            1、activity_main.xml  代码

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:app="http://schemas.android.com/apk/res-auto"
    3. xmlns:tools="http://schemas.android.com/tools"
    4. android:layout_width="match_parent"
    5. android:layout_height="match_parent"
    6. android:orientation="vertical"
    7. tools:context=".MainActivity">
    8. <LinearLayout
    9. android:layout_width="match_parent"
    10. android:layout_height="wrap_content"
    11. android:orientation="vertical">
    12. <EditText
    13. android:id="@+id/etName"
    14. android:layout_width="match_parent"
    15. android:layout_height="wrap_content"
    16. android:textColor="#000000"
    17. android:textSize="18sp"
    18. android:hint="名称"/>
    19. <EditText
    20. android:id="@+id/etAge"
    21. android:layout_width="match_parent"
    22. android:layout_height="wrap_content"
    23. android:textColor="#000000"
    24. android:inputType="number"
    25. android:textSize="18sp"
    26. android:hint="年龄"/>
    27. <Button
    28. android:id="@+id/btnAdd"
    29. android:layout_width="match_parent"
    30. android:layout_height="wrap_content"
    31. android:text="添加"/>
    32. LinearLayout>
    33. <TextView
    34. android:layout_width="match_parent"
    35. android:layout_height="40dp"
    36. android:gravity="center|left"
    37. android:paddingLeft="20dp"
    38. android:background="#D9F8F5"
    39. android:textColor="#000000"
    40. android:textSize="18sp"
    41. android:text="数据列表-点击可删除"/>
    42. <androidx.recyclerview.widget.RecyclerView
    43. android:id="@+id/rvData"
    44. android:layout_width="match_parent"
    45. android:layout_height="match_parent"/>
    46. LinearLayout>

            2、MainActivity  代码

    1. import androidx.appcompat.app.AppCompatActivity;
    2. import androidx.recyclerview.widget.GridLayoutManager;
    3. import android.os.Bundle;
    4. import com.xolo.myobjectbox.databinding.ActivityMainBinding;
    5. import java.util.ArrayList;
    6. import java.util.List;
    7. public class MainActivity extends AppCompatActivity {
    8. //系统依据 activity_main.xml 自动生成的 ActivityMainBinding 类,通过 ActivityMainBinding 可以获取对应的控件
    9. ActivityMainBinding mainBinding;
    10. private StudentAdapter studentAdapter;
    11. private List<StudentModel> list;
    12. @Override
    13. protected void onCreate(Bundle savedInstanceState) {
    14. super.onCreate(savedInstanceState);
    15. //setContentView(R.layout.activity_main); -需要注释掉
    16. //加载界面
    17. mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
    18. setContentView(mainBinding.getRoot());
    19. //添加StudentAdapter适配器
    20. list = new ArrayList<>();
    21. studentAdapter = new StudentAdapter(this, list, o -> {
    22. list.remove((int)o);
    23. studentAdapter.notifyDataSetChanged();
    24. });
    25. mainBinding.rvData.setLayoutManager(new GridLayoutManager(this, 1));
    26. mainBinding.rvData.setNestedScrollingEnabled(true);
    27. mainBinding.rvData.setAdapter(studentAdapter);
    28. //添加点击事件
    29. mainBinding.btnAdd.setOnClickListener(v -> {
    30. String name = mainBinding.etName.getText().toString();
    31. String age = mainBinding.etAge.getText().toString();
    32. list.add(0, new StudentModel(name, age));
    33. studentAdapter.notifyDataSetChanged();
    34. });
    35. }
    36. }

    四、adapter代码

            1、adapter_student.xml

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent"
    3. android:layout_height="wrap_content"
    4. android:orientation="vertical"
    5. android:padding="5dp"
    6. android:background="#FFFFFF"
    7. android:layout_marginBottom="1dp"
    8. android:id="@+id/llClick">
    9. <TextView
    10. android:id="@+id/tvName"
    11. android:layout_width="match_parent"
    12. android:layout_height="wrap_content"
    13. android:text="name:"
    14. android:textColor="#000000"
    15. android:textSize="18sp" />
    16. <TextView
    17. android:id="@+id/tvAge"
    18. android:layout_width="match_parent"
    19. android:layout_height="wrap_content"
    20. android:text="age:"
    21. android:textColor="#000000"
    22. android:textSize="18sp" />
    23. LinearLayout>

            2、StudentAdapter

    1. import android.annotation.SuppressLint;
    2. import android.content.Context;
    3. import android.view.LayoutInflater;
    4. import android.view.ViewGroup;
    5. import androidx.annotation.NonNull;
    6. import androidx.recyclerview.widget.RecyclerView;
    7. import com.xolo.myobjectbox.databinding.AdapterStudentBinding;
    8. import java.util.List;
    9. public class StudentAdapter extends RecyclerView.Adapter.ViewHolder> {
    10. private Context context;
    11. private List<StudentModel> list;
    12. private ClickInterface clickInterface;
    13. public StudentAdapter(Context context, List<StudentModel> list, ClickInterface clickInterface) {
    14. this.context = context;
    15. this.list = list;
    16. this.clickInterface = clickInterface;
    17. }
    18. @NonNull
    19. @Override
    20. public StudentAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    21. //界面设置
    22. AdapterStudentBinding studentBinding = AdapterStudentBinding.inflate(LayoutInflater.from(context), parent, false);
    23. return new ViewHolder(studentBinding);
    24. }
    25. @SuppressLint("SetTextI18n")
    26. @Override
    27. public void onBindViewHolder(@NonNull StudentAdapter.ViewHolder holder, int position) {
    28. holder.studentBinding.tvName.setText("姓名:"+list.get(position).getName());
    29. holder.studentBinding.tvAge.setText("年龄:"+list.get(position).getAge());
    30. holder.studentBinding.llClick.setOnClickListener(v -> clickInterface.ClickOnThe(position));
    31. }
    32. @Override
    33. public int getItemCount() {
    34. return list.size();
    35. }
    36. public class ViewHolder extends RecyclerView.ViewHolder {
    37. 系统依据 activity_main.xml 自动生成的 AdapterStudentBinding 类,通过 ActivityMainBinding 可以获取对应的控件
    38. AdapterStudentBinding studentBinding;
    39. public ViewHolder(@NonNull AdapterStudentBinding itemView) {
    40. super(itemView.getRoot());
    41. studentBinding = itemView;
    42. }
    43. }
    44. }

    五、Interface代码

    1. public interface ClickInterface {
    2. void ClickOnThe(Object o);
    3. }

    六、Model代码

    1. public class StudentModel {
    2. private String name;
    3. private String age;
    4. public StudentModel() {
    5. }
    6. public StudentModel(String name, String age) {
    7. this.name = name;
    8. this.age = age;
    9. }
    10. public String getName() {
    11. return name;
    12. }
    13. public void setName(String name) {
    14. this.name = name;
    15. }
    16. public String getAge() {
    17. return age;
    18. }
    19. public void setAge(String age) {
    20. this.age = age;
    21. }
    22. }

    七、代码界面展示

  • 相关阅读:
    神经网络与深度学习(三):循环神经网络网络
    使用 Ant Design Vue 你可能会遇到的14个问题
    VS Code Java 3月更新|代码补全、Maven 以及 Java 插件预览版本新升级!
    算法总结-最短距离和问题
    壳聚糖-聚乙二醇-罗丹明B,RhodamineB-PEG-Chitosan
    【LeetCode】【简单】【2】20. 有效的括号
    java基础 集合(2) Set接口
    极端业务场景下,我们应该如何做好稳定性保障?
    devops-5:从0开始构建一条完成的CI CD流水线
    4.4——数据库和前后端身份认证
  • 原文地址:https://blog.csdn.net/weixin_41454168/article/details/126279414