RecycleView的学习笔记
本篇内容是学习笔记,不代表是最好的方案,不保证所有方法的理解正确。
欢迎各位大佬指正,优化。
学习假想环境是一个冰箱保存内容的管理App。
数据:食品的名称,放入时间,到期时间。如果已经过期,标记为Bad。
一、创建画面
在activity的xml文件中放入RecycleView控件。
红色框的部分是RecycleView
代码中包括两个部分,TextView是Title,RecycleView有多个版本,此处使用androidx的版本。
<LinearLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/home_things_list_title"
android:textSize="24sp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/home_things_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
二、创建RecycleView的条目布局
下图中包含五个元素,第1和4元素是image,用于显示图片。第2元素是TextView。第3和5元素是EditText。
源代码如下:
<?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"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|start">
<ImageView
android:id="@+id/things_list_status"
android:layout_width="@dimen/home_things_list_item_image_height"
android:layout_height="@dimen/home_things_list_item_height"
android:contentDescription="@string/home_things_list_item_status"
app:srcCompat="@mipmap/good" />
<TextView
android:id="@+id/things_list_name"
style="@android:style/Widget.Material.TextView"
android:layout_width="wrap_content"
android:layout_height="@dimen/home_things_list_item_height"
android:text="@string/home_things_list_item_name"
android:textColor