简介:

  本篇博客主要介绍如何在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. path设置

    查看 export declare -x HISTCONTROL="ignoredups"declare -x HISTSIZE="1000"declare - ...

  2. ffmpeg摄像头采集h264编码RTP发送

    一. 相关API说明 1. av_register_all 2. avformat_network_init 不管是流媒体发送还是流媒体接收, 需要先执行该函数. 3. avformat_alloc_ ...

  3. Oracle 错误: sp2 0734 unknown command beginning -- 解决方法

    今天在做一个Oracle的倒库操作,使用SQLPLUS倒库的时候发生SP2 0734的错误 !! 注:我是用的是Ocale11g自带的SQLPLUS 由于之前在工具(SQL Developer)里面运 ...

  4. Cocos2D中的内部函数

    virtual bool isVisible();    // 是否可见    virtual void setVisible(bool visible);      // 设置是否可见    boo ...

  5. html5测试总结

    1.因为html5不兼容IE78,所以在PC上使用并非十分光.pc上IE还是占主流 2.html5主要用在移动终端 3.html5短期内因为自身的缺陷,用户体验无法达到原生app的体验.如:html5 ...

  6. babel-preset-latest使用介绍

    本文介绍babel中的babel-preset-latest插件 简介 该插件的作用是包含了每年的js代码转换规则 默认包含es2015,es2016,es2017,默认对这些代码都会进行转码,从而方 ...

  7. cs231n神经网络 常用激活函数

    CS231n课程笔记翻译:神经网络笔记1(上) 一.常用激活函数 每个激活函数(或非线性函数)的输入都是一个数字,然后对其进行某种固定的数学操作.下面是在实践中可能遇到的几种激活函数: ——————— ...

  8. 提高ListView的效率

    按照普通的写法,如果ListView里面有比较多的东西的话,在加载每一个Item的时候,是非常非常卡的.具体表现就是滚动起来的时候会看见明显的卡顿. 关键还是处理自定义Adapter里面的getVie ...

  9. 微信小程序中在页面中实现下拉刷新显示提醒语后在消失

    最近在做小程序的时候遇见一个问题,就是页面要下拉刷新给客户一个提醒语,查看了小程序的官方文档 这里有个注意点:如果你是一页进行下拉刷新就在那个文件夹的json里面加上"enablePullD ...

  10. 【292】Python 关于中文字符串的操作

    参考:什么是字符编码? 参考:Unicode 和 UTF-8 有何区别? 参考:python中文decode和encode转码 一.相关说明 Python 中关于字符串的操作只限于英文字母,当进行中文 ...