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);
        }
    }
}

 

转载于:https://www.cnblogs.com/wangjiaghe/p/7261571.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以在 RecyclerView添加头部和尾部视图来实现添加 header 和 footer 的效果。下面是一种常见的实现方式: 首先,你需要创建两个布局文件用作 header 和 footer 的视图。例如,header_view.xml 和 footer_view.xml。 然后,在你的 RecyclerView 的适配器中,你需要创建两个常量来表示 header 和 footer 的视图类型。例如,HEADER_VIEW_TYPE 和 FOOTER_VIEW_TYPE。 接下来,在适配器中,你需要重写以下几个方法: 1. getItemViewType(int position) 方法:根据 position 来返回相应的视图类型。如果 position 是 0,则返回 HEADER_VIEW_TYPE;如果 position 是数据集合的大小加上 1,则返回 FOOTER_VIEW_TYPE;否则返回普通的 item 类型。 2. onCreateViewHolder(ViewGroup parent, int viewType) 方法:根据 viewType 来创建对应的 ViewHolder。如果 viewType 是 HEADER_VIEW_TYPE 或 FOOTER_VIEW_TYPE,则使用相应的布局文件创建 ViewHolder;否则使用普通的 item 布局文件创建 ViewHolder。 3. onBindViewHolder(ViewHolder holder, int position) 方法:根据 position 来绑定数据到 ViewHolder。如果 position 是 HEADER_VIEW_TYPE 或 FOOTER_VIEW_TYPE,则不需要绑定数据;否则绑定普通的 item 数据。 最后,在你的 RecyclerView 中设置适配器,并在数据集合中添加对应的数据项作为 header 和 footer。例如,使用以下代码: ``` MyAdapter adapter = new MyAdapter(dataList); adapter.addHeader(headerData); adapter.addFooter(footerData); recyclerView.setAdapter(adapter); ``` 请注意,上述代码中的 MyAdapter 是你自定义的 RecyclerView.Adapter 子类,其中包含了添加 header 和 footer 的方法。 以上就是在 RecyclerView添加 header 和 footer 的基本步骤。希望能对你有所帮助!如有需要,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值