学习Android Material Design(RecyclerView代替ListView)

2019-12-10 19:15:24王旭

布局思路就是 CardView里面嵌入了一个LinearLayout。图片部分用固定宽度100dp,文字部分利用android:layout_weight=”1”占据了其他部分。

TextView利用android:gravity=”center”使得标题的文字居中。

LinearLayout里面利用android:gravity=”center”使得“2015-01-09 科学研究院 1129次”居中,

新闻详情内容的TextView利用

android:maxLines="2"
android:ellipsize="end"

将文章内容限定为2行,超出部分用省略号显示。

使用fresco这儿有个坑需要注意,请移步这篇文章

Android 之 Fresco 显示圆形图片 之坑

预览效果 

学习Android Material Design(RecyclerView代替ListView)

新闻列表的 xml 文件,layout 文件夹下面的fragment_article.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">

 <android.support.v7.widget.RecyclerView
  android:id="@+id/rcv_article"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="1" />
</LinearLayout>

2、Adapter 实现

主要步骤是:

根据上面的 item_article_type_1.xml实现一个 class ImageItemArticleViewHolder extends RecyclerView.ViewHolder
继承RecyclerView.Adapter ,class ItemArticleListAdapter extends RecyclerView.Adapter <...>