本案例来自于学校的一个简单的课程实验

先看效果图,可以显然的看到,一些item是不同的布局,而其他布局就是简单的布局嵌套

看一下xml代码:

<?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"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:orientation="vertical"android:background="@color/light_gray_color"android:layout_height="match_parent"android:theme="@style/Theme.AppCompat.NoActionBar"tools:context=".MainActivity40">
<include layout="@layout/title_bar"/><LinearLayoutandroid:orientation="horizontal"android:layout_width="match_parent"android:layout_height="40dp"android:background="@color/white"><TextViewstyle="@style/tvStyle"android:text="推荐"android:textColor="#000000"/><TextViewstyle="@style/tvStyle"android:text="热点"android:textColor="#000000"/><TextViewstyle="@style/tvStyle"android:text="视频"android:textColor="#000000"/><TextViewstyle="@style/tvStyle"android:text="北京"android:textColor="#000000"/><TextViewstyle="@style/tvStyle"android:text="热点"android:textColor="#000000"/><TextViewstyle="@style/tvStyle"android:text="娱乐"android:textColor="#000000"/></LinearLayout><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="#eeeeee"/><androidx.recyclerview.widget.RecyclerViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/rv_list"/>
</LinearLayout>

XML代码中部分重复样式写入到了style文件中

看一下加入style.xm的代码,这个文件是存放在value文件夹下,存放样式、主题等。

<style name="tvStyle"><item name="android:layout_width">wrap_content</item><item name="android:layout_height">match_parent</item><item name="android:padding">10dp</item><item name="android:gravity">center</item><item name="android:textSize">15sp</item></style>

看一下Activity的Java代码

使用数组存放标题、内容、时间、图片等数据(因为现在还只是死数据)

setDate(单词拼错了!) 该函数,就是将数据填充到NewsList(一个泛型List)中

然后就是new一个自己声明的一个自定义适配器,使用Listview的setAdapter方法设置其适配器

public class MainActivity40 extends AppCompatActivity {
private  String[] titles={"各地餐企齐行动,杜绝餐饮浪费","花菜有人焯水,有人直接炒,都错了,看饭店大厨如何做","睡觉时,双脚突然蹬一下,有踩空感,像从高楼坠落,是咋回事?","实拍外卖小哥砸开小吃店的卷帘门救火,灭火后淡定决定继续送外卖","还没成熟就被迫提前采摘,8毛一斤却没人要,果农无奈,不摘不行","大会、大展、大赛一起来,北京电竞“好嗨哟”"};
private String[] names={"央视新闻客户端","味美食记","民富康健康","生活小记","禾木报告","燕鸣"};
private String[] comments={"1234评","14214评","534评","134评","1353评","876评"};
private String[]times={"刚刚","6小时前","8小时前","2小时前","刚刚","4小时前"};
private  int[] icons1={R.drawable.food,R.drawable.takeout,R.drawable.e_sports};
private int[] icons2={R.drawable.sleep1,R.drawable.sleep2,R.drawable.sleep3,R.drawable.fruit1,R.drawable.fruit2,R.drawable.fruit3};
private int[] types={1,1,2,1,2,1};
private RecyclerView mRecyclerView;
private myAdapter myAdapter;
private List<NewsBean> NewsList;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main40);setDate();mRecyclerView=findViewById(R.id.rv_list);mRecyclerView.setLayoutManager(new LinearLayoutManager(this));myAdapter=new myAdapter(MainActivity40.this,NewsList);mRecyclerView.setAdapter(myAdapter);}private void setDate(){NewsList=new ArrayList<NewsBean>();NewsBean bean;for (int i = 0; i < titles.length; i++) {bean=new NewsBean();bean.setId(i+1);bean.setTitle(titles[i]);bean.setName(names[i]);bean.setComment(comments[i]);bean.setTime(times[i]);bean.setType(types[i]);switch (i){case 0:List<Integer> imgList0=new ArrayList<>();bean.setImgList(imgList0);break;case 1:List<Integer> imgList1=new ArrayList<>();imgList1.add(icons1[i-1]);bean.setImgList(imgList1);break;case 2:List<Integer> imgList2=new ArrayList<>();imgList2.add(icons2[i-2]);imgList2.add(icons2[i-1]);imgList2.add(icons2[i]);bean.setImgList(imgList2);break;case 3:List<Integer> imgList3=new ArrayList<>();imgList3.add(icons1[i-2]);bean.setImgList(imgList3);break;case 4:List<Integer> imgList4=new ArrayList<>();imgList4.add(icons2[i-1]);imgList4.add(icons2[i]);imgList4.add(icons2[i+1]);bean.setImgList(imgList4);break;case 5:List<Integer> imgList5=new ArrayList<>();imgList5.add(icons1[i-3]);bean.setImgList(imgList5);break;}NewsList.add(bean);}}}

