Android Material Design越来越流行,以前很常用的 ListView 现在也用RecyclerView代替了,实现原理还是相似的,感兴趣的小伙伴们可以参考一下
本文实例实现一下 RecyclerView,代码比较简单,适合初学者,如有错误,欢迎指出。
复习 ListView
了解关于ListView 的基础知识。
实现过程中需要复写BaseAdapter,主要是这4个方法
- public int getCount() :适配器中数据集中 数据的个数,即ListView需要显示的数据个数
- public Object getItem(int position) : 获取数据集中与指定索引对应的数据项
- public long getItemId(int position) : 获取指定行对应的ID
-
public View getView(int position, View convertView, ViewGroup parent) :获取每一个Item的显示内容
一般 ListView 每一项都是相同的布局,若想各个项实现不同的布局,可复写 getItemViewType和getViewTypeCount实现
RecyclerView 实现1、xml 布局
下面是RecyclerView中每一项的布局 layout下面的item_article_type_1.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://www.easck.com/apk/res/android" xmlns:card_view="http://www.easck.com/apk/res-auto" xmlns:app="http://www.easck.com/apk/res-auto" xmlns:fresco="http://www.easck.com/apk/res-auto" android:id="@+id/cv_item" android:layout_width="match_parent" android:layout_height="wrap_content" android:foreground="?android:attr/selectableItemBackground" app:cardCornerRadius="5dp" app:cardElevation="5dp" app:contentPadding="2dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/rcv_article_photo" android:layout_width="100dp" android:layout_height="100dp" android:layout_centerVertical="true" fresco:actualImageScaleType="centerInside" fresco:roundAsCircle="true" fresco:roundingBorderColor="@color/lightslategray" fresco:roundingBorderWidth="1dp" /> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/rcv_article_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="2dp" android:gravity="center" android:text="关于举办《经典音乐作品欣赏与人文审美》讲座的通知" android:textColor="@color/primary_text" /> <!-- 新闻 发布时间 来源 阅读次数--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/rcv_article_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="2dp" android:text="2015-01-09" /> <TextView android:id="@+id/rcv_article_source" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:text="科学研究院" /> <TextView android:id="@+id/rcv_article_readtimes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:text="1129次" /> </LinearLayout> <TextView android:id="@+id/rcv_article_preview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="5dp" android:ellipsize="end" android:maxLines="2" android:text="讲座主要内容:以中、西方音乐历史中经典音乐作品为基础,通过作曲家及作品创作背景、相关音乐文化史知识及音乐欣赏常识..." /> </LinearLayout> </LinearLayout> </android.support.v7.widget.CardView>










