RecyclerView添加两种布局
简介:
本篇博客主要介绍如何在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添加两种布局的更多相关文章
- Android 常用UI控件之TabHost(1)TabHost的两种布局方式
TabHost是Android中的tab组件. TabHost布局文件的基本结构 TabHost下有个layout,这个layout中有TabWidget与FrameLayout.TabWidget是 ...
- 两种布局的ListVIew Adapter。例如微信对话界面
这个界面 实现的不是微信对话界面.实现的是,focus的状态下,变为放大的另一种布局 重点: 一.定义类型个数 private final int TYPE_COUNT = 2; privat ...
- word中一页中添加两种不同的页码
,在文档编写的过程中,可能一个页面需要编写两个不同的页码,举个例子,在页脚有一个页码是整个文档的页码,页眉有一个页码,是每个章节的页码: 设置如下: 此处选中这个图标是为了能够看到分节符和其他的符号 ...
- 两种经典电商CSS布局
圣杯布局和双飞翼布局! 两种布局功能相同,都是为了实现两端宽度固定,中间宽度自适应的三栏布局 圣杯布局: 三个区域都处于左浮动状态,并使main的宽度成父容器的100% 为两侧侧边栏添加负margin ...
- 七种CSS左侧固定,右侧自适应两栏布局
一 两栏布局基本HTML和CSS 首先创建基本的HTML布局和最基本的样式. 基本的样式是,两个盒子相距20px, 左侧盒子宽120px,右侧盒子宽度自适应 <div class="w ...
- Cesium 中两种添加 model 方法的区别
概述 Cesium 中包含两种添加 model 的方法,分别为: 通过 viewer.entities.add() 函数添加 通过 viewer.scene.primitives.add() 函数添加 ...
- TouTiao开源项目 分析笔记15 新闻详情之两种类型的实现
1.预览效果 1.1.首先看一下需要实现的效果. 第一种,文字类型新闻. 第二种,图片类型新闻. 1.2.在NewsArticleTextViewBinder中设置了点击事件 RxView.click ...
- bootstrap的栅格布局与两列布局结合使用
在工作中我们常常需要实现响应式布局,这个可以使用bootstrap的栅格系统来实现,我们在列里也需要实现一部分的响应式.比如下面的效果图,需要实现左边图标固定,右边的自适应 : 左边固定宽度,右边自适 ...
- Recyclerview添加头布局和尾布局,点击效果
简介: 本篇博客主要包括recyclerview添加多种布局以及添加头布局和尾布局,还有item点击事件 思路: 主要重写Recyclerview.Adapter中的一些方法 1.public int ...
随机推荐
- 使用阿里云服务器(OOS)实现图片上传
一: 页面 <%@ page language="java" contentType="text/html; charset=utf-8" pageEnc ...
- 【转】jmeter实践
目录(?)[-] Jmeter相关概念简介 应用实例 AbApacheBench对比 本文主要介绍性能测试中的常用工具jmeter的使用方式,以方便开发人员在自测过程中就能自己动手对系统进行自动压测和 ...
- java多线程====练习继承Thread
package com.aa; class XianCheng extends Thread { @Override public void run() { for (int i = 0; i < ...
- kali virtualbox
提示需要头文件错误 C header files matching your running kernel were not found. Refer to your distribution's ...
- PL/SQL 训练06--字符串处理
现在需要做一个任务调度,请大家设计,满足以下需求(1)任务可配置,比如可以配置PKG方法TEST_PROCEDURE(:1,:2...),可以是任意多个入参的方法,也可以没有入参(2)每个方法的实际参 ...
- model进阶(queryset,中介模型,查询优化,extra)
queryset 方法 ############# 可切片 def queryTest(request): ret = models.Atricle.objects.all() 数据库查询 print ...
- IDA Pro 权威指南学习笔记(五) - IDA 主要的数据显示窗口
在默认配置下,IDA(从 6.1 版开始)会在对新二进制文件的初始加载和分析阶段创建 7 个显示窗口 3 个立即可见的窗口分别为 IDA-View 窗口.函数窗口和消息输出窗口 可以通过 View - ...
- MFC 文档/视图
1.文档修改后,关闭时需要保存,主要用到2个函数,在需要更改文档内容的函数里调用SetModifiedFlag(TRUE),另一个就是SaveModified()函数,简单的例子: BOOL CMFC ...
- Python可执行对象——exec、eval、compile
Python提供的调用可执行对象的内建函数进行说明,涉及exec.eval.compile三个函数.exec语句用来执行存储在代码对象.字符串.文件中的Python语句,eval语句用来计算存储在代码 ...
- 前端自动化之nvm安装
nvm ——node环境版本控制工具. 1.解压安装包 2.打开setting文件,修改文件内容 root: D:\node\nvm path: D:\node\nodejs arch: proxy: ...