其中涉及到了适配器,话不多说,上适配器代码,适配器是连接数据与Listview的一个“桥梁”

RecyclerView与ListView的适配器其实共同特征有很多。

在自定义的适配器中,首先是一个构造方法,来获取当前上下文以及数据列表

class myAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{private Context mContext;private List<NewsBean> NewsList;public myAdapter(Context context, List<NewsBean> list){this.mContext=context;this.NewsList=list;}@NonNull@NotNull@Overridepublic RecyclerView.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {View itemView=null;RecyclerView.ViewHolder holder=null;if(viewType==1){itemView= LayoutInflater.from(mContext).inflate(R.layout.list_item_one,parent,false);holder=new ViewHolder1(itemView);}else  if(viewType==2){itemView= LayoutInflater.from(mContext).inflate(R.layout.list_item_two,parent,false);holder=new ViewHolder2(itemView);}return holder;}@Overridepublic int getItemViewType(int position) {return  NewsList.get(position).getType();}@Overridepublic void onBindViewHolder(@NonNull @NotNull RecyclerView.ViewHolder holder, int position) {NewsBean bean=NewsList.get(position);if(holder instanceof ViewHolder1){if(position==0){((ViewHolder1)holder).iv_top.setVisibility(View.VISIBLE);((ViewHolder1)holder).iv_img.setVisibility(View.GONE);}else {((ViewHolder1)holder).iv_top.setVisibility(View.GONE);((ViewHolder1)holder).iv_img.setVisibility(View.VISIBLE);}((ViewHolder1)holder).title.setText(bean.getTitle());((ViewHolder1)holder).name.setText(bean.getName());((ViewHolder1)holder).comment.setText(bean.getComment());((ViewHolder1)holder).time.setText(bean.getTime());if (bean.getImgList().size()==0)return;((ViewHolder1)holder).iv_img.setImageResource(bean.getImgList().get(0));}else  if(holder instanceof ViewHolder2){((ViewHolder2) holder).title.setText(bean.getTitle());((ViewHolder2) holder).name.setText(bean.getName());((ViewHolder2) holder).comment.setText(bean.getComment());((ViewHolder2) holder).time.setText(bean.getTime());((ViewHolder2) holder).iv_img1.setImageResource(bean.getImgList().get(0));((ViewHolder2) holder).iv_img2.setImageResource(bean.getImgList().get(1));((ViewHolder2) holder).iv_img3.setImageResource(bean.getImgList().get(2));}}@Overridepublic int getItemCount() {return NewsList.size();}class  ViewHolder1 extends RecyclerView.ViewHolder {ImageView iv_top,iv_img;TextView title,name,comment,time;public ViewHolder1(@NonNull @NotNull View itemView) {super(itemView);iv_top=itemView.findViewById(R.id.iv_top);iv_img=itemView.findViewById(R.id.iv_img);title=itemView.findViewById(R.id.tv_title);name=itemView.findViewById(R.id.tv_name);comment=itemView.findViewById(R.id.tv_comment);time=itemView.findViewById(R.id.tv_time);}}class  ViewHolder2 extends RecyclerView.ViewHolder {ImageView iv_img1,iv_img2,iv_img3;TextView title,name,comment,time;public ViewHolder2(@NonNull @NotNull View itemView) {super(itemView);iv_img1=itemView.findViewById(R.id.iv_img1);iv_img2=itemView.findViewById(R.id.iv_img2);iv_img3=itemView.findViewById(R.id.iv_img3);title=itemView.findViewById(R.id.tv_title);name=itemView.findViewById(R.id.tv_name);comment=itemView.findViewById(R.id.tv_comment);time=itemView.findViewById(R.id.tv_time);}}}

