简介:

  本篇博客主要介绍如何在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. fuser命令

    fuser命令 http://blog.itpub.net/27573546/viewspace-765240/

  2. windows下安装ImageMagick

    最近在开发过程中用到了ImageMagick,结合网上的教程做一下记录 安装的具体步骤可以参考http://blog.csdn.net/belen_xue/article/details/728962 ...

  3. FPGA各大厂商,不可不知

    引言: FPGA市场前景诱人,但是门槛之高在芯片行业里无出其右.全球有60多家公司先后斥资数十亿美元,前赴后继地尝试登顶FPGA高地,其中不乏英特尔.IBM.德州仪器.摩托罗拉.飞利浦.东芝.三星这样 ...

  4. 【转】Jenkins+Ant+Jmeter搭建持续集成的接口测试平台

    一.什么是接口测试? 接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换,传递和控制管理过程,以及系统间的相互逻 ...

  5. netty中的UDP

    UDP 提供了向多个接收者发送消息的额外传输模式: 多播——传输到一个预定义的主机组: 广播——传输到网络(或者子网)上的所有主机. 本示例应用程序将通过发送能够被同一个网络中的所有主机所接收的消息来 ...

  6. 阿里云openapi接口使用,PHP,视频直播

    1.下载sdk放入项目文件夹中 核心就是aliyun-php-sdk-core,它的配置文件会自动加载相应的类 2.引入文件 include_once LIB_PATH . 'ORG/aliyun-o ...

  7. php图像处理函数imagecopyresampled

    语法 bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , i ...

  8. Maven Assembly打包提示[WARNING] transitive dependencies if any will not be available

    maven assembly打包出现错误 [WARNING] The POM for com.flink.xxr:0.0.1-SNAPSHOT is invalid, transitive depen ...

  9. 关于Pycharm中如何注释

    主要有三种方法: ①在程序行前面加“#” ②将需要注释的代码用'''----''' 包起来,此方法类似C的注释方法 ③将需要注释的代码选住然后利用“Ctrl+/”进行注释 # test=input(' ...

  10. CSS——优先级

    转自:http://www.planabc.net/2008/05/06/css_specificity/ CSS2.1 中规定了关于 CSS 规则 Specificity(特异性)的计算方式,用一个 ...