简介:
本篇博客主要介绍如何在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); } } }