代码解说将会在后续补充

5-3   现在补充一下 list_item_one.xml与list_item_two.xml,这两个布局文件是Listview中两个item

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="90dp"android:layout_marginBottom="8dp"android:background="@color/white"android:padding="8dp"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/ll_info"android:orientation="vertical"><TextViewandroid:layout_width="280dp"android:layout_height="wrap_content"android:id="@+id/tv_title"android:maxLines="2"android:textColor="#3c3c3c"android:textSize="16sp"/><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid:layout_width="20dp"android:layout_height="20dp"android:id="@+id/iv_top"android:layout_alignParentBottom="true"android:src="@drawable/aplle"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_toRightOf="@+id/iv_top"android:orientation="horizontal"><TextViewstyle="@style/tvInfo"android:id="@+id/tv_name"/><TextViewandroid:id="@+id/tv_comment"style="@style/tvInfo"/><TextViewandroid:id="@+id/tv_time"style="@style/tvInfo"/></LinearLayout></RelativeLayout></LinearLayout>
<ImageViewandroid:layout_width="match_parent"android:layout_height="90dp"android:id="@+id/iv_img"android:layout_toRightOf="@id/ll_info"android:padding="3dp"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:background="@color/white"android:padding="8dp"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/tv_title"android:maxLines="2"android:padding="8dp"android:textColor="#3c3c3c"android:textSize="16sp"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/ll_img"android:layout_below="@id/tv_title"android:orientation="horizontal"><ImageViewandroid:id="@+id/iv_img1"style="@style/ivImg"/><ImageViewandroid:id="@+id/iv_img2"style="@style/ivImg"/><ImageViewandroid:id="@+id/iv_img3"style="@style/ivImg"/></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/ll_img"android:orientation="vertical"android:padding="8dp"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:id="@+id/tv_name"style="@style/tvInfo"/><TextViewandroid:id="@+id/tv_comment"style="@style/tvInfo"/><TextViewandroid:id="@+id/tv_time"style="@style/tvInfo"/></LinearLayout></LinearLayout>
</RelativeLayout>

补充一下NewBean  其实就是个实体类啦  其中get、set方法以及构造方法可以快速生成

