简介:

  本篇博客主要介绍如何在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. TableView刷新 局部刷新等

    1.对整个页面刷新 [ tableView reloadData]; 2.对某一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithI ...

  2. 安装S_S相关报错的troubleshooting

    在安装S_S server时,在Debian上会出现类似如下的报错: File , in <module> sys.exit(main()) File , in main config = ...

  3. 你知道PING功能是怎么实现的吗

    以太网的协议有层,而每层都包含有更多的协议.所谓协议,通俗的讲就是通信双方约定的规则. 今天我们介绍一些一个听起来陌生却有很常用的协议,ICMP协议. —ICMP是(Internet Control ...

  4. web项目WebContent目录结构参考(WEB-INF)

    WEB-INF目录是Java WEB应用的安全目录,客户端(浏览器等)无法访问,只有服务端可以访问.该目录主要用来存放配置文件,如web.xml等. 若是将jsp文件放在WEB-INF目录中,则必须通 ...

  5. Oracle 复杂查询(1)

    一.复杂查询 1. 列出至少有一个员工的所有部门编号.名称,并统计出这些部门的平均工资.最低工资.最高工资. 1.确定所需要的数据表: emp表:可以查询出员工的数量: dept表:部门名称: emp ...

  6. 【转】Context.getExternalFilesDir()和Context.getExternalCacheDir()方法

    应用程序在运行的过程中如果需要向手机上保存数据,一般是把数据保存在SDcard中的.大部分应用是直接在SDCard的根目录下创建一个文件夹,然后把数据保存在该文件夹中.这样当该应用被卸载后,这些数据还 ...

  7. Linux - 创建用户的相关文件

    创建一个用户会与 6 个文件相关 /etc/passwd 储存了所有用户的相关信息 第一行中,从左往右 root 为用户名,: 为分隔符,x 为密码,0 为 uid,0 为 gid,root 为用户的 ...

  8. 迷你MVVM框架 avalonjs 0.99发布

    在本版本主要是性能优化,添加一些有用的功能(如回调什么的),离成品阶段不远了. 修正 updateViewModel bug 修正监控数组的set方法 bug 添加data-each-rendered ...

  9. Variable hoisting Function hoisting

    Variable hoisting Another unusual thing about variables in JavaScript is that you can refer to a var ...

  10. UML类图介绍及简单用法

    原文链接 一.类的属性的表示方式 在UML类图中,类使用包含类名.属性(field) 和方法(method) 且带有分割线的矩形来表示,比如下图表示一个Employee类,它包含name,age和em ...