简介:

  本篇博客主要介绍如何在RecyclerView中添加两种布局

  思路:主要重写Recyclerview.Adapter中的一些方法

  1.public int getItemViewType(int position)   获取不同的type

  2.public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)  根据不同的viewType   添加不同的布局

案例:

1.布局

1)activity_main.xml

  

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> </RelativeLayout>

2)第一种子布局item_recyclerview

  

<?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="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp"
app:contentPadding="5dp"
app:cardBackgroundColor="#44ff0000"
android:layout_margin="5dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/img"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv1"
android:maxLines="1"
android:ellipsize="end"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv2"
android:maxLines="1"
android:ellipsize="end"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView> </LinearLayout>

2)第二种子布局item_recyclerview2

  

<?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="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp"
app:contentPadding="5dp"
app:cardBackgroundColor="#4400ff00"
android:layout_margin="5dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
> <LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginRight="10dp"
android:gravity="center_vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv1"
android:maxLines="1"
android:ellipsize="end"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv2"
android:maxLines="1"
android:ellipsize="end"
/>
</LinearLayout>
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/img"
/>
</LinearLayout>
</android.support.v7.widget.CardView> </LinearLayout>

2.数据 Person类

  

package com.example.dhj.recyclerviewtest;

/**
* Created by Administrator on 2017/7/31 0031.
*/ public class Person {
private int age;
private String name;
private String detail;
private int imgId; public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getDetail() {
return detail;
} public void setDetail(String detail) {
this.detail = detail;
} public int getImgId() {
return imgId;
} public void setImgId(int imgId) {
this.imgId = imgId;
}
}

3.适配器MyAdapter

  

package com.example.dhj.recyclerviewtest;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView; import java.util.List; /**
* Created by Administrator on 2017/7/31 0031.
*/ public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private Context context;
private List<Person> datas; private static final int ITEM_ONE=1;
private static final int ITEM_TWO=2;
public MyAdapter(Context context, List<Person> datas) {
this.context = context;
this.datas = datas;
} @Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if(viewType==ITEM_ONE){
view= LayoutInflater.from(context).inflate(R.layout.item_recyclerview,parent,false);
}else{
view=LayoutInflater.from(context).inflate(R.layout.item_recyclerview2,parent,false);
}
ViewHolder vh=new ViewHolder(view);
return vh;
} @Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.img.setBackgroundResource(datas.get(position).getImgId());
holder.tv1.setText(datas.get(position).getName());
holder.tv2.setText(datas.get(position).getDetail());
} @Override
public int getItemCount() {
return datas==null?0:datas.size();
} @Override
public int getItemViewType(int position) {
if(position%2==0){
return ITEM_ONE;
}else{
return ITEM_TWO;
}
} class ViewHolder extends RecyclerView.ViewHolder{
private TextView tv1,tv2;
private ImageView img; public ViewHolder(View itemView) {
super(itemView);
tv1=itemView.findViewById(R.id.tv1);
tv2=itemView.findViewById(R.id.tv2);
img=itemView.findViewById(R.id.img);
}
}
}

4.Activity

  

public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView; private List<Person> datas;
private MyAdapter adapter; private int[] imgs={R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5,R.drawable.pic1,
R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.pic5,R.drawable.pic6}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView= (RecyclerView) findViewById(R.id.recyclerview);
initData();
initRecyclerView();
} private void initRecyclerView() {
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter=new MyAdapter(this,datas);
recyclerView.setAdapter(adapter); } private void initData() {
datas=new ArrayList<>();
for(int i=0;i<imgs.length;i++){
Person p=new Person();
p.setName("hahfa"+i);
p.setDetail("今天是星期一,还有5天才能休息"+i);
p.setImgId(imgs[i]);
datas.add(p);
}
}
}