import java.util.List;public class NewsBean {private  int id;private String title;private List<Integer> imgList;private String name;private String comment;private String time;private int type;public NewsBean() {}public NewsBean(int id, String title, List<Integer> imgList, String name, String comment, String time, int type) {this.id = id;this.title = title;this.imgList = imgList;this.name = name;this.comment = comment;this.time = time;this.type = type;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public List<Integer> getImgList() {return imgList;}public void setImgList(List<Integer> imgList) {this.imgList = imgList;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getComment() {return comment;}public void setComment(String comment) {this.comment = comment;}public String getTime() {return time;}public void setTime(String time) {this.time = time;}public int getType() {return type;}public void setType(int type) {this.type = type;}
}

Android Studio初学者实例:RecyclerView学习--模仿今日头条相关推荐

  1. Android Studio初学者实例:仿拼多多砍价页面

    本次实验较为综合,主要是用到了RecyclerView.okhttp库(用于网络访问).gson库(解析json数据).tomacat(服务器存放图片.文字等),所以代码较多,但知识点常用,如果毕设. ...

  2. Android Studio初学者实例:Fragment学习--仿美团外卖界面

    本次课程为Fragment为主题,课程的示例仿美团外卖界面,不同于底部导航栏的Fragment案例,此界面分为左侧切换与顶部切换.本文先是发布代码与效果,后续讲解将会在后续补充.先看看效果: 首先是布 ...

  3. Android Studio初学者实例:SQLite实验:绿豆通讯录

    本次实验是使用SQLite对一个通讯录表进行简单增删改查 以下是实验效果: 首先是继承SQLiteOpenHelper的数据库自定义类 对于此类必须继承于SQLiteOpenHelper ,当new创 ...

  4. Android Studio初学者实例:SharedPreferences 登录/注册模块实现

    该实验是紧接上一期使用SP登录.记住密码的实验,主要实现是除了上次登陆以外,登录以后在主界面有一系列假数据,可以通过右上角进行排序选择,以下是实验效果: 示例:好友名排序效果 首先是登陆界面以及逻辑代 ...

  5. Android Studio第六课:模仿QQ登录跳转

    Android Studio第六课:模仿QQ登录跳转 导包,添加依赖 登陆界面 欢迎界面 主界面 导包,添加依赖 找到build.gradle,后缀是自己建的项目名 // 基础依赖包,必须要依赖imp ...

  6. 《Android Studio开发实战》学习(五) - 截图

    <Android Studio开发实战>学习(五) - 截图 背景 页面布局 布局文件的编写 代码文件的编写 ImageView控件截图的原理 运行结果 背景 在这里继续学习Android ...

  7. 《Android Studio开发实战》学习(二)- 聊天室

    <Android Studio开发实战>学习(二)- 聊天室 背景 聊天室布局文件的编写 聊天室代码文件的编写 运行结果 背景 在前一篇文章 1中实现了使用Android Studio开发 ...

  8. 《Android Studio开发实战》学习(一)- Hello World

    <Android Studio开发实战>学习(一)- Hello World 背景 Android Studio的安装 Android Studio的启动和运行 运行小应用Hello Wo ...

  9. 《Android Studio开发实战》学习(三)- 展示图片

    <Android Studio开发实战>学习(三)- 展示图片 背景 问题描述 将图片添加到Android Studio资源中 图像视图ImageView的使用 关闭APP中标题的显示 图 ...

最新文章

  1. 如何判断你的数据集是否适合使用深度学习模型?如果数据量太小有什么解决办法?
  2. SpringMVC的数据响应方式-页面跳转
  3. UEFI下用rufus安装ubuntu16.04 LTS
  4. 使用Moles对静态方法做UnitTest
  5. 有效创建Oracle dblink的两种方式
  6. python从指定文件夹导入模块_Python实现的在特定目录下导入模块功能分析
  7. C# 访问数据的时候报错 (拒绝了对对象 'XXXX' (数据库 'SHQY',架构 'dbo')的 SELECT 权限)...
  8. iOS网络请求 get - post 区别
  9. P、NP、NPC和NP-Hard相关概念的图形和解释
  10. 6.MySQL中文排序(根据拼音排序)
  11. 用计算机绘制滴定曲线,利用Excel系统绘制酸碱滴定曲线
  12. rtl8201以太网卡调试
  13. [Android6.0][MTK6737] 启动流程分析
  14. 从零开始Kubernetes CronJob实现任务调度
  15. matlab拟合公式不准确,Matlab拟合函数误差:函数值和YDATA大小不相等
  16. 2021-07-27_TPM描述
  17. select苹果手机样式设置
  18. blob开头的文件无法下载怎么办
  19. Kinect for Unity检测身高方法
  20. SaaSBase:推荐一些超好用的低代码开发软件(中篇)

热门文章

  1. FTP如何设置用户名密码
  2. 解决can't resolve the symbol 'R'方法(转载)
  3. HP惠普打印机驱动安装详解
  4. 安卓手机刷入面具Magisk
  5. 苹果的胜利:Adobe宣布Flash Media服务支持iOS设备运行Flash
  6. 公众号数据全面分析解读(上篇)
  7. 湘潭大学计算机组成原理试卷,湘潭大学11级原理课堂测验题集.ppt
  8. 通过堡垒机rdp 黑屏_如何绕过堡垒机远程登录
  9. 怎么制作书单视频?免费制作书单视频软件分享
  10. 2022年「博客之星」参赛博主:顽石九变