Recyclerview的多类型item入门
compile 'com.android.support:recyclerview-v7:25.3.1'
适配器
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.junruiyun.superman.R;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Smile on 2017/6/1.
*/
public class OrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<String> list=new ArrayList<>();
public static final int TYPE_ORDER_HEAD=0xff01;
public static final int TYPE_ORDER_BODY=0xff02;
public OrderAdapter(List list){
this.list=list;
}
class HolderTypeHead extends RecyclerView.ViewHolder{
TextView textView;
public HolderTypeHead(View itemView){
super(itemView);
textView=(TextView)itemView.findViewById(R.id.head_item);
}
}
class HolderTypeBody extends RecyclerView.ViewHolder{
TextView textView;
public HolderTypeBody(View itemView){
super(itemView);
textView=(TextView) itemView.findViewById(R.id.body_item);
}
}
/**
* 创建子项布局
* @param parent
* @param viewType
* @return
*/
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType){
case TYPE_ORDER_HEAD:
return new HolderTypeHead(LayoutInflater.from(parent.getContext()).inflate(R.layout.order_recycler_view_head_item,parent,false));
case TYPE_ORDER_BODY:
return new HolderTypeBody(LayoutInflater.from(parent.getContext()).inflate(R.layout.order_recycler_view_body_item,parent,false));
default:
return null;
}
}
/**
*绑定子项布局
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof HolderTypeHead){
bindTypehead((HolderTypeHead) holder,position);
}else if (holder instanceof HolderTypeBody){
bindTypeBody((HolderTypeBody) holder,position);
}
}
/**
* 设定类型
* @param position
* @return
*/
@Override
public int getItemViewType(int position) {
if (position==0){
return TYPE_ORDER_HEAD;
}else{
return TYPE_ORDER_BODY;
}
}
/**
* 根据遍历的类型进行指定显示几行
* @param recyclerView
*/
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
RecyclerView.LayoutManager manager=recyclerView.getLayoutManager();
if (manager instanceof GridLayoutManager){
final GridLayoutManager gridLayoutManager=(GridLayoutManager)manager;
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
int TYPE=getItemViewType(position);
switch (TYPE){
case TYPE_ORDER_HEAD:
return gridLayoutManager.getSpanCount();
case TYPE_ORDER_BODY:
return 1;
default:
return gridLayoutManager.getSpanCount();
}
}
});
}
}
/**
* 指定控件的长度
* @return
*/
@Override
public int getItemCount() {
return list.size()+1;
}
/**
* 头部ViewHold
*/
private void bindTypehead(HolderTypeHead holder,int position){
holder.textView.setText("红名列表");
}
/**
* 身体ViewHold
*/
private void bindTypeBody(HolderTypeBody holder,int position){
String text=list.get(position-1);
holder.textView.setText(text);
}
}