RecyclerView添加两种布局的更多相关文章

  1. Android 常用UI控件之TabHost(1)TabHost的两种布局方式

    TabHost是Android中的tab组件. TabHost布局文件的基本结构 TabHost下有个layout,这个layout中有TabWidget与FrameLayout.TabWidget是 ...

  2. 两种布局的ListVIew Adapter。例如微信对话界面

    这个界面  实现的不是微信对话界面.实现的是,focus的状态下,变为放大的另一种布局 重点: 一.定义类型个数 private final int TYPE_COUNT = 2;    privat ...

  3. word中一页中添加两种不同的页码

    ,在文档编写的过程中,可能一个页面需要编写两个不同的页码,举个例子,在页脚有一个页码是整个文档的页码,页眉有一个页码,是每个章节的页码: 设置如下: 此处选中这个图标是为了能够看到分节符和其他的符号 ...

  4. 两种经典电商CSS布局

    圣杯布局和双飞翼布局! 两种布局功能相同,都是为了实现两端宽度固定,中间宽度自适应的三栏布局 圣杯布局: 三个区域都处于左浮动状态,并使main的宽度成父容器的100% 为两侧侧边栏添加负margin ...

  5. 七种CSS左侧固定,右侧自适应两栏布局

    一 两栏布局基本HTML和CSS 首先创建基本的HTML布局和最基本的样式. 基本的样式是,两个盒子相距20px, 左侧盒子宽120px,右侧盒子宽度自适应 <div class="w ...

  6. Cesium 中两种添加 model 方法的区别

    概述 Cesium 中包含两种添加 model 的方法,分别为: 通过 viewer.entities.add() 函数添加 通过 viewer.scene.primitives.add() 函数添加 ...

  7. TouTiao开源项目 分析笔记15 新闻详情之两种类型的实现

    1.预览效果 1.1.首先看一下需要实现的效果. 第一种,文字类型新闻. 第二种,图片类型新闻. 1.2.在NewsArticleTextViewBinder中设置了点击事件 RxView.click ...

  8. bootstrap的栅格布局与两列布局结合使用

    在工作中我们常常需要实现响应式布局,这个可以使用bootstrap的栅格系统来实现,我们在列里也需要实现一部分的响应式.比如下面的效果图,需要实现左边图标固定,右边的自适应 : 左边固定宽度,右边自适 ...

  9. Recyclerview添加头布局和尾布局,点击效果

    简介: 本篇博客主要包括recyclerview添加多种布局以及添加头布局和尾布局,还有item点击事件 思路: 主要重写Recyclerview.Adapter中的一些方法 1.public int ...

随机推荐

  1. (7)Stream简介

    流的个人理解: 怎样获得流: 怎样处理流: 中间操作和终端操作介绍: 中间操作和终端操作的使用: 流的个人理解: Stream也就是流,他和IO流不一样,他是java8诞生的东西,我对他的理解就是一个 ...

  2. 今天去python官网下载包安装的时候的问题记录

    去官网下载了 tar压缩包 放到了site-packages下解压 然后使用 python setup.py install 安装 安装完后,所要引用的模块文件居然还在解压出来的压缩文件里面,导致无法 ...

  3. 【转】使用JMeter进行负载测试——终极指南

    使用JMeter进行负载测试——终极指南 这篇教程讨论的是JMeter,它是一款基于Java的.集合了几个应用程序.具有特定用途的负载和性能测试工具. 本篇主要涉及的内容: 解释一下JMeter的用途 ...

  4. 使用Fiddler对android应用抓http或https包

    工作原理 先上个图 此图一目了然,可以看出fiddler在请求中所处的位置,我们就可以确定它能干些什么.   WinInet(“Windows Internet”)API帮助程序员使用三个常见的Int ...

  5. Java之常用类及方法

    下面我们介绍Java类库所提供的常用类及类的常用方法 一.java.lang.String 1. String类常用的构造函数 public String(String original) 使用串对象 ...

  6. 黑客工具包ShadowBrokers浅析

    臭名昭著的方程式组织工具包再次被公开,TheShadowBrokers 在 steemit.com博客上提供了相关消息. 本次被公开的工具包大小为117.9MB,包含23 个黑客工具,其中部分文件显示 ...

  7. Mycat实战之新增基于hash分片的表

    1. 修改rule.xml hash分片规则 主要改两个地方: vi rule.xml 分片数量,这里改为3 对应 三个库 hash规则 默认是id列 这里为 PROVINCE 2. reload 加 ...

  8. FFmpeg新版本(2016年10月份以后) 支持硬件解码

    FFmpeg provides a subsystem for hardware acceleration. Hardware acceleration allows to use specific ...

  9. cacti启动有图无数据

    cactiEZ服务器重启后,获取不到图形的解决办法 cd /var/www/html/cli/ php -q rebuild_poller_cache.php -d myisamchk --safe- ...

  10. 嵌入式Linux启动配置文件及脚本分…

    使用Busybox制作根文件系统时,/etc目录非常重要,它包含了嵌入式Linux启动所需的配置文件及脚本.由于init进程,或者说linuxrc程序会解析inittab文件,因此就从/etc/ini ...