上一个的实验室Issue前几天才完成,两个项目都实现了老师要求,算上中途多次放下去做其他issue,这个issue做了差不多有四个月了吧。还好不是改bug,不然四个月时间足够被批评n次了。趁着现在还有印象,把这个issue里最难搞的问题总结一下,之前做的过程也总结了一些遇到的问题,比如后台多个定时器同事运行的问题,settings界面的注意事项等都总结了,这次总结一下花费最多时间去想的问题,中途还经过多次推倒重来,也不是说这个功能多难实现,主要是在几年前的项目上添加新功能,有些吃力,毕竟好多方法都被调用过,不能随意改变,尤其是数据封装好的类型很难去改变,想要找到一个好的方法挺难的,只能找一些折中的方法。这篇博文讲解的是我自己想的一种方法,可能不是最好的,但是适合我的项目,如果有比较好的方法,欢迎一起讨论。
有时候需要对一些数据进行分类显示,还要显示他们的分类标题,普通的GridView是挺难实现的,因为GridView不支持不同行自定义列数,例如我想标题占满一行,其他子项每两个占一行。GridView也可以实现,需要对GridView的方法做大量地重写override,重绘item等等,对一些像我这种菜鸟级别的人来说,门槛太高。后来发现可以使用RecyclerView实现这种功能,RecyclerView支持自定义不同列数。接下来介绍一下如何使用。
- 先看一下做的项目的效果图
解释都写在代码里,直接看代码吧:
- 先分别新建一个title的layout文件和一个item的layout文件
recyclerview_title.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="标题"/>
</LinearLayout>
recyclerview_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/image"
android:scaleType="fitXY"
android:alpha="0.7"
android:src="@drawable/ic_launcher_background"/>
<TextView